Skip to content

Commit

Permalink
Begin semantic versioning, enhance commands for data generation
Browse files Browse the repository at this point in the history
  • Loading branch information
leethobbit committed Nov 21, 2024
1 parent 5a08a8c commit 9a5dd68
Show file tree
Hide file tree
Showing 20 changed files with 159 additions and 323 deletions.
2 changes: 1 addition & 1 deletion dragonroost/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.0"
__version__ = "0.2.0"
__version_info__ = tuple(
int(num) if num.isdigit() else num
for num in __version__.replace("-", ".", 1).split(".")
Expand Down
88 changes: 88 additions & 0 deletions dragonroost/animals/management/commands/add_animals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import random
import secrets
from os import listdir
from pathlib import Path

from django.conf import settings
from django.core.management.base import BaseCommand
from faker import Faker
from measurement.measures import Weight

from dragonroost.animals.models import Animal
from dragonroost.animals.models import MedicalRecord
from dragonroost.animals.models import Species
from dragonroost.business.models import Location
from dragonroost.people.models import Person

fake = Faker()

MEDIA_ROOT = settings.MEDIA_ROOT


def random_image():
dir_path = Path(MEDIA_ROOT) / "images/"
files = [
content for content in listdir(dir_path) if Path(dir_path / content).is_file()
]
image = random.choice(files) # noqa: S311
return "images/" + str(image)


class Command(BaseCommand):
help = "Create test data for Dragonroost."

def add_arguments(self, parser):
parser.add_argument(
"-c",
"--count",
type=int,
help="Number of animals to create.",
)

def handle(self, *args, **options):
if options["count"]:
self.create_animals(options["count"])
self.stdout.write(self.style.SUCCESS("Test data created successfully!"))

def create_animals(self, count):
species = list(Species.objects.all())
locations = list(Location.objects.all())
people = list(Person.objects.all())
names = [fake.unique.first_name() for i in range(200)]

statuses = [
"ADOPTED",
"AMBASSADOR",
"AVAILABLE",
"DECEASED",
"FOSTERED",
"MEDICAL_HOLD",
"ON_HOLD",
"QUARANTINE",
]

for i in range(count):
animal = Animal.objects.create(
name=names[i],
description=fake.text(max_nb_chars=50),
species=secrets.choice(species),
location=secrets.choice(locations),
color=fake.color(),
donation_fee=random.uniform(5.00, 350.00), # noqa: S311
age=random.uniform(1, 50), # noqa: S311
starting_weight=Weight(lb=random.randint(5, 35)), # noqa: S311
status=secrets.choice(statuses),
animal_photo=random_image(),
)
animal.save()
record_count = secrets.randbelow(6)
for _i in range(record_count):
medical_record = MedicalRecord.objects.create(
animal=animal,
notes=fake.text(max_nb_chars=50),
treatments=fake.text(max_nb_chars=75),
current_weight=Weight(lb=random.randint(7, 40)), # noqa: S311
bowel_movement=fake.boolean(chance_of_getting_true=50),
q_volunteer=secrets.choice(people),
)
medical_record.save()
99 changes: 40 additions & 59 deletions dragonroost/animals/management/commands/add_initial_data.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
import logging
import random
import secrets

from django.core.management.base import BaseCommand
from faker import Faker
from measurement.measures import Weight

from dragonroost.animals.models import Animal
from dragonroost.animals.models import MedicalRecord
from dragonroost.animals.models import Species
from dragonroost.business.models import Location
from dragonroost.people.models import Person
from dragonroost.people.models import Role

fake = Faker()
logger = logging.getLogger(__name__)


class Command(BaseCommand):
help = "Create test data for Dragonroost."

def add_arguments(self, parser):
parser.add_argument(
"--initial",
help="Create initial data before animals can be added.",
)
parser.add_argument("--animals", type=int, help="Number of animals to create.")

def handle(self, *args, **options):
if options["initial"]:
"""
Create all initial data EXCEPT animals.
"""
self.create_species()
self.create_locations()
if options["animals"]:
self.create_animals(options["animals"])
self.create_species()
self.create_locations()
self.create_roles()
self.create_people()
self.stdout.write(self.style.SUCCESS("Test data created successfully!"))

def create_species():
def create_species(self):
if Species.objects.all().count() > 0:
return

species_names = [
"Corn Snake",
"Red-eared Slider",
"Ball Python",
"Bearded Dragon",
"Reticulated Python",
"Russian Tortoise",
]

for name in species_names:
Expand All @@ -50,7 +43,10 @@ def create_species():
is_ohio_native=fake.boolean(chance_of_getting_true=25),
)

def create_locations():
def create_locations(self):
if Location.objects.all().count() > 0:
return

location_names = ["Sales Floor", "Q Room", "Upstairs"]

for name in location_names:
Expand All @@ -59,44 +55,29 @@ def create_locations():
description=fake.text(max_nb_chars=20),
)

def create_animals(self, count):
species = list(Species.objects.all())
locations = list(Location.objects.all())
people = list(Person.objects.all())
names = [fake.unique.first_name() for i in range(200)]

statuses = [
"ADOPTED",
"AMBASSADOR",
"AVAILABLE",
"DECEASED",
"FOSTERED",
"MEDICAL_HOLD",
"ON_HOLD",
"QUARANTINE",
]
def create_roles(self):
if Role.objects.all().count() > 0:
return

