Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Market Feed Endpoints #368

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
5 changes: 4 additions & 1 deletion backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'onboarding',
'marketfeed',
'drf_spectacular',
'rest_framework',
'rest_framework_simplejwt',
'rest_framework_simplejwt.token_blacklist',
]


MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
Expand All @@ -50,9 +52,11 @@
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


CORS_ALLOW_ALL_ORIGINS = True
ROOT_URLCONF = 'backend.urls'


TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
Expand All @@ -72,7 +76,6 @@
WSGI_APPLICATION = 'backend.wsgi.application'



DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
Expand Down
2 changes: 1 addition & 1 deletion backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
name="swagger-ui",
),
path("", include("onboarding.urls")),

path("", include("marketfeed.urls")),
]

if settings.DEBUG:
Expand Down
4 changes: 2 additions & 2 deletions backend/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

import time
import mysql.connector
# import mysql.connector
import os
from django.core.management import execute_from_command_line
from django.db import connection, OperationalError
Expand All @@ -20,7 +20,7 @@ def main():
connection.ensure_connection()
print("Db is ready")
break
except OperationalError:
except OperationalError as e:
print("Database not ready yet. Waiting...")
time.sleep(5)

Expand Down
Empty file added backend/marketfeed/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions backend/marketfeed/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions backend/marketfeed/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class MarketfeedConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "marketfeed"
184 changes: 184 additions & 0 deletions backend/marketfeed/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# Generated by Django 4.2 on 2024-11-03 15:49

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Currency",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=40)),
("code", models.CharField(max_length=5, unique=True)),
],
),
migrations.CreateModel(
name="Portfolio",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=50)),
("description", models.CharField(max_length=150)),
("created_at", models.DateTimeField()),
("updated_at", models.DateTimeField(auto_now=True, null=True)),
],
),
migrations.CreateModel(
name="Tag",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=40, unique=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"user_id",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.CreateModel(
name="Stock",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=250)),
("symbol", models.CharField(max_length=250, unique=True)),
(
"currency",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="marketfeed.currency",
),
),
],
),
migrations.CreateModel(
name="Post",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=50)),
("content", models.TextField()),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True, null=True)),
(
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
(
"liked_by",
models.ManyToManyField(
related_name="liked_posts", to=settings.AUTH_USER_MODEL
),
),
(
"portfolios",
models.ManyToManyField(
to="marketfeed.portfolio", verbose_name="list of portfolios"
),
),
(
"tags",
models.ManyToManyField(
to="marketfeed.tag", verbose_name="list of tags"
),
),
],
),
migrations.AddField(
model_name="portfolio",
name="stocks",
field=models.ManyToManyField(
to="marketfeed.stock", verbose_name="list of stocks in the portfolio"
),
),
migrations.AddField(
model_name="portfolio",
name="user_id",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
migrations.CreateModel(
name="Comment",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("content", models.CharField(max_length=250)),
(
"post_id",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="marketfeed.post",
),
),
(
"user_id",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
18 changes: 18 additions & 0 deletions backend/marketfeed/migrations/0002_alter_portfolio_created_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2 on 2024-11-03 19:59

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("marketfeed", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="portfolio",
name="created_at",
field=models.DateTimeField(auto_now_add=True),
),
]
Empty file.
71 changes: 71 additions & 0 deletions backend/marketfeed/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from django.db import models
from onboarding.models import *

# Models for market feed such as post, portfolio, stock, comment

"""-----------------------------------------------------MANAGERS-----------------------------------------------------"""

class StockManager(models.Manager):
def get_queryset(self):
queryset = super().get_queryset()
return queryset


"""-------------------------------------------------------MODELS-------------------------------------------------------"""

class Currency(models.Model):
name = models.CharField(max_length=40)
code = models.CharField(max_length=5, unique=True)


class Stock(models.Model):
name = models.CharField(max_length=250)
symbol = models.CharField(max_length=250, unique=True)
currency = models.ForeignKey(Currency, on_delete=models.CASCADE)

objects = StockManager()

@property
def price(self):
return self.fetch_current_stock_price()

def fetch_current_stock_price(self):
#TODO: Stock fetching mechanism to be implemented
return 10


class Tag(models.Model):
name = models.CharField(max_length=40, unique=True)
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)

def save(self, **kwargs):
self.name = self.name.lower()
return super().save(**kwargs)


class Portfolio(models.Model):
name = models.CharField(max_length=50)
description = models.CharField(max_length = 150)
user_id = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True, auto_now=True)
stocks = models.ManyToManyField(Stock, verbose_name="list of stocks in the portfolio")


class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
liked_by = models.ManyToManyField(User, related_name="liked_posts")
tags = models.ManyToManyField(Tag, verbose_name="list of tags")
portfolios = models.ManyToManyField(Portfolio, verbose_name="list of portfolios")


class Comment(models.Model):
post_id=models.ForeignKey(Post, on_delete=models.CASCADE)
user_id=models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
content = models.CharField(max_length=250)
Loading