Пример #1
0
def user_registration(request):
    if request.method == "POST":
        form = UserForm(request.POST)
        if form.is_valid():
            try:
                user = form.save()
                main_cycle = MainCycle()
                main_cycle.user = user
                main_cycle.save()
                user = authenticate(request,
                                    username=request.POST['username'],
                                    password=request.POST['password'])
                login(request, user)
                return True, 'index', {}
            except ValidationError as error:
                return (False, 'registration.html', {
                    'invalid': True,
                    'form': form,
                    'message': error.messages[0]
                })
        else:
            return False, 'registration.html', {
                'invalid': True,
                'form': form,
                'message': 'Incorrect Data'
            }
    else:
        return False, 'registration.html', {
            'invalid': False,
            'form': UserForm(),
        }
Пример #2
0
def create_or_update_user(request: HttpRequest, user_form: UserForm,
                          user_profile_form: UserProfileForm,
                          creating: bool) -> HttpResponse:
    if not user_form.is_valid() or not user_profile_form.is_valid():
        messages.add_message(request, messages.ERROR,
                             user_form.non_field_errors())
    else:
        try:
            if creating:
                user = User.objects.create_user(
                    username=user_form.cleaned_data['username'],
                    email=user_form.cleaned_data['email'],
                    password=user_form.cleaned_data['password'],
                    is_active=True)
            else:
                user = user_form.instance
                user.username = user_form.cleaned_data['username']
                new_pass = user_form.cleaned_data.get('password')
                if new_pass:
                    user.set_password(new_pass)
                else:
                    user.password = user_form.initial['password']
                user.email = user_form.cleaned_data['email']
                user.save()
            user_profile = user_profile_form.save(commit=False)
            user_profile.user_info = user
            user_profile.save()
            if creating:
                sessionlist = Session.objects.all()
                for item in sessionlist:
                    persessionuserlikes = PerSessionUserLikes(
                        user=user_profile, session=item)
                    persessionuserlikes.save()
                messages.add_message(
                    request, messages.SUCCESS,
                    'Congratulations ! Your user is created.')
                t = authenticate(username=user.username,
                                 password=user_form.cleaned_data['password'])
                if t is not None:
                    login(request, t)
            else:
                messages.add_message(request, messages.SUCCESS,
                                     'Your user has been updated.')
            return redirect(reverse('user:profile'))
        except Exception:
            #logger.exception('Error while creating/updating a user. creating=' + str(creating) + ', req=' + "\n".join(request.readlines()))
            messages.add_message(request, messages.ERROR,
                                 'Error, Contact Admin')
    return None
Пример #3
0
def register(request: HttpRequest) -> HttpResponse:
    if settings.REGISTER_ENABLED:
        if request.method == "POST":
            user_profile_form = UserProfileForm(request.POST, request.FILES)
            user_form = UserForm(request.POST)
            response = create_or_update_user(request, user_form,
                                             user_profile_form, True)
            if response is not None:
                return response
        else:
            user_profile_form = UserProfileForm()
            user_form = UserForm()
    else:
        messages.add_message(
            request, messages.ERROR,
            "You cannot register anymore, please contact an admin.")
    return render(request, 'user_profile/register.html', locals())
Пример #4
0
def user_registration(request):
    if request.method == "POST":
        form = UserForm(request.POST)
        if form.is_valid():
            user = form.save()
            mainCycle = MainCycle()
            mainCycle.user = user
            mainCycle.save()
            user = authenticate(request,
                                username=request.POST['username'],
                                password=request.POST['password'])
            login(request, user)
            return redirect('index')

        else:
            return render(request, 'registration.html', {
                'invalid': True,
                'form': form
            })
    else:
        form = UserForm()
        return render(request, 'registration.html', {
            'invalid': False,
            'form': form
        })
Пример #5
0
def profile(request: HttpRequest) -> HttpResponse:
    user = request.user
    if not hasattr(user, 'userprofile'):
        messages.add_message(
            request, messages.ERROR,
            'We tried really hard to find it but this user does not exists.')
        return redirect(reverse('user:login'))
    user_profile = user.userprofile

    if request.method == "POST":
        user_form = UserForm(request.POST, instance=user)
        user_profile_form = UserProfileForm(request.POST,
                                            request.FILES,
                                            instance=user_profile)
        response = create_or_update_user(request, user_form, user_profile_form,
                                         False)
        if response is not None:
            return response
    else:
        user_form = UserForm(instance=user)
        user_profile_form = UserProfileForm(instance=user_profile)

    return render(request, 'user_profile/profile.html', locals())
Пример #6
0
def personal_info(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Your profile was successfully updated!')
            return redirect('profile:personal_info')
        else:
            messages.error(request, 'Please correct the error below.')

    user_form = UserForm()
    profile_form = ProfileForm()
    return render(request, 'user_profile/personal_info.html',
                  {'title': 'Personal info',
                   'user_form': user_form,
                   'profile_form': profile_form})
Пример #7
0
def settings_view(request):
    if request.method == 'POST':
        if 'profile_submit_button' in request.POST:
            return profile_photo_view(request)

        if 'cover_submit_button' in request.POST:
            return cover_photo_view(request)

        user_form = UserForm(request.POST, instance=request.user)
        profile_form = UserDetailsForm(request.POST,
                                       instance=request.user.user_details)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.date_of_birth = profile_form.clean_date()
            profile_form.save(commit=False)
            profile_form.user = request.user
            profile_form.save()
            messages.success(request,
                             ('Your profile was successfully updated!'))
            return redirect(
                reverse('user_profile:user_profile',
                        kwargs={'username': request.user.username}))
        else:
            messages.error(request, ('Please correct the error below.'))
    elif request.method == 'GET':
        user_form = UserForm(instance=request.user)
        profile_form = UserDetailsForm(instance=request.user.user_details)
        profile_photo_form = ProfilePhotoForm(
            instance=request.user.user_details)
        cover_photo_form = CoverPhotoForm(instance=request.user.user_details)

        return render(
            request, 'settings.html', {
                'user_form': user_form,
                'profile_form': profile_form,
                'profile_photo_form': profile_photo_form,
                'cover_photo_form': cover_photo_form,
            })