You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

122 lines
3.9 KiB

# coding=utf-8
from gettext import gettext as _
from django.contrib import messages
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.views import LoginView
from django.db import transaction
from django.http import HttpResponse
from django.shortcuts import redirect
from django.shortcuts import render
from django.urls import reverse
from app.forms import ProfileForm
from app.forms import UserForm
def index(request) -> HttpResponse:
context = {
'site': {
'title': 'Django template'
}
}
if request.user.is_authenticated:
return render(request, template_name='app/home.html', context=context)
else:
return render(request, template_name='app/landing.html', context=context)
@transaction.atomic
def register(request) -> HttpResponse:
if request.user.is_authenticated:
return redirect('app:index')
user_form = UserCreationForm(data=request.POST or None)
profile_form = ProfileForm(data=request.POST or None, files=request.FILES or None)
context = {
'site': {
'title': 'Django template'
},
'user_form': user_form,
'profile_form': profile_form
}
if request.method == "POST" and user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
profile.save()
login(request, user=user)
messages.success(request, _('User was successfully registered'))
return redirect('edit_profile')
return render(request, template_name='registration/register.html', context=context)
def user_login(request) -> HttpResponse:
login_view = LoginView()
login_view.setup(request)
login_view.redirect_authenticated_user = True
login_view.extra_context = {
'active': 'login',
'title': _('Login'),
'site': {
'title': 'Django template'
},
}
return login_view.dispatch(request)
@login_required()
@transaction.atomic
def edit_profile(request) -> HttpResponse:
user_form = UserForm(data=request.POST or None, instance=request.user)
profile_form = ProfileForm(data=request.POST or None, files=request.FILES or None,
instance=request.user.profile)
user_form.fields['email'].help_text = _('The email address is required for password recovery.')
context = {
'active': 'edit_profile',
'title': _('Edit User Profile'),
'site': {
'title': 'Django template'
},
'user_form': user_form,
'profile_form': profile_form
}
if request.method == "POST" and user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile = profile_form.save(commit=False)
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
profile.save()
messages.success(request, _('Profile was successfully updated'))
return render(request, template_name='registration/edit_profile.html', context=context)
@login_required()
def password_change_done(request) -> HttpResponse:
messages.success(request, _('Your password was successfully changed.'))
return redirect(reverse('edit_profile'))
def legal(request) -> HttpResponse:
context = {
'site': {
'title': 'Django template'
}
}
return render(request, template_name='app/legal.html', context=context)
def privacy(request) -> HttpResponse:
context = {
'site': {
'title': 'Django template'
}
}
return render(request, template_name='app/privacy.html', context=context)