예제 #1
0
def edit(request):
    profile = request.user.profile

    def is_selected(prefix):
        if request.method == "POST":
            for name in request.POST.keys():
                if name.startswith(prefix + '-'):
                    return True
            if request.FILES:
                for name in request.FILES.keys():
                    if name.startswith(prefix + '-'):
                        return True
        return False

    if is_selected("profile"):
        had_enabled_stream_emails = profile.enabled_stream_emails
        profile_form = ProfileForm(request,
                                   request.POST,
                                   instance=profile,
                                   prefix="profile")
        if profile_form.is_valid():
            enabled_stream_emails = profile_form.cleaned_data.get(
                "enabled_stream_emails")
            # If is enabling stream emails, set last_stream_email_sent to now
            if not had_enabled_stream_emails and enabled_stream_emails:
                profile.last_stream_email_sent = datetime.datetime.now()
            profile.save()
            return HttpResponseRedirect(reverse("accounts-home"))
    else:
        profile_form = ProfileForm(request, instance=profile, prefix="profile")

    if is_selected("image"):
        image_form = AvatarForm(request.POST, request.FILES, prefix="image")
        if image_form.is_valid():
            if image_form.cleaned_data["remove"]:
                profile.has_avatar = False
                profile.save()
            else:
                handle_uploaded_image(profile, image_form.cleaned_data["file"])
                profile.has_avatar = True
                profile.save()
            return HttpResponseRedirect(reverse("accounts-home"))
    else:
        image_form = AvatarForm(prefix="image")

    has_granted_permissions = AccessToken.objects.filter(
        user=request.user).count()

    tvars = {
        'profile': profile,
        'profile_form': profile_form,
        'image_form': image_form,
        'has_granted_permissions': has_granted_permissions
    }
    return render(request, 'accounts/edit.html', tvars)
예제 #2
0
def edit(request):
    profile = request.user.profile

    def is_selected(prefix):
        if request.method == "POST":
            for name in request.POST.keys():
                if name.startswith(prefix + '-'):
                    return True
            if request.FILES:
                for name in request.FILES.keys():
                    if name.startswith(prefix + '-'):
                        return True
        return False

    if is_selected("profile"):
        had_enabled_stream_emails = profile.enabled_stream_emails
        profile_form = ProfileForm(request, request.POST, instance=profile, prefix="profile")
        if profile_form.is_valid():
            enabled_stream_emails = profile_form.cleaned_data.get("enabled_stream_emails")
            # If is enabling stream emails, set last_stream_email_sent to now
            if not had_enabled_stream_emails and enabled_stream_emails:
                profile.last_stream_email_sent = datetime.datetime.now()
            profile.save()
            return HttpResponseRedirect(reverse("accounts-home"))
    else:
        profile_form = ProfileForm(request, instance=profile, prefix="profile")

    if is_selected("image"):
        image_form = AvatarForm(request.POST, request.FILES, prefix="image")
        if image_form.is_valid():
            if image_form.cleaned_data["remove"]:
                profile.has_avatar = False
                profile.save()
            else:
                handle_uploaded_image(profile, image_form.cleaned_data["file"])
                profile.has_avatar = True
                profile.save()
            return HttpResponseRedirect(reverse("accounts-home"))
    else:
        image_form = AvatarForm(prefix="image")

    has_granted_permissions = AccessToken.objects.filter(user=request.user).count()

    tvars = {
        'profile': profile,
        'profile_form': profile_form,
        'image_form': image_form,
        'has_granted_permissions': has_granted_permissions
    }
    return render(request, 'accounts/edit.html', tvars)
예제 #3
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['user'] = self.request.user
     context['is_owner'] = True
     context['avatar_form'] = AvatarForm()
     context['profile_info_form'] = UserProfileInfoForm(
         data=model_to_dict(self.request.user.profile))
     return context
예제 #4
0
def crop_avatar_view(request):
    if request.method == 'POST':
        form = AvatarForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('photo_list')
    else:
        form = AvatarForm()
예제 #5
0
            def checkUNA():
                """ Handle UNA updates. """

                user_form = UserForm(request.POST, instance=request.user)
                """
                We also need to post the avatar which lives in tUserProfile.
                Create a new instance of AvatarForm.
                Notice the request.FILES.
                Django needs to know we are uploading a file.
                """
                user_profile_form = AvatarForm(
                    request.POST,
                    request.FILES,
                    instance=request.user.tuserprofile)

                if user_form.is_valid() and user_profile_form.is_valid():
                    user_form.save()
                    user_profile_form.save()
                    messages.success(request,
                                     'Your account was updated successfully!')
                else:
                    messages.error(request, user_form.errors)
