Allow editing your user profile. Limited ability at the moment

This commit is contained in:
Michael Hall 2018-02-01 23:26:11 -05:00
parent 51b9530a04
commit 3826e49168
8 changed files with 92 additions and 4 deletions

View file

@ -3,7 +3,8 @@ from django import forms
from django.forms.widgets import TextInput, Media
from django.utils.translation import ugettext_lazy as _
from .models.profiles import Team
from django.contrib.auth.models import User
from .models.profiles import Team, UserProfile
from .models.events import Event, Place
from datetime import time
@ -207,3 +208,13 @@ class NewPlaceForm(forms.ModelForm):
super().__init__(*args, **kwargs)
self.fields['city'].required = True
class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ['email']
class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ['realname', 'avatar']

View file

@ -15,7 +15,7 @@ class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
realname = models.CharField(_("Real Name"), max_length=150, blank=True)
tz = models.CharField(max_length=32, verbose_name=_('Timezone'), default='UTC', choices=[(tz, tz) for tz in pytz.all_timezones], blank=False, null=False, help_text=_('The most commonly used timezone for this User.'))
avatar = models.URLField(verbose_name=_("Photo"), max_length=150, blank=True, null=True)
avatar = models.URLField(verbose_name=_("Photo Image"), max_length=150, blank=True, null=True)
web_url = models.URLField(verbose_name=_('Website URL'), blank=True, null=True)
twitter = models.CharField(verbose_name=_('Twitter Name'), max_length=32, blank=True, null=True)

View file

@ -0,0 +1,4 @@
<table>
{{ user_form }}
{{ profile_form }}
</table>

View file

@ -68,7 +68,7 @@ form {
Welcome {{ request.user.username }}
</a>
<div class="dropdown-menu" aria-labelledby="navbarUserMenuLink">
<a class="dropdown-item disabled" href="#">Profile</a>
<a class="dropdown-item" href="{% url 'edit-profile' %}">Profile</a>
<a class="dropdown-item" href="{% url 'logout' %}">Logout</a>
</div>
</li>

View file

@ -0,0 +1,13 @@
{% extends "get_together/base.html" %}
{% block content %}
<h2>Profile: {{request.user}}</h2>
<form action="{% url "edit-profile" %}" method="post">
{% csrf_token %}
{% include "events/profile_form.html" %}
<br />
<button type="submit" class="btn btn-primary">Save</button>
</form>
{% endblock %}

View file

@ -33,6 +33,8 @@ urlpatterns = [
path('api/spr/', event_views.spr_list),
path('api/cities/', event_views.city_list),
path('profile/+edit', views.edit_profile, name='edit-profile'),
path('events/', views.events_list, name='events'),
path('+create-team/', views.create_team, name='create-team'),
path('teams/', views.teams_list, name='teams'),

View file

@ -0,0 +1,24 @@
from django.utils.translation import ugettext_lazy as _
from django.contrib import messages
from django.contrib.auth import logout as logout_user
from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from events.models.profiles import Team, UserProfile, Member
from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm
from events.models.events import Event, Place, Attendee
import datetime
import simplejson
def new_user_confirm_profile(request):
pass
def new_user_find_teams(request):
pass
def new_user_find_events(request):
pass

View file

@ -6,7 +6,7 @@ from django.shortcuts import render, redirect
from django.http import HttpResponse, JsonResponse
from events.models.profiles import Team, UserProfile, Member
from events.forms import TeamForm, NewTeamForm, TeamEventForm, NewTeamEventForm, NewPlaceForm
from events.forms import UserForm, UserProfileForm
from events.models.events import Event, Place, Attendee
@ -15,3 +15,37 @@ import simplejson
from .teams import *
from .events import *
def edit_profile(request):
if not request.user.is_authenticated:
messages.add_message(request, messages.WARNING, message=_('You must be logged in to edit your profile.'))
return redirect('login')
user = request.user
profile = request.user.profile
if request.method == 'GET':
user_form = UserForm(instance=user)
profile_form = UserProfileForm(instance=profile)
context = {
'user_form': user_form,
'profile_form': profile_form,
}
return render(request, 'get_together/users/edit_profile.html', context)
elif request.method == 'POST':
user_form = UserForm(request.POST, instance=user)
profile_form = UserProfileForm(request.POST, instance=profile)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
profile = profile_form.save()
return redirect('home')
else:
context = {
'user_form': user_form,
'profile_form': profile_form,
}
return render(request, 'get_together/users/edit_profile.html', context)
else:
return redirect('home')