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 ): email = forms.EmailField() def __init__(self, *args, **kw): if 'instance' in kw.keys(): user = kw['instance'].user initVals = { 'email': user.email } if not 'initial' in kw.keys(): kw['initial'] = initVals else: kw['initial'].update(initVals) super(UserEditForm, self).__init__( *args, **kw ) self.fields.keyOrder.remove('email') self.fields.keyOrder.insert(2, 'email') def save(self): if self.is_valid(): super(UserEditForm,self).save() self.instance.user.email = self.cleaned_data['email'] self.instance.user.save() class Meta: model = Musician exclude = [ 'user','image', 'small_image', 'instrument', 'position', 'public_description' ] #fields = '__all__' def own_profile( request ): return user_edit( request, request.user ) def user_edit( request, username ): musician = get_object_or_404( Musician, user__username=username ) 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() return HttpResponseRedirect('/') # Redirect after POST else: form = UserEditForm( instance= musician ) return render(request, 'musicians/musician_edit.html', { 'form': form, 'musician': musician } ) class MusicianUpdate( UpdateView ): model = Musician #fields = [] template_name = "musicians/musician_edit.html" success_url = '/books/' def addressbook( request ): context = dict() context['musicians'] = Musician.objects.all().order_by('user__first_name') return render( request, 'musicians/addressbook.html', context ) ############################################################################################################ ######################### User Management Views ############################################################ ############################################################################################################ from django.contrib.auth.views import password_change from django.contrib.auth import authenticate, login, logout from django.shortcuts import redirect from django.http import HttpResponse from django.utils import simplejson from django.contrib.auth.models import User from datetime import timedelta def change_password( request ): return password_change(request, "musicians/change_password.html", post_change_redirect= "/" ) def logout_view(request): logout( request ) return redirect( login_view ) def userlistForAutocompletion(request): result = [ u.username for u in User.objects.all() ] return HttpResponse( simplejson.dumps(result), mimetype='application/json' ) def login_view( request ): if request.method == 'POST': # If the form has been submitted... raiseFirstLetter = lambda s: s[:1].upper() + s[1:] if s else '' username = raiseFirstLetter( request.POST['username'] ) password = request.POST['password'] user = authenticate( username=username, password=password ) result = dict() result['err'] = "" if user is not None: if user.is_active: if not request.POST.get('remember', None): # Expire in one year request.session.set_expiry( timedelta( weeks=52 ) ) else: # Expire on browser close request.session.set_expiry( 0 ) login(request, user) result['redirect'] = "/" print ( "Setting Redirect" ) if 'next' in request.POST : result['redirect'] = request.POST["next"] print ( "Using " + request.POST["next"] ) else: result['err'] = "Dein Account wurde deaktiviert." # Return a 'disabled account' error message else: result['err'] = "Falscher Benutzername oder falsches Kennwort." return HttpResponse( simplejson.dumps(result), mimetype='application/json' ) else: # Check if user already logged in if request.user.is_authenticated(): return redirect( "/") if 'next' in request.GET: nextPage = request.GET['next'] else: nextPage = "/" return render( request, 'musicians/login.html', { 'next' : nextPage } )