1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import random |
3
|
|
|
from django.conf import settings |
4
|
|
|
from django.contrib.auth import get_user_model |
5
|
|
|
from django.contrib.sites.models import Site |
6
|
|
|
from django.core.management.base import BaseCommand |
7
|
|
|
from django.db import transaction |
8
|
|
|
from allauth.account.models import EmailAddress |
9
|
|
|
from datetime import date |
10
|
|
|
from faker import Faker |
11
|
|
|
|
12
|
|
|
from wye.profiles.models import UserType, Profile |
13
|
|
|
from wye.organisations.models import Organisation |
14
|
|
|
from wye.regions.models import Location, State |
15
|
|
|
from wye.workshops.models import WorkshopSections, Workshop |
16
|
|
|
from wye.base.constants import WorkshopStatus, WorkshopLevel |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
NUMBER_OF_USERS = getattr(settings, "NUMBER_OF_USERS", 10) |
20
|
|
|
NUMBER_OF_LOCATIONS = getattr(settings, "NUMBER_OF_LOCATIONS", 10) |
21
|
|
|
NUMBER_OF_ORGANISATIONS = getattr(settings, "NUMBER_OF_ORGANISATIONS", 10) |
22
|
|
|
NUMBER_OF_WORKSHOP_SECTIONS = getattr( |
23
|
|
|
settings, "NUMBER_OF_WORKSHOP_SECTIONS", 5) |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class Command(BaseCommand): |
27
|
|
|
help = "Creating Initial demo data for testing application" |
28
|
|
|
fake = Faker() |
29
|
|
|
|
30
|
|
|
@transaction.atomic |
31
|
|
|
def handle(self, *args, **options): |
32
|
|
|
self.fake.seed(4321) |
33
|
|
|
|
34
|
|
|
# Update site url |
35
|
|
|
self.stdout.write(' Updating domain to localhost:8000') |
36
|
|
|
site = Site.objects.get_current() |
37
|
|
|
site.domain, site.name = 'localhost:8000', 'Local' |
38
|
|
|
site.save() |
39
|
|
|
|
40
|
|
|
# User Type |
41
|
|
|
self.stdout.write(' Creating User Types') |
42
|
|
|
self.create_user_type(counter=NUMBER_OF_WORKSHOP_SECTIONS) |
43
|
|
|
|
44
|
|
|
self.stdout.write(' Creating Superuser') |
45
|
|
|
email = '[email protected]' |
46
|
|
|
user = self.create_user(is_superuser=True, username='admin', |
47
|
|
|
email=email, is_active=True, is_staff=True, |
48
|
|
|
first_name='Admin') |
49
|
|
|
|
50
|
|
|
# User |
51
|
|
|
self.stdout.write(' Creating sample users') |
52
|
|
|
for i in range(NUMBER_OF_USERS): |
53
|
|
|
self.create_user() |
54
|
|
|
|
55
|
|
|
# Location |
56
|
|
|
self.stdout.write(' Creating sample locations') |
57
|
|
|
self.create_locations(counter=NUMBER_OF_LOCATIONS) |
58
|
|
|
|
59
|
|
|
# Organization |
60
|
|
|
self.stdout.write(' Creating sample organisations') |
61
|
|
|
self.create_organisations(counter=NUMBER_OF_ORGANISATIONS) |
62
|
|
|
|
63
|
|
|
# Workshop |
64
|
|
|
self.stdout.write(' Creating sample workshop sections') |
65
|
|
|
self.create_workshop_sections() |
66
|
|
|
|
67
|
|
|
# Profile |
68
|
|
|
self.stdout.write(' Creating Profile') |
69
|
|
|
self.create_profile(user) |
70
|
|
|
|
71
|
|
|
# Sample Workshops |
72
|
|
|
self.stdout.write(' Creating Sample Workshop') |
73
|
|
|
self.create_sample_workshops(user) |
74
|
|
|
user_email = EmailAddress.objects.create( |
75
|
|
|
email=user.email, user=user, verified=True) |
76
|
|
|
user_email.save() |
77
|
|
|
|
78
|
|
|
def create_user(self, counter=None, **kwargs): |
79
|
|
|
params = { |
80
|
|
|
"first_name": kwargs.get('first_name', self.fake.first_name()), |
81
|
|
|
"last_name": kwargs.get('last_name', self.fake.last_name()), |
82
|
|
|
"username": kwargs.get('username', self.fake.user_name()), |
83
|
|
|
"email": kwargs.get('email', self.fake.email()), |
84
|
|
|
"is_active": kwargs.get('is_active', self.fake.boolean()), |
85
|
|
|
"is_superuser": kwargs.get('is_superuser', False), |
86
|
|
|
"is_staff": kwargs.get( |
87
|
|
|
'is_staff', |
88
|
|
|
kwargs.get('is_superuser', self.fake.boolean())), |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
user, created = get_user_model().objects.get_or_create(**params) |
92
|
|
|
|
93
|
|
|
if params['is_superuser']: |
94
|
|
|
password = '123123' |
95
|
|
|
user.set_password(password) |
96
|
|
|
user.save() |
97
|
|
|
|
98
|
|
|
self.stdout.write( |
99
|
|
|
"SuperUser created with username: {} and password: {}".format( |
100
|
|
|
params['username'], password) |
101
|
|
|
) |
102
|
|
|
return user |
103
|
|
|
|
104
|
|
|
def create_locations(self, counter=None): |
105
|
|
|
for i in range(counter): |
106
|
|
|
state, updated = State.objects.update_or_create( |
107
|
|
|
name=self.fake.state()) |
108
|
|
|
Location.objects.update_or_create( |
109
|
|
|
name=self.fake.city(), state=state) |
110
|
|
|
|
111
|
|
|
def create_user_type(self, counter=None): |
112
|
|
|
user_type_tuple = [ |
113
|
|
|
('tutor', 'Tutor'), |
114
|
|
|
('lead', 'Regional Lead'), |
115
|
|
|
('poc', 'Organisation Ambassador'), |
116
|
|
|
('admin', 'admin'), |
117
|
|
|
('volunteer', 'Volunteer'), |
118
|
|
|
('student', 'Student')] |
119
|
|
|
for i in user_type_tuple: |
120
|
|
|
obj, updated = UserType.objects.update_or_create( |
121
|
|
|
slug=i[0]) |
122
|
|
|
obj.display_name = i[1] |
123
|
|
|
obj.save() |
124
|
|
|
|
125
|
|
|
def create_organisations(self, counter=None): |
126
|
|
|
users = get_user_model().objects.all() |
127
|
|
|
locations = Location.objects.all() |
128
|
|
|
|
129
|
|
|
for i in range(counter): |
130
|
|
|
number = self.fake.random_digit() |
131
|
|
|
text = self.fake.text() |
132
|
|
|
name = self.fake.company() |
133
|
|
|
org, updated = Organisation.objects.update_or_create( |
134
|
|
|
name=name, |
135
|
|
|
location=locations[number], |
136
|
|
|
organisation_type=number, |
137
|
|
|
organisation_role=text, |
138
|
|
|
description=text, |
139
|
|
|
) |
140
|
|
|
org.user.add(users[number]) |
141
|
|
|
|
142
|
|
|
def create_workshop_sections(self): |
143
|
|
|
sections = ["Python2", "Python3", "Django", "Flask", "Gaming"] |
144
|
|
|
|
145
|
|
|
for section in sections: |
146
|
|
|
self.stdout.write(' Creating %s' % section) |
147
|
|
|
WorkshopSections.objects.create(name=section) |
148
|
|
|
|
149
|
|
|
def create_profile(self, user): |
150
|
|
|
django = WorkshopSections.objects.get(name='Django') |
151
|
|
|
python3 = WorkshopSections.objects.get(name='Python3') |
152
|
|
|
location = Location.objects.all()[0] |
153
|
|
|
user_type = UserType.objects.get(slug='admin') |
154
|
|
|
profile = Profile( |
155
|
|
|
user=user, |
156
|
|
|
mobile='8758885872', |
157
|
|
|
location=location) |
158
|
|
|
profile.usertype.add(user_type) |
159
|
|
|
profile.interested_locations.add(location) |
160
|
|
|
profile.interested_sections.add(django, python3) |
161
|
|
|
profile.save() |
162
|
|
|
return profile |
163
|
|
|
|
164
|
|
|
def create_sample_workshops(self, user): |
165
|
|
|
organisations = Organisation.objects.all() |
166
|
|
|
# locations = Location.objects.all() |
167
|
|
|
sections = WorkshopSections.objects.all() |
168
|
|
|
|
169
|
|
|
for i in range(50): |
170
|
|
|
w = Workshop.objects.create( |
171
|
|
|
no_of_participants=random.randrange(10, 100), |
172
|
|
|
expected_date=date( |
173
|
|
|
2015, random.randrange(1, 12), random.randrange(1, 29)), |
174
|
|
|
description=self.fake.text(), |
175
|
|
|
requester=random.choice(organisations), |
176
|
|
|
workshop_level=WorkshopLevel.BEGINNER, |
177
|
|
|
workshop_section=random.choice(sections), |
178
|
|
|
status=WorkshopStatus.COMPLETED |
179
|
|
|
) |
180
|
|
|
w.presenter.add(user) |
181
|
|
|
w.save() |
182
|
|
|
|