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