forked from leah/django-elsewhere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
58 lines (45 loc) · 2.03 KB
/
views.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
51
52
53
54
55
56
57
58
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from elsewhere.models import *
@login_required
def example(request):
if request.method == 'POST':
new_data = request.POST.copy()
# Add forms
if new_data.get('sn-form') or new_data.get('im-form') or new_data.get('w-form'):
if new_data.get('sn-form'):
form = SocialNetworkForm(new_data)
elif new_data.get('im-form'):
form = InstantMessengerForm(new_data)
elif new_data.get('w-form'):
form = WebsiteForm(new_data)
if form.is_valid():
profile = form.save(commit=False)
profile.user = request.user
profile.save()
return HttpResponseRedirect(request.path)
else:
## TODO should probably show the errors
print form.errors
# Delete forms
elif new_data.get('delete-sn-form') or new_data.get('delete-im-form') or new_data.get('delete-w-form'):
delete_id = request.POST['delete_id']
if new_data.get('delete-sn-form'):
request.user.social_network_profiles.get(id=delete_id).delete()
elif new_data.get('delete-im-form'):
request.user.instant_messenger_profiles.get(id=delete_id).delete()
elif new_data.get('delete-w-form'):
request.user.website_profiles.get(id=delete_id).delete()
return HttpResponseRedirect(request.path)
# WTF?
else:
return HttpResponseServerError
# Create blank forms
sn_form = SocialNetworkForm()
im_form = InstantMessengerForm()
w_form = WebsiteForm()
return render_to_response('elsewhere/example.html', {
'sn_form': sn_form, 'im_form': im_form, 'w_form': w_form,
}, context_instance=RequestContext(request))