예제 #6
0
def edit(request):
    profile = request.user.profile

    def is_selected(prefix):
        if request.method == "POST":
            for name in request.POST.keys():
                if name.startswith(prefix + '-'):
                    return True
            if request.FILES:
                for name in request.FILES.keys():
                    if name.startswith(prefix + '-'):
                        return True
        return False

    if is_selected("profile"):
        profile_form = ProfileForm(request, request.POST, instance=profile, prefix="profile")
        if profile_form.is_valid():
            profile_form.save()
            return HttpResponseRedirect(reverse("accounts-home"))
    else:
        profile_form = ProfileForm(request,instance=profile, prefix="profile")

    if is_selected("image"):
        image_form = AvatarForm(request.POST, request.FILES, prefix="image")
        if image_form.is_valid():
            if image_form.cleaned_data["remove"]:
                profile.has_avatar = False
                profile.save()
            else:
                handle_uploaded_image(profile, image_form.cleaned_data["file"])
                profile.has_avatar = True
                profile.save()
            return HttpResponseRedirect(reverse("accounts-home"))
    else:
        image_form = AvatarForm(prefix="image")

    return render_to_response('accounts/edit.html', dict(profile=profile, profile_form=profile_form, image_form=image_form), context_instance=RequestContext(request))
예제 #7
0
def my_account_view(request):
    """ Handle account changes. Check which form was passed by the request
        and update the account accordingly.

        Args:
            request: An HttpRequest to /account/my-account/

        Returns:
            An HttpResponse object with the account form or redirects to
            /account/my-account/ if a change has been made
    """

    if request.user.is_authenticated():

        template = 'accounts/my_account.html'

        # POST request: Validate and save the forms.
        # Then render them with the new values pre-populated.
        if request.method == 'POST':

            which_form = request.POST.get('which_form')

            def checkUNA():
                """ Handle UNA updates. """

                user_form = UserForm(request.POST, instance=request.user)
                """
                We also need to post the avatar which lives in tUserProfile.
                Create a new instance of AvatarForm.
                Notice the request.FILES.
                Django needs to know we are uploading a file.
                """
                user_profile_form = AvatarForm(
                    request.POST,
                    request.FILES,
                    instance=request.user.tuserprofile)

                if user_form.is_valid() and user_profile_form.is_valid():
                    user_form.save()
                    user_profile_form.save()
                    messages.success(request,
                                     'Your account was updated successfully!')
                else:
                    messages.error(request, user_form.errors)

            def checkEmail():
                """ Handle Email updates. """

                user_email_form = UserEmailForm(request.POST,
                                                instance=request.user)

                if user_email_form.is_valid():
                    user_email_form.save()
                    messages.success(request,
                                     'Your email was updated successfully!')
                else:
                    messages.error(request, user_email_form.errors)

            def checkSubscr():
                """ Handle Subscription changes. """

                # Create a new instance of the SubscrForm form
                subscr_form = SubscrForm(request.POST,
                                         instance=request.user.tuserprofile)

                if subscr_form.is_valid():
                    subscr_form.save()
                    messages.success(request,
                                     'Your account was updated successfully!')
                else:
                    messages.error(request, subscr_form.errors)

            def checkPassword():
                """ Handle Password changes. """

                # PasswordChangeForm does not inherit from ModelForm
                password_form = PasswordChangeForm(user=request.user,
                                                   data=request.POST)

                if password_form.is_valid():
                    user = password_form.save()
                    # The user should stay logged in after changing password.
                    update_session_auth_hash(request, user)
                    messages.success(
                        request, 'Your password was updated successfully!')
                else:
                    messages.error(request, password_form.errors)

            def checkClosure():
                """ Handle account closure. """

                account_closure_form = AccountClosureForm(
                    request.POST, instance=request.user)

                if account_closure_form.is_valid():
                    account_closure_form.save()

            # Create a dictionary that maps the form name with a function
            options = {
                'UNAForm': checkUNA,
                'EmailForm': checkEmail,
                'SubscrForm': checkSubscr,
                'PasswordForm': checkPassword,
                'AccountClosureForm': checkClosure
            }

            options[which_form]()

            return redirect('/account/my-account/')

        # GET request: Render the forms, pre-populated with the existed values.
        else:
            user_form = UserForm(instance=request.user)
            user_profile_form = AvatarForm(instance=request.user.tuserprofile)
            user_email_form = UserEmailForm(instance=request.user)
            subscr_form = SubscrForm(instance=request.user.tuserprofile)
            password_form = PasswordChangeForm(user=request.user)
            account_closure_form = AccountClosureForm(instance=request.user)

        return render(
            request, template, {
                'user_form': user_form,
                'user_profile_form': user_profile_form,
                'user_email_form': user_email_form,
                'subscr_form': subscr_form,
                'password_form': password_form,
                'account_closure_form': account_closure_form
            })
    # If the user is not authenticated redirect him/her to the home page
    else:
        return redirect('/home/')