Skip to content

Commit

Permalink
Refactor + docker-compose-development
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent-01 committed May 18, 2023
1 parent c3a4658 commit 9ab8a84
Show file tree
Hide file tree
Showing 42 changed files with 508 additions and 347 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,6 @@ dmypy.json

# Pyre type checker
.pyre/

# Poetry
backend/poetry.lock
4 changes: 2 additions & 2 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ The env variables in the .env files might be slightly different.
### jasma-api-server

`START_FIRST_TIME` Controls if the PSQL database is re-created on server startup. `(true / false)` <br />
`NODE_ENV` Controls if the app is started in dev or production mode. `(development / production)` <br />
`STAGE` Controls if the app is started in dev or production mode. `(development / production)` <br />
`HOSTNAME` Controls the hostname of the server. NOT USED (YET?). <br />
`PORT` Controls the port of the API server. `(Integer)` <br />
`SESSION_SECRET` Controls the session secret of the cookies stored in Redis. `(String)` <br />
Expand All @@ -187,7 +187,7 @@ The env variables in the .env files might be slightly different.
`PORT` The port of NextJS. Used to connect the user/browser to the client. NextJS Server Side Rendering. `(Integer)` <br />
`NEXT_PUBLIC_API_SERVER_URL` The IP address or domain of the API server. Used for connecting client to backend. `(IP address or domain)` <br />
`NEXT_PUBLIC_API_SERVER_PORT` The port the API server. Used for connecting client to backend. `(Integer)` <br />
`NEXT_PUBLIC_NODE_ENV` Controls if the app is started in dev or production mode. `(development / production)` <br />
`NEXT_PUBLIC_STAGE` Controls if the app is started in dev or production mode. `(development / production)` <br />
`ANALYZE` Controls if the NextJS document size analyzer should be turned on. Setting this to true might crash the browser. `(true / false)` <br />
`SESSION_SECRET` NextJS session secret. NOT USED. `(String)` <br />
`PAYPAL_SECRET` Paypal secret of your paypal account. NOT USED. `(String)` <br />
Expand Down
29 changes: 0 additions & 29 deletions backend/.env.example

This file was deleted.

15 changes: 15 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM python:3.11-bullseye AS backend
ENV PYTHONUNBUFFERED=1
WORKDIR /backend
COPY requirements.txt ./
RUN apt update && apt upgrade -y
RUN pip install -r requirements.txt
COPY docker-entrypoint.sh ./
RUN chmod +x docker-entrypoint.sh
ENTRYPOINT ["bash", "/backend/docker-entrypoint.sh"]
CMD [ "python", "manage.py", "runserver" ]

# We will need to revisit this file as this doess't really allow to get rid of dev dependencies
# Look into poetry / pipenv for this
# We'll need to manage migration carefully

7 changes: 7 additions & 0 deletions backend/api/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Could be an idea to review naming scheme so as to use:
# from api.constants import files
# then file.ALLOWED_PATH_TYPE, FILE.ALLOWED_PATH_CONTEXT, ...
# and for things like gender
# from api.constants import genders
# then use like:à
# gender.LIST, GENDER.MODEL
54 changes: 26 additions & 28 deletions backend/api/generate_fake_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import random
from faker import Faker
from django.db.utils import IntegrityError
from api.models import User, Post, Comment, Hashtag, User_Profile, Reported_Post, Following, Subscribed_Hashtag, Bookmarked_Post, Ad
from api.models import User, Post, Comment, Hashtag, UserProfile, ReportedPost, Following, SubscribedHashtag, BookmarkedPost, Ad
from api.constants.genders import GENDERS
from api.constants.relationships import RELATIONSHIPS
from api.constants.countries import COUNTRIES
Expand Down Expand Up @@ -43,7 +43,7 @@ def generate_user():
#if created == False:
user.after_create()

user_profile = User_Profile.objects.get(user=user)
user_profile = UserProfile.objects.get(user=user)
user_profile.given_name = fake.first_name()
user_profile.last_name = fake.last_name()
user_profile.bio = fake.text()
Expand All @@ -62,7 +62,7 @@ def generate_user_relationship():
user = pick_random_user()
relationship_with = pick_random_user()

user_profile = User_Profile.objects.get(user=user)
user_profile = UserProfile.objects.get(user=user)
user_profile.relationship_with = relationship_with
user_profile.save()

