Skip to content

Commit

Permalink
Merge pull request #23 from Alexgodoroja/DB_seeder
Browse files Browse the repository at this point in the history
Db seeder
  • Loading branch information
Alexgodoroja authored Mar 8, 2023
2 parents 62c818a + ee950cc commit f989882
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 20 deletions.
113 changes: 113 additions & 0 deletions blogs/management/commands/seed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from django.core.management.base import BaseCommand
from faker import Faker
from faker.providers import internet, person
from blogs.models import User, Club
import random
import logging
""" Clear all data and creates addresses """
MODE_REFRESH = 'refresh'

""" Clear all data and do not create any object """
MODE_CLEAR = 'clear'

fake = Faker()
fake.add_provider(internet)
fake.add_provider(person)
user_list= []
themes = [ # Taken from Google Books
("E", "Ebooks"),
("A", "Arts"),
("BM", "Biographies & Memoirs"),
("BI", "Business & Investing"),
("C", "Comics"),
("CT", "Computers & Technology"),
("CF", "Cookery, Food & Wine"),
("F", "Fantasy"),
("FL", "Fiction & Literature"),
("G", "Gardening"),
("HF", "Health & Fitness"),
("HM", "Health, Mind & Body"),
("H", "History"),
("M", "Mystery & Thrillers"),
("N", "Nature"),
("P", "Poetry"),
("PC", "Politics & Current Affairs"),
("R", "Reference"),
("RO", "Romance"),
("RS", "Religion & Spirituality"),
("S", "Science"),
("SF", "Science Fiction"),
("SP", "Sports"),
("T", "Travel"),
("Y", "Young Adult"),
]
class Command(BaseCommand):
help = "seed database for testing and development."

def __init__(self):
super().__init__()
self.faker = Faker('en_GB')

def add_arguments(self, parser):
parser.add_argument('--mode', type=str, help="Mode")

def handle(self, *args, **options):
self.stdout.write('seeding data...')
run_seed(self, options['mode'])
self.stdout.write('done.')

def clear_data():
"""Deletes all the table data"""
logging.info("Delete Address instances")
User.objects.all().delete()


def create_user():
"""Creates an user object using faker"""
logging.info("Creating user")
fname = fake.unique.first_name()
lname = fake.unique.last_name()
user = User(
username = "@" + fname + str(random.randint(0, 12)) + str(random.randint(1,9)),
first_name = fname,
last_name = lname,
email = fname + "." + lname + "@" + fake.free_email_domain(),
bio = fake.paragraph(nb_sentences=5)
)
user.save()
logging.info("{} user created.".format(user))
return user

def create_club():
"""Creates an user object using faker"""
logging.info("Creating a club with exactly two admins and 10 members")


club = Club(
name = fake.text(max_nb_chars=20),
bio = fake.paragraph(nb_sentences=2),
rules = fake.paragraph(nb_sentences=4),
theme = (random.choice(themes))[1]
)
club.save()
club.admins.add(create_user())
club.admins.add(create_user())
for i in range(10):
current_user= create_user()
club.members.add(current_user)
logging.info("{} user created.".format(club))
return club
def run_seed(self, mode):
""" Seed database based on mode
:param mode: refresh / clear
:return:
"""
# Clear data from tables
clear_data()
if mode == MODE_CLEAR:
return

# Creating 25 clubs
for i in range(25):
create_club()
32 changes: 12 additions & 20 deletions blogs/templates/sign_up.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,19 @@
{%include 'partials/navbar_minimal.html'%}
<div class="blur ">

<div class="container pb-5 pt-3">
<div class="row">
<div class="col-12">
<h1>Sign up and start flying your kite!</h1>
<div class="container pb-5 pt-3">
<div class="row">
<div class="col-12">
<h1>Sign up and start flying your kite!</h1>

<form action="{% url 'sign_up' %}" method="post">
{% csrf_token %}
{% include 'partials/bootstrap_form.html' %}
<input class="btn btn-dark btn-lg btn-block" type="submit" value = "Sign up">
</form>
<form action="{% url 'sign_up' %}" method="post">
{% csrf_token %}
{% include 'partials/bootstrap_form.html' %}
<input class="btn btn-dark btn-lg btn-block" type="submit" value = "Sign up">
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

{%include 'partials/footer.html'%}
{%endblock%}
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ sqlparse==0.4.3
coverage==6.5.0
django-widget-tweaks==1.4.12
libgravatar==1.0.3
Faker==17.6.0
python-dateutil==2.8.2
six==1.16.0

0 comments on commit f989882

Please sign in to comment.