Beispiel #1
0
 def get_context_data(self, **kwargs):
     """Returns context for the template"""
     context = super(ProfileSettings, self).get_context_data(**kwargs)
     user_profile = self.request.user.profile
     email_initial = {'email': self.request.user.email}
     profile_form = ProfileSettingsForm(instance=user_profile)
     email_form = EmailSettingsForm(
         initial=email_initial,
         instance=user_profile,
     )
     org_form = OrgPreferencesForm(instance=user_profile)
     # these move to squarelet in the future
     donations = RecurringDonation.objects.filter(user=self.request.user)
     crowdfunds = RecurringCrowdfundPayment.objects.filter(
         user=self.request.user
     )
     context.update({
         'squarelet_url': settings.SQUARELET_URL,
         'profile_form': profile_form,
         'email_form': email_form,
         'org_form': org_form,
         'donations': donations,
         'crowdfunds': crowdfunds,
     })
     return context
Beispiel #2
0
 def get_context_data(self, **kwargs):
     """Returns context for the template"""
     context = super(ProfileSettings, self).get_context_data(**kwargs)
     user_profile = self.request.user.profile
     profile_initial = {
         'first_name': self.request.user.first_name,
         'last_name': self.request.user.last_name,
     }
     email_initial = {'email': self.request.user.email}
     receipt_initial = {
         'emails':
         '\n'.join(r.email for r in self.request.user.receipt_emails.all())
     }
     profile_form = ProfileSettingsForm(
         initial=profile_initial,
         instance=user_profile,
     )
     email_form = EmailSettingsForm(
         initial=email_initial,
         instance=user_profile,
     )
     org_form = OrgPreferencesForm(instance=user_profile)
     receipt_form = (kwargs.get('receipt_form')
                     or ReceiptForm(initial=receipt_initial))
     current_plan = dict(ACCT_TYPES)[user_profile.acct_type]
     donations = RecurringDonation.objects.filter(user=self.request.user)
     crowdfunds = RecurringCrowdfundPayment.objects.filter(
         user=self.request.user)
     context.update({
         'stripe_pk': settings.STRIPE_PUB_KEY,
         'profile_form': profile_form,
         'email_form': email_form,
         'receipt_form': receipt_form,
         'org_form': org_form,
         'current_plan': current_plan,
         'credit_card': user_profile.card(),
         'donations': donations,
         'crowdfunds': crowdfunds,
     })
     return context
Beispiel #3
0
def profile_settings(request):
    """Update a users information"""
    # pylint: disable=too-many-locals
    user_profile = request.user.profile
    settings_forms = {
        'profile': ProfileSettingsForm,
        'email': EmailSettingsForm,
        'billing': BillingPreferencesForm,
        'org': OrgPreferencesForm,
    }
    receipt_form = None
    if request.method == 'POST':
        action = request.POST.get('action')
        if action == 'receipt':
            receipt_form = ReceiptForm(request.POST)
            if receipt_form.is_valid():
                new_emails = receipt_form.cleaned_data['emails'].split('\n')
                new_emails = {e.strip() for e in new_emails}
                old_emails = {
                    r.email
                    for r in request.user.receipt_emails.all()
                }
                ReceiptEmail.objects.filter(
                    user=request.user,
                    email__in=(old_emails - new_emails),
                ).delete()
                ReceiptEmail.objects.bulk_create([
                    ReceiptEmail(user=request.user, email=e)
                    for e in new_emails - old_emails
                ])
                messages.success(request, 'Your settings have been updated.')
                return redirect('acct-settings')
        elif action:
            form = settings_forms[action]
            form = form(request.POST, request.FILES, instance=user_profile)
            if form.is_valid():
                form.save()
                messages.success(request, 'Your settings have been updated.')
                return redirect('acct-settings')
    profile_initial = {
        'first_name': request.user.first_name,
        'last_name': request.user.last_name,
    }
    email_initial = {'email': request.user.email}
    receipt_initial = {
        'emails': '\n'.join(r.email for r in request.user.receipt_emails.all())
    }
    profile_form = ProfileSettingsForm(initial=profile_initial,
                                       instance=user_profile)
    email_form = EmailSettingsForm(initial=email_initial,
                                   instance=user_profile)
    org_form = OrgPreferencesForm(instance=user_profile)
    receipt_form = receipt_form or ReceiptForm(initial=receipt_initial)
    current_plan = dict(ACCT_TYPES)[user_profile.acct_type]
    context = {
        'stripe_pk': settings.STRIPE_PUB_KEY,
        'profile_form': profile_form,
        'email_form': email_form,
        'receipt_form': receipt_form,
        'org_form': org_form,
        'current_plan': current_plan,
        'credit_card': user_profile.card()
    }
    return render_to_response('accounts/settings.html',
                              context,
                              context_instance=RequestContext(request))