Expand All @@ -81,14 +81,13 @@ def generate_post():
file_url=None,
post_type="text")

for i in range(random.randint(1, 5)):
#hashtag = generate_hashtag()
for _ in range(random.randint(1, 5)):
hashtag = pick_random_hashtag()
post.hashtags.add(hashtag)
post.save()

def generate_comment():
comment = Comment.objects.create(
Comment.objects.create(
user=pick_random_user(),
post=pick_random_post(),
text_content=fake.text(),
Expand All @@ -106,16 +105,15 @@ def generate_reported_post():
report_reason = random.choice(report_reasons)
try:
# First check if the post has already been reported
reported_post = Reported_Post.objects.get(post=post)
reported_post = ReportedPost.objects.get(post=post)
if reported_post is not None:
# if it has already been reported then increase the number of reports
reported_post.reported_x_times += 1
reported_post.save()
except Reported_Post.DoesNotExist:
except ReportedPost.DoesNotExist:
# If not reported, create a new report for the post
reported_post = Reported_Post.objects.create(
post=post,
report_reason=report_reason)
ReportedPost.objects.create(post=post,
report_reason=report_reason)

def generate_follower():
user = None
Expand All @@ -125,16 +123,16 @@ def generate_follower():
user = pick_random_user()
follow_new_person = pick_random_user()
if user != follow_new_person:
following, created = Following.objects.get_or_create(
_, created = Following.objects.get_or_create(
user=user,
following=follow_new_person)

def generate_subscribed_hashtag():
def generate_subscribed_hashtags():
created = False
while created == False:
user = pick_random_user()
hashtag = pick_random_hashtag()
subscribed_hashtag, created = Subscribed_Hashtag.objects.get_or_create(
_, created = SubscribedHashtag.objects.get_or_create(
user=user,
hashtag=hashtag)

Expand All @@ -143,7 +141,7 @@ def generate_bookmarked_post():
while created == False:
user = pick_random_user()
post = pick_random_post()
bookmarked_post, created = Bookmarked_Post.objects.get_or_create(
_, created = BookmarkedPost.objects.get_or_create(
user=user,
post=post)

Expand All @@ -156,7 +154,7 @@ def generate_ad():
while age_start != age_end and age_start < age_end:
age_start = random.randint(18, 100)
age_end = random.randint(18, 100)
ad = Ad.objects.get_or_create(
Ad.objects.get_or_create(
user=pick_random_user(),
ad_name=fake.word(),
text_content=fake.text(),
Expand All @@ -174,53 +172,53 @@ def generate_fake_db(n):
print(f"GENERATING {n} users, {n*3} hashtags, {n*2} posts, {n*4} comments, {n*3} followers, {math.floor(n * 0.1)} ads, {n*3} followers, {n*3} bookmarked posts, and {math.floor(n * 0.5)} reported posts...")

print("Generating fake users...")
for i in range(n):
for _ in range(n):
generate_user()

# 20% of users are in a relationship with another user
for i in range(math.floor(n * 0.2)):
for _ in range(math.floor(n * 0.2)):
generate_user_relationship()
print(f"{OK} Done user generation.")

print("Generating fake hashtags...")
for i in range(n*3):
for _ in range(n*3):
generate_hashtag()
print(f"{OK} Done hashtag generation.")

print("Generating fake posts...")
for i in range(n*2):
for _ in range(n*2):
generate_post()
print(f"{OK} Done post generation.")

print("Generating fake comments...")
for i in range(n*4):
for _ in range(n*4):
generate_comment()
print(f"{OK} Done comment generation.")

# 25% of posts
print("Generating fake reported posts...")
for i in range(math.floor(n * 0.5)):
for _ in range(math.floor(n * 0.5)):
generate_reported_post()
print(f"{OK} Done reported post generation.")

print("Generating fake followers...")
for i in range(n*3):
for _ in range(n*3):
generate_follower()
print(f"{OK} Done follower generation.")

print("Generating fake subscribed hashtags...")
for i in range(n*2):
generate_subscribed_hashtag()
for _ in range(n*2):
generate_subscribed_hashtags()
print(f"{OK} Done subscribed hashtag generation.")

print("Generating fake bookmarked posts...")
for i in range(n*3):
for _ in range(n*3):
generate_bookmarked_post()
print(f"{OK} Done bookmarked post generation.")

# 10% of users
print("Generating fake ads...")
for i in range(math.floor(n * 0.1)):
for _ in range(math.floor(n * 0.1)):
generate_ad()
print(f"{OK} Done ad generation.")

Expand All @@ -237,4 +235,4 @@ def generate_fake_db(n):

if __name__ == "__main__":
fakes_to_generate = int(sys.argv[1])
generate_fake_db(fakes_to_generate)
generate_fake_db(fakes_to_generate)
22 changes: 11 additions & 11 deletions backend/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2 on 2023-05-13 05:54
# Generated by Django 4.2 on 2023-05-18 03:29

from django.conf import settings
import django.contrib.auth.models
Expand Down Expand Up @@ -54,7 +54,7 @@ class Migration(migrations.Migration):
],
),
migrations.CreateModel(
name='Bug_Report',
name='BugReport',
fields=[
('bug_report_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('report_description', models.CharField(max_length=5000)),
Expand Down Expand Up @@ -105,7 +105,7 @@ class Migration(migrations.Migration):
},
),
migrations.CreateModel(
name='User_Notification_Preferences',
name='UserNotificationPreferences',
fields=[
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
('is_all_email', models.BooleanField(default=True)),
Expand Down Expand Up @@ -142,7 +142,7 @@ class Migration(migrations.Migration):
},
),
migrations.CreateModel(
name='Subscribed_Hashtag',
name='SubscribedHashtag',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('hashtag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.hashtag')),
Expand All @@ -154,7 +154,7 @@ class Migration(migrations.Migration):
},
),
migrations.CreateModel(
name='Reported_Post',
name='ReportedPost',
fields=[
('report_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('report_reason', models.CharField(max_length=300)),
Expand Down Expand Up @@ -197,7 +197,7 @@ class Migration(migrations.Migration):
},
),
migrations.CreateModel(
name='Bookmarked_Post',
name='BookmarkedPost',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('bookmarked_at', models.DateTimeField(auto_now_add=True)),
Expand Down Expand Up @@ -233,10 +233,10 @@ class Migration(migrations.Migration):
},
),
migrations.CreateModel(
name='User_Profile',
name='UserProfile',
fields=[
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
('profile_pic_url', models.URLField(default='http://localhost:8000/media/images/avatars/default-profile-pic.webp', max_length=300)),
('profile_pic_url', models.URLField(default='http://jasma-api-server:5000/media/images/avatars/default-profile-pic.webp', max_length=300)),
('given_name', models.CharField(blank=True, max_length=35, null=True)),
('last_name', models.CharField(blank=True, max_length=35, null=True)),
('display_name', models.CharField(blank=True, max_length=70, null=True)),
Expand All @@ -256,19 +256,19 @@ class Migration(migrations.Migration):
},
),
migrations.AddConstraint(
model_name='subscribed_hashtag',
model_name='subscribedhashtag',
constraint=models.UniqueConstraint(fields=('user', 'hashtag'), name='composite_pk_on_subscribed_hashtags'),
),
migrations.AddConstraint(
model_name='reported_post',
model_name='reportedpost',
constraint=models.UniqueConstraint(fields=('post',), name='unique_reported_post'),
),
migrations.AddConstraint(
model_name='following',
constraint=models.UniqueConstraint(fields=('user', 'following'), name='composite_pk_on_following'),
),
migrations.AddConstraint(
model_name='bookmarked_post',
model_name='bookmarkedpost',
constraint=models.UniqueConstraint(fields=('user', 'post'), name='composite_pk_on_bookmarked_posts'),
),
]
17 changes: 17 additions & 0 deletions backend/api/migrations/0002_alter_post_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-05-18 03:47

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='post',
options={'ordering': ['-created_at'], 'verbose_name_plural': 'Posts'},
),
]
17 changes: 17 additions & 0 deletions backend/api/migrations/0003_alter_comment_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-05-18 03:48

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0002_alter_post_options'),
]

operations = [
migrations.AlterModelOptions(
name='comment',
options={'ordering': ['created_at'], 'verbose_name_plural': 'Comments'},
),
]
17 changes: 17 additions & 0 deletions backend/api/migrations/0004_alter_comment_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2 on 2023-05-18 03:49

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('api', '0003_alter_comment_options'),
]

operations = [
migrations.AlterModelOptions(
name='comment',
options={'ordering': ['-created_at'], 'verbose_name_plural': 'Comments'},
),
]
Loading

0 comments on commit 9ab8a84

Please sign in to comment.