-
Notifications
You must be signed in to change notification settings - Fork 32
/
profile.py
50 lines (40 loc) · 1.54 KB
/
profile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from django.core.urlresolvers import reverse
from django.views.generic.list import ListView
from multi_form_view import MultiModelFormView
from base.forms import PhotoForm, ProfileForm
from base.models import Photo, Profile
class ProfileListView(ListView):
template_name = 'profiles.html'
model = Profile
class ProfileFormView(MultiModelFormView):
form_classes = {
'avatar_form' : PhotoForm,
'background_form' : PhotoForm,
'profile_form' : ProfileForm,
}
record_id = None
template_name = 'profiles_form.html'
def get_form_kwargs(self):
kwargs = super(ProfileFormView, self).get_form_kwargs()
kwargs['avatar_form']['prefix'] = 'avatar'
kwargs['background_form']['prefix'] = 'background'
return kwargs
def get_objects(self):
self.profile_id = self.kwargs.get('profile_id', None)
try:
profile = Profile.objects.get(id=self.profile_id)
except Profile.DoesNotExist:
profile = None
return {
'profile_form': profile,
'avatar_form': profile.avatar if profile else None,
'background_form': profile.background if profile else None,
}
def get_success_url(self):
return reverse('profiles')
def forms_valid(self, forms):
profile = forms['profile_form'].save(commit=False)
profile.avatar = forms['avatar_form'].save()
profile.background = forms['background_form'].save()
profile.save()
return super(ProfileFormView, self).forms_valid(forms)