Skip to content

Commit

Permalink
(#15, #23) Enabling email and password changes through profile
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDerb committed Sep 15, 2014
1 parent 4d16971 commit fbb9735
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions kegstarter/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
url(r'^accounts/(?P<user_id>\d+)/payments/delete', 'main.payments.delete', name='payment_delete'),
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
(r'^accounts/logout/$', 'django.contrib.auth.views.logout'),
url(r'', include('django.contrib.auth.urls')),
url(r'^accounts/register/$', 'main.views.register', name='register'),

url(r'^admin/', include(admin.site.urls)),
Expand Down
7 changes: 7 additions & 0 deletions main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def save(self, commit=True):
user.save()
return user

class UserInfoForm(ModelForm):
email = EmailField(required=True)

class Meta:
model = User
fields = ('email',)


class DonationForm(ModelForm):
user = ModelChoiceField(queryset=User.objects.order_by('username'))
Expand Down
35 changes: 29 additions & 6 deletions main/templates/main/profile.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load markdown_filter %}

{% block title %}{{ requested_user }} | Kegstarter{% endblock %}
Expand All @@ -9,7 +10,7 @@ <h2>{{ requested_user }}</h2>

<div class="row">
<div class="col-md-12">
<h2>Payment Options</h2>
<h3>Payment Options</h3>
{% if payment_options|length > 0 %}
<ul class="list-group">
{% for option in payment_options %}
Expand All @@ -31,13 +32,35 @@ <h2>Payment Options</h2>
<p>Looks like {% if same_user %}you{% else %}they{% endif %} haven't set up any payments yet!</p>
{% endif %}
{% if same_user %}
<form action="" method="post" role="form" class="form-horizontal">
{% csrf_token %}
{{ payment_options_form.as_p }}
<input type="submit" value="Add Payment Option" />
</form>
<div class="well">
<form action="" method="post" role="form" class="form-horizontal">
{% csrf_token %}
{{ payment_options_form | crispy }}
<p>* Required field</p>
<input type="submit" name="add_payment" class="btn btn-primary" value="Add Payment Option" />
</form>
</div>
{% endif %}
</div>
</div>

{% if same_user %}
<div class="row">
<div class="col-md-12">
<h3>Profile</h3>
<h4>Info</h4>
<div class="well">
<form action="" method="post" role="form" class="form-horizontal">
{% csrf_token %}
{{ user_form | crispy }}
<p>* Required field</p>
<input type="submit" name="change_info" class="btn btn-primary" value="Add Payment Option" />
</form>
</div>
<h4>Change Password</h4>
<a href="{% url 'password_change' %}" class="btn">Change your password</a>
</div>
</div>
{% endif %}

{% endblock %}
35 changes: 24 additions & 11 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User
from django.contrib.auth.forms import PasswordChangeForm
from django.db.models import Sum, Max, Q
from django.http import HttpResponseRedirect, HttpResponseBadRequest
from django.shortcuts import render
Expand All @@ -13,7 +14,7 @@

from .forms import DonationForm, VoteForm, PurchaseForm, KegForm, \
PurchasePriceForm, AddPaymentOptionForm, PurchaseChangeForm, \
UserCreationWithEmailForm
UserCreationWithEmailForm, UserInfoForm
from .models import Brewery, Donation, Purchase, KegMaster, PaymentOption, Suggestion, Vote
from .shared import sum_queryset_field, get_user_balance

Expand Down Expand Up @@ -173,21 +174,33 @@ def profile(request, user_id):
'requested_user': requested_user
}

# Only display form is it's same user
# Only display form if it's same user
if requested_user == request.user:
user_form = UserInfoForm(instance=request.user)
form = AddPaymentOptionForm()

context['same_user'] = True
if request.method == 'POST':
form = AddPaymentOptionForm(request.POST)

if form.is_valid():
payment_option = form.save(commit=False)
payment_option.user = request.user
payment_option.save()
return HttpResponseRedirect(reverse('profile', args={user_id}))
else:
form = AddPaymentOptionForm()
if 'add_payment' in request.POST:
form = AddPaymentOptionForm(request.POST)

if form.is_valid():
payment_option = form.save(commit=False)
payment_option.user = request.user
payment_option.save()
messages.info(request, "Payment option added.")
return HttpResponseRedirect(reverse('profile', args={user_id}))

if 'change_info' in request.POST:
user_form = UserInfoForm(request.POST, instance=request.user)
if user_form.is_valid():
user_form.save()
messages.info(request, "Profile info successfully updated!")
return HttpResponseRedirect(reverse('profile', args={user_id}))

context['payment_options_form'] = form
context['user_form'] = user_form
context['change_password_form'] = PasswordChangeForm(request.user)

return render(request, 'main/profile.html', context)

Expand Down

0 comments on commit fbb9735

Please sign in to comment.