Completed
Push — master ( 0c1e88...4cd38f )
by Vijay
06:42 queued 02:42
created

Command.create_locations()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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', 'College POC'),
116
            ('admin', 'admin')]
117
        for i in user_type_tuple:
118
            obj, updated = UserType.objects.update_or_create(
119
                slug=i[0])
120
            obj.display_name = i[1]
121
            obj.save()
122
123
    def create_organisations(self, counter=None):
124
        users = get_user_model().objects.all()
125
        locations = Location.objects.all()
126
127
        for i in range(counter):
128
            number = self.fake.random_digit()
129
            text = self.fake.text()
130
            name = self.fake.company()
131
            org, updated = Organisation.objects.update_or_create(
132
                name=name,
133
                location=locations[number],
134
                organisation_type=number,
135
                organisation_role=text,
136
                description=text,
137
            )
138
            org.user.add(users[number])
139
140
    def create_workshop_sections(self):
141
        sections = ["Python2", "Python3", "Django", "Flask", "Gaming"]
142
143
        for section in sections:
144
            self.stdout.write('  Creating %s' % section)
145
            WorkshopSections.objects.create(name=section)
146
147
    def create_profile(self, user):
148
        django = WorkshopSections.objects.get(name='Django')
149
        python3 = WorkshopSections.objects.get(name='Python3')
150
        location = Location.objects.all()[0]
151
        user_type = UserType.objects.get(slug='admin')
152
        profile = Profile(
153
            user=user,
154
            mobile='8758885872',
155
            location=location)
156
        profile.usertype.add(user_type)
157
        profile.interested_locations.add(location)
158
        profile.interested_sections.add(django, python3)
159
        profile.save()
160
        return profile
161
162
    def create_sample_workshops(self, user):
163
        organisations = Organisation.objects.all()
164
        locations = Location.objects.all()
165
        sections = WorkshopSections.objects.all()
166
167
        for i in range(50):
168
            w = Workshop.objects.create(
169
                no_of_participants=random.randrange(10, 100),
170
                expected_date=date(
171
                    2015, random.randrange(1, 12), random.randrange(1, 29)),
172
                description=self.fake.text(),
173
                requester=random.choice(organisations),
174
                location=random.choice(locations),
175
                workshop_level=WorkshopLevel.BEGINNER,
176
                workshop_section=random.choice(sections),
177
                status=WorkshopStatus.COMPLETED
178
            )
179
            w.presenter.add(user)
180
            w.save()
181