2 |
2 |
3 from django.contrib.auth.models import User |
3 from django.contrib.auth.models import User |
4 |
4 |
5 from django.db import IntegrityError |
5 from django.db import IntegrityError |
6 |
6 |
7 from workshop.reg.models import Event |
7 from workshop.reg.models import Profile, Event |
8 from workshop.reg.utils import gen_key |
8 from workshop.reg.utils import gen_key |
9 |
9 |
10 def create_user(email, password, firstname="", lastname=""): |
10 def create_user(email, password, firstname="", lastname="", gender="M", profession="S", affiliated_to="", interests="" ): |
11 """ create a user with random username and set the password. |
11 """ create a user with random username and set the password. |
12 """ |
12 """ |
13 |
13 |
14 username = ('%s %s'%(firstname, lastname)).title().strip() |
14 while True: |
|
15 try: |
|
16 username = gen_key(8) |
|
17 user = User.objects.get(username=username) |
|
18 except User.DoesNotExist: |
|
19 break |
15 new_user = User.objects.create_user(username, email, password) |
20 new_user = User.objects.create_user(username, email, password) |
16 new_user.firstname = firstname |
21 new_user.firstname = firstname |
17 new_user.lastname = lastname |
22 new_user.lastname = lastname |
18 new_user.is_active = False |
23 new_user.is_active = False |
19 new_user.save() |
24 new_user.save() |
20 return new_user |
25 |
|
26 new_profile = Profile(user=new_user) |
|
27 new_profile.gender = gender |
|
28 new_profile.profession = profession |
|
29 new_profile.affiliated_to = affiliated_to |
|
30 new_profile.interests = interests |
|
31 |
|
32 while True: |
|
33 try: |
|
34 new_profile.activation_key = gen_key(30) |
|
35 new_profile.save() |
|
36 return new_user |
|
37 except IntegrityError: |
|
38 pass |
21 |
39 |
22 def create_event(title, desc, start_date, stop_date, created_by=None): |
40 def create_event(title, desc, start_date, stop_date, created_by=None): |
23 """ make an event and save it. |
41 """ make an event and save it. |
24 """ |
42 """ |
25 |
43 |