render_to community add_institution html def add_institution request i

 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
59
60
61
62
63
64
@render_to('community/add_institution.html')
def add_institution(request, id, city, institution):
"""
Registration and adding record. If user isnt authorized
it will show regisration fields, if he does, only institution
fields showed.
"""
city = City.objects.get(slug=city)
institution = get_object_or_404(Institution, city=city, slug=institution)
if request.user.is_authenticated():
if not request.user.is_active:
return HttpResponseRedirect("/accounts/activation/")
if request.method == 'POST':
form = RecordForm(request.POST)
if form.is_valid():
start_year = request.POST.get('start_year')
end_year = request.POST.get('end_year')
record = Record(user=request.user, institution=institution, start_year=start_year, end_year=end_year)
record.save()
record.add_to_counter()
flash.notice_next(u"Community successfully added")
return HttpResponseRedirect("/accounts/profile/")
else:
form = RecordForm(request.POST)
return {'institution': institution, 'form': form}
initial_data = {'slug': institution.slug}
form = RecordForm(initial=initial_data)
return {'institution': institution, 'form': form}
else:
if request.method == 'POST':
form = ProfileRegistration(request.POST)
if form.is_valid():
email = request.POST['email']
first_name = request.POST['first_name']
last_name = request.POST['last_name']
country = Country.objects.get(pk=request.POST['country'])
city = request.POST['city']
old_last_name = request.POST['old_last_name']
gender = request.POST['gender']
password = request.POST['password']
start_year = request.POST['start_year']
end_year = request.POST['end_year']
birthday = datetime.datetime(int(request.POST['year']), int(request.POST['month']), int(request.POST['day']))
user = User(username=email.lower(), email=email, password=password, first_name=first_name, last_name=last_name, is_active=False)
user.set_password(password)
user.save()
hash = gen_md5()
activation = Activation(user=user, hash=hash)
activation.save()
profile = Profile(user=user, country=country, city=city, gender=gender, old_last_name=old_last_name, birthday=birthday)
profile.save()
record = Record(user=user, institution=institution, start_year=start_year, end_year=end_year)
record.save()
record.add_to_counter()
message = "You have been successfully registered on http://%s site\nThis is your activation code http://%s/accounts/activation/%s" % (request.META['HTTP_HOST'], request.META['HTTP_HOST'], hash)
send_mail("Activation", message, 'admin@test.com', [email], fail_silently=True)
flash.notice_next(u"Community successfully added")
return HttpResponseRedirect("/accounts/activation/")
else:
form = ProfileRegistration(request.POST)
return {'institution': institution, 'form': form}
initial_data = {'slug': institution.slug}
form = ProfileRegistration(initial=initial_data)
return {'institution': institution, 'form': form}