-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
14 changed files
with
174 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,10 @@ | |
from fleet_management.factories import ( | ||
CarFactory, | ||
DriveFactory, | ||
PassengerFactory, | ||
ProjectFactory, | ||
UserFactory, | ||
) | ||
from ...models import Passenger, Project, User | ||
from ...models import Project, User | ||
|
||
|
||
class Command(BaseCommand): | ||
|
@@ -23,50 +22,59 @@ class Command(BaseCommand): | |
help = "Populates database with fake items." | ||
|
||
def handle(self, *args, **options): | ||
driver_group = Group.objects.get(name=Groups.Driver.name) | ||
passenger_group = Group.objects.get(name=Groups.Passenger.name) | ||
|
||
self.stdout.write(self.style.SUCCESS('Creating 5 cars')) | ||
for _ in tqdm(range(5)): | ||
CarFactory.create() | ||
|
||
self.stdout.write(self.style.SUCCESS('Creating 5 users')) | ||
usernames = [] | ||
self.stdout.write(self.style.SUCCESS('Creating 5 drivers')) | ||
drivers = [] | ||
for _ in tqdm(range(5)): | ||
usernames.append(UserFactory.create().username) | ||
drivers.append(UserFactory.create(groups=[driver_group]).username) | ||
|
||
passengers = [] | ||
self.stdout.write(self.style.SUCCESS('Creating 10 passengers')) | ||
for _ in tqdm(range(10)): | ||
PassengerFactory.create() | ||
passengers.append(UserFactory.create(groups=[passenger_group]).username) | ||
|
||
self.stdout.write(self.style.SUCCESS('Creating 5 projects')) | ||
for _ in tqdm(range(5)): | ||
ProjectFactory.create() | ||
|
||
self.stdout.write(self.style.SUCCESS('Creating 50 drives')) | ||
all_users = list(User.objects.all()) | ||
all_passengers = list(Passenger.objects.all()) | ||
all_drivers = list(User.objects.filter(groups=driver_group)) | ||
all_passengers = list(User.objects.filter(groups=passenger_group)) | ||
all_projects = list(Project.objects.all()) | ||
|
||
for _ in tqdm(range(50)): | ||
DriveFactory.create( | ||
passengers=random.sample(all_passengers, random.randint(1, 4)), | ||
passenger=random.choice(all_passengers), | ||
project=random.choice(all_projects), | ||
driver=random.choice(all_users), | ||
driver=random.choice(all_drivers), | ||
) | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS('Database successfully populated') | ||
) | ||
|
||
self.stdout.write('=' * 50) | ||
self.stdout.write(self.style.SUCCESS('Newly created users:')) | ||
for index, username in enumerate(usernames): | ||
self.stdout.write(self.style.SUCCESS('Newly created drivers:')) | ||
for index, username in enumerate(drivers): | ||
self.stdout.write(self.style.SUCCESS('{index}. {username}'.format( | ||
index=index, | ||
username=username, | ||
))) | ||
|
||
self.stdout.write(self.style.SUCCESS('Newly created passengers:')) | ||
for index, username in enumerate(passengers): | ||
self.stdout.write(self.style.SUCCESS('{index}. {username}'.format( | ||
index=index, | ||
username=username, | ||
))) | ||
|
||
default_user = User.objects.filter(email='[email protected]').first() | ||
|
||
driver_group = Group.objects.get(name=Groups.Driver.name) | ||
driver_group.user_set.add(default_user) | ||
passenger_group = Group.objects.get(name=Groups.Passenger.name) | ||
passenger_group.user_set.add(default_user) |
17 changes: 8 additions & 9 deletions
17
backend/fleet_management/migrations/0011_add_default_groups.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
backend/fleet_management/migrations/0017_auto_20191002_1858.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 2.1.2 on 2019-10-02 18:58 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
import django_countries.fields | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('fleet_management', '0016_auto_20190825_2051'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='drive', | ||
name='passenger', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='drives_taken', to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AlterField( | ||
model_name='drive', | ||
name='driver', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='drives_driven', to=settings.AUTH_USER_MODEL), | ||
), | ||
migrations.AlterField( | ||
model_name='project', | ||
name='country', | ||
field=django_countries.fields.CountryField(default=None, max_length=2), | ||
), | ||
] |
44 changes: 44 additions & 0 deletions
44
backend/fleet_management/migrations/0018_passenger_to_user.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Generated by Django 2.1.2 on 2019-10-02 19:00 | ||
|
||
from django.db import migrations | ||
|
||
|
||
def migrate_passenger_to_user(apps, schema_editor): | ||
Passenger = apps.get_model("fleet_management", "Passenger") | ||
User = apps.get_model("fleet_management", "User") | ||
Group = apps.get_model("auth", "Group") | ||
Drive = apps.get_model("fleet_management", "Drive") | ||
g = Group.objects.get(name="Passenger") | ||
passengers = Passenger.objects.all() | ||
for p in passengers: | ||
u, created = User.objects.get_or_create( | ||
username=p.email, | ||
defaults={ | ||
'email': p.email, | ||
'is_superuser': False, | ||
'is_staff': False, | ||
'country': p.country, | ||
'is_active': True, | ||
'first_name': p.first_name, | ||
'last_name': p.last_name | ||
} | ||
) | ||
u.save() | ||
g.user_set.add(u) | ||
|
||
drives = Drive.objects.filter(passengers=p.id) | ||
for d in drives: | ||
assert d.passengers.all().count() == 1, f"Too many passengers in a drive {d.id}" | ||
d.passenger = u | ||
d.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('fleet_management', '0017_auto_20191002_1858'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(migrate_passenger_to_user), | ||
] |
20 changes: 20 additions & 0 deletions
20
backend/fleet_management/migrations/0019_remove_passenger_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 2.1.2 on 2019-10-02 21:00 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('fleet_management', '0018_passenger_to_user'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='drive', | ||
name='passengers', | ||
), | ||
migrations.DeleteModel( | ||
name='Passenger', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.