blechreiz-website/musicians/views.py

89 lines
2.3 KiB
Python
Raw Normal View History

2013-05-28 15:20:06 +02:00
from django.views.generic.edit import UpdateView
from django.views.generic import ListView
from musicians.models import Musician
from django import forms
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.shortcuts import get_object_or_404
class MusicianList( ListView):
model = Musician
class UserEditForm( forms.ModelForm ):
2013-05-28 15:20:06 +02:00
email = forms.EmailField()
def __init__(self, *args, **kw):
if 'instance' in kw.keys():
user = kw['instance'].user
2013-06-30 11:01:12 +02:00
initVals = { 'email': user.email }
2013-05-28 15:20:06 +02:00
if not 'initial' in kw.keys():
kw['initial'] = initVals
else:
kw['initial'].update(initVals)
2013-06-30 11:01:12 +02:00
super(UserEditForm, self).__init__( *args, **kw )
2013-05-28 15:20:06 +02:00
self.fields.keyOrder.remove('email')
self.fields.keyOrder.insert(2, 'email')
def save(self):
if self.is_valid():
super(UserEditForm,self).save()
2013-09-29 16:22:42 +02:00
self.instance.user.email = self.cleaned_data['email']
2013-05-28 15:20:06 +02:00
self.instance.user.save()
class Meta:
model = Musician
2013-09-29 16:22:42 +02:00
exclude = [ 'user','image', 'small_image', 'instrument', 'position', 'public_description' ]
2013-05-28 15:20:06 +02:00
#fields = '__all__'
2013-09-29 16:22:42 +02:00
def own_profile( request ):
return user_edit( request, request.user )
2013-05-28 15:20:06 +02:00
def user_edit( request, username ):
2013-06-30 11:01:12 +02:00
musician = get_object_or_404( Musician, user__username=username )
2013-05-28 15:20:06 +02:00
if request.method == 'POST': # If the form has been submitted...
form = UserEditForm(request.POST) # A form bound to the POST data
form.instance = musician
if form.is_valid(): # All validation rules pass
form.save()
2013-09-29 16:22:42 +02:00
return HttpResponseRedirect('/') # Redirect after POST
2013-05-28 15:20:06 +02:00
else:
form = UserEditForm( instance= musician )
2013-09-29 16:22:42 +02:00
return render(request, 'musicians/musician_edit.html', { 'form': form, 'musician': musician } )
2013-05-28 15:20:06 +02:00
class MusicianUpdate( UpdateView ):
model = Musician
#fields = []
template_name = "musicians/musician_edit.html"
success_url = '/books/'
def addressbook( request ):
context = dict()
2013-10-15 21:51:05 +02:00
context['musicians'] = Musician.objects.all().order_by('user__first_name')
return render( request, 'musicians/addressbook.html', context )