Completed
Push — master ( 67dc57...cc006d )
by Vijay
01:38
created

Command.create_organisations()   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 16
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
        self.stdout.write('  Updating domain to localhost:8000')  # Update site url
35
        site = Site.objects.get_current()
36
        site.domain, site.name = 'localhost:8000', 'Local'
37
        site.save()
38
39
        # User Type
40
        self.stdout.write('  Creating User Types')
41
        self.create_user_type(counter=NUMBER_OF_WORKSHOP_SECTIONS)
42
43
        self.stdout.write('  Creating Superuser')
44
        email = '[email protected]'
45
        user = self.create_user(is_superuser=True, username='admin',
46
                                email=email, is_active=True, is_staff=True,
47
                                first_name='Admin')
48
49
        # User
50
        self.stdout.write('  Creating sample users')
51
        for i in range(NUMBER_OF_USERS):
52
            self.create_user()
53
54
        # Location
55
        self.stdout.write('  Creating sample locations')
56
        self.create_locations(counter=NUMBER_OF_LOCATIONS)
57
58
        # Organization
59
        self.stdout.write('  Creating sample organisations')
60
        self.create_organisations(counter=NUMBER_OF_ORGANISATIONS)
61
62
        # Workshop
63
        self.stdout.write('  Creating sample workshop sections')
64
        self.create_workshop_sections()
65
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('is_staff', kwargs.get('is_superuser', self.fake.boolean())),
87
        }
88
89
        user, created = get_user_model().objects.get_or_create(**params)
90
91
        if params['is_superuser']:
92
            password = '123123'
93
            user.set_password(password)
94
            user.save()
95
96
            self.stdout.write("SuperUser created with username: {username} and password: {password}".format(
97
                username=params['username'], password=password)
98
            )
99
        return user
100
101
    def create_locations(self, counter=None):
102
        for i in range(counter):
103
            state, updated = State.objects.update_or_create(
104
                name=self.fake.state())
105
            Location.objects.update_or_create(
106
                name=self.fake.city(), state=state)
107
108
    def create_user_type(self, counter=None):
109
        user_type_tuple = [
110
            ('tutor', 'Tutor'),
111
            ('lead', 'Regional Lead'),
112
            ('poc', 'College POC'),
113
            ('admin', 'admin')]
114
        for i in user_type_tuple:
115
            obj, updated = UserType.objects.update_or_create(
116
                slug=i[0])
117
            obj.display_name = i[1]
118
            obj.save()
119
120
    def create_organisations(self, counter=None):
121
        users = get_user_model().objects.all()
122
        locations = Location.objects.all()
123
124
        for i in range(counter):
125
            number = self.fake.random_digit()
126
            text = self.fake.text()
127
            name = self.fake.company()
128
            org, updated = Organisation.objects.update_or_create(
129
                name=name,
130
                location=locations[number],
131
                organisation_type=number,
132
                organisation_role=text,
133
                description=text,
134
            )
135
            org.user.add(users[number])
136
137
    def create_workshop_sections(self):
138
        sections = ["Python2", "Python3", "Django", "Flask", "Gaming"]
139
140
        for section in sections:
141
            self.stdout.write('  Creating %s' % section)
142
            WorkshopSections.objects.create(name=section)
143
144
    def create_profile(self, user):
145
        django = WorkshopSections.objects.get(name='Django')
146
        python3 = WorkshopSections.objects.get(name='Python3')
147
        location = Location.objects.all()[0]
148
        user_type = UserType.objects.get(slug='admin')
149
        profile = Profile(
150
            user=user,
151
            mobile='8758885872',
152
            location=location)
153
        profile.usertype.add(user_type)
154
        profile.interested_locations.add(location)
155
        profile.interested_sections.add(django, python3)
156
        profile.save()
157
        return profile
158
159
    def create_sample_workshops(self, user):
160
        organisations = Organisation.objects.all()
161
        locations = Location.objects.all()
162
        sections = WorkshopSections.objects.all()
163
164
        for i in range(50):
165
            w = Workshop.objects.create(
166
                no_of_participants=random.randrange(10, 100),
167
                expected_date=date(2015, random.randrange(1, 12), random.randrange(1, 29)),
168
                description=self.fake.text(),
169
                requester=random.choice(organisations),
170
                location=random.choice(locations),
171
                workshop_level=WorkshopLevel.BEGINNER,
172
                workshop_section=random.choice(sections),
173
                status=WorkshopStatus.COMPLETED
174
            )
175
            w.presenter.add(user)
176
            w.save()
177