roles = ["Volunteer", "Donor", "Foster", "Adopter"]

for i in range(count):
animal = Animal.objects.create(
name=names[i],
for role in roles:
Role.objects.get_or_create(
name=role,
description=fake.text(max_nb_chars=50),
species=secrets.choice(species),
location=secrets.choice(locations),
color=fake.color(),
donation_fee=random.uniform(5.00, 350.00), # noqa: S311
age=random.uniform(1, 50), # noqa: S311
starting_weight=Weight(lb=random.randint(5, 35)), # noqa: S311
status=secrets.choice(statuses),
)
animal.save()
record_count = secrets.randbelow(6)
for _i in range(record_count):
medical_record = MedicalRecord.objects.create(
animal=animal,
notes=fake.text(max_nb_chars=50),
treatments=fake.text(max_nb_chars=75),
current_weight=Weight(lb=random.randint(7, 40)), # noqa: S311
bowel_movement=fake.boolean(chance_of_getting_true=50),
q_volunteer=secrets.choice(people),
)
medical_record.save()

def create_people(self):
person_count = 15
roles = list(Role.objects.all())

for _ in range(person_count):
person = Person.objects.create(
first_name=fake.first_name(),
last_name=fake.last_name(),
notes=fake.text(max_nb_chars=50),
)
person.roles.set(
random.sample(roles, k=random.randrange(1, len(roles))), # noqa: S311
)
person.save()
13 changes: 9 additions & 4 deletions dragonroost/animals/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Generated by Django 5.0.9 on 2024-10-22 18:29
# Generated by Django 5.0.9 on 2024-11-21 19:38

import django.db.models.deletion
import django_measurement.models
import measurement.measures.mass
from django.db import migrations, models


Expand Down Expand Up @@ -43,8 +45,8 @@ class Migration(migrations.Migration):
('color', models.CharField(default='None', max_length=80)),
('sex', models.CharField(choices=[('MALE', 'Male'), ('FEMALE', 'Female'), ('UNKNOWN', 'Unknown')], default='UNKNOWN', max_length=20)),
('age', models.IntegerField(default=0)),
('starting_weight', models.IntegerField(default=0)),
('status', models.CharField(choices=[('PENDING', 'Pending'), ('ADOPTABLE', 'Adoptable'), ('QUARANTINED', 'Quarantined'), ('FOSTERED', 'Fostered'), ('MEDICAL_HOLD', 'Medical Hold'), ('ADOPTED', 'Adopted'), ('DECEASED', 'Deceased'), ('AMBASSADOR', 'Ambassador')], default='ADOPTABLE', max_length=80)),
('starting_weight', django_measurement.models.MeasurementField(measurement=measurement.measures.mass.Mass)),
('status', models.CharField(choices=[('ADOPTED', 'Adopted'), ('AMBASSADOR', 'Ambassador'), ('AVAILABLE', 'Available'), ('DECEASED', 'Deceased'), ('FOSTERED', 'Fostered'), ('MEDICAL_HOLD', 'Medical Hold'), ('ON_HOLD', 'On Hold'), ('QUARANTINE', 'Quarantine')], default='AVAILABLE', max_length=80)),
('location', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='animals', to='business.location')),
('species', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='animals', to='animals.species')),
],
Expand All @@ -58,14 +60,17 @@ class Migration(migrations.Migration):
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('notes', models.TextField(blank=True, default='', max_length=500)),
('current_weight', models.IntegerField(default=0)),
('current_weight', django_measurement.models.MeasurementField(measurement=measurement.measures.mass.Mass)),
('bowel_movement', models.BooleanField(default=True)),
('problem_list', models.TextField(blank=True, default='', max_length=500)),
('findings', models.TextField(blank=True, default='', max_length=500)),
('treatments', models.TextField(blank=True, default='', max_length=500)),
('animal', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='medical_records', to='animals.animal')),
('q_volunteer', models.ForeignKey(default=1, on_delete=django.db.models.deletion.SET_DEFAULT, to='people.person')),
],
options={
'db_table_comment': 'Table holds medical record entries.',
'ordering': ['created'],
},
),
]

This file was deleted.

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions dragonroost/animals/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import logging
import random
from datetime import timezone
from os import listdir
from pathlib import Path

from django.conf import settings
from django.db import models
from django.urls import reverse
from django_measurement.models import MeasurementField
Expand All @@ -13,20 +9,9 @@
from dragonroost.business.models import Location
from dragonroost.people.models import Person

BASE_DIR = settings.MEDIA_ROOT

logger = logging.getLogger(__name__)


def random_image():
dir_path = Path(BASE_DIR) / "images/"
files = [
content for content in listdir(dir_path) if Path(dir_path / content).is_file()
]
image = random.choice(files) # noqa: S311
return "images/" + str(image)


# Create your models here.
class Species(models.Model):
CLASS_CHOICES = [
Expand Down Expand Up @@ -124,7 +109,6 @@ class Animal(models.Model):
upload_to="images/",
null=True,
blank=True,
default=random_image,
)
color = models.CharField(max_length=80, null=False, default="None")
sex = models.CharField(max_length=20, choices=SEX_CHOICES, default="UNKNOWN")
Expand Down
Loading

0 comments on commit 9a5dd68

Please sign in to comment.