Skip to content

Commit

Permalink
Created models for penn wrapped -- issue with pip files though (#309)
Browse files Browse the repository at this point in the history
* Created models

* pip file fix

* Fixed models.py and added to admin.py

* models

* Penn-Wrapped Backend Requests

* fix github pipfile diff for no reason + others

* Updated Models

Finished Models for wrapped and serializer working

* Fixes

* Penn-Wrapped Prod Push


Request Format: /wrapped/generate/semester=2020A

---------

Co-authored-by: vcai122 <[email protected]>
  • Loading branch information
zachHarpaz and vcai122 authored Nov 17, 2024
1 parent 8315c52 commit d1044c2
Show file tree
Hide file tree
Showing 13 changed files with 454 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/pennmobile/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"accounts.apps.AccountsConfig",
"identity.apps.IdentityConfig",
"analytics.apps.AnalyticsConfig",
"wrapped.apps.WrappedConfig",
"django_filters",
"debug_toolbar",
"gsr_booking",
Expand Down
1 change: 1 addition & 0 deletions backend/pennmobile/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
path("dining/", include("dining.urls")),
path("penndata/", include("penndata.urls")),
path("sublet/", include("sublet.urls")),
path("wrapped/", include("wrapped.urls")),
]

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

from wrapped.models import (
GlobalStat,
GlobalStatKey,
GlobalStatPageField,
IndividualStat,
IndividualStatKey,
IndividualStatPageField,
Page,
Semester,
)


class WrappedIndividualAdmin(admin.ModelAdmin):
search_fields = ["user__username__icontains", "key__key__icontains", "semester__icontains"]
list_display = ["user", "key", "value", "semester"]


class WrappedGlobalAdmin(admin.ModelAdmin):

list_display = ["key", "value", "semester"]
search_fields = ["key__icontains"]


class IndividualStatPageFieldAdmin(admin.TabularInline):
model = IndividualStatPageField
extra = 1


class GlobalStatPageFieldAdmin(admin.TabularInline):
model = GlobalStatPageField
extra = 1


class PageAdmin(admin.ModelAdmin):
inlines = [IndividualStatPageFieldAdmin, GlobalStatPageFieldAdmin]


admin.site.register(IndividualStat, WrappedIndividualAdmin)
admin.site.register(GlobalStat, WrappedGlobalAdmin)
admin.site.register(IndividualStatKey)
admin.site.register(GlobalStatKey)
admin.site.register(Page, PageAdmin)
admin.site.register(Semester)
6 changes: 6 additions & 0 deletions backend/wrapped/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class WrappedConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "wrapped"
172 changes: 172 additions & 0 deletions backend/wrapped/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Generated by Django 5.0.2 on 2024-11-10 16:21

import datetime

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


class Migration(migrations.Migration):

initial = True

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

operations = [
migrations.CreateModel(
name="GlobalStatKey",
fields=[
("key", models.CharField(max_length=50, primary_key=True, serialize=False)),
],
),
migrations.CreateModel(
name="IndividualStatKey",
fields=[
("key", models.CharField(max_length=50, primary_key=True, serialize=False)),
],
),
migrations.CreateModel(
name="GlobalStatPageField",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("text_field_name", models.CharField(max_length=50)),
(
"global_stat_key",
models.ForeignKey(
default=None,
on_delete=django.db.models.deletion.CASCADE,
to="wrapped.globalstatkey",
),
),
],
),
migrations.CreateModel(
name="IndividualStatPageField",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("text_field_name", models.CharField(max_length=50)),
(
"individual_stat_key",
models.ForeignKey(
default=None,
on_delete=django.db.models.deletion.CASCADE,
to="wrapped.individualstatkey",
),
),
],
),
migrations.CreateModel(
name="Page",
fields=[
("name", models.CharField(max_length=50, primary_key=True, serialize=False)),
("template_path", models.CharField(max_length=50)),
("duration", models.DurationField(blank=True, default=datetime.timedelta(0))),
(
"global_stats",
models.ManyToManyField(
blank=True,
through="wrapped.GlobalStatPageField",
to="wrapped.globalstatkey",
),
),
(
"individual_stats",
models.ManyToManyField(
blank=True,
through="wrapped.IndividualStatPageField",
to="wrapped.individualstatkey",
),
),
],
),
migrations.AddField(
model_name="individualstatpagefield",
name="Page",
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="wrapped.page"),
),
migrations.AddField(
model_name="globalstatpagefield",
name="Page",
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="wrapped.page"),
),
migrations.CreateModel(
name="Semester",
fields=[
("semester", models.CharField(max_length=5, primary_key=True, serialize=False)),
("pages", models.ManyToManyField(blank=True, to="wrapped.page")),
],
),
migrations.CreateModel(
name="IndividualStat",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("value", models.CharField(max_length=50)),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
(
"key",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="wrapped.individualstatkey"
),
),
(
"semester",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="wrapped.semester"
),
),
],
options={
"unique_together": {("key", "semester", "user")},
},
),
migrations.CreateModel(
name="GlobalStat",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("value", models.CharField(max_length=50)),
(
"key",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="wrapped.globalstatkey"
),
),
(
"semester",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="wrapped.semester"
),
),
],
options={
"unique_together": {("key", "semester")},
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Generated by Django 5.0.2 on 2024-11-10 17:40

import datetime

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.RenameField(
model_name="globalstatpagefield",
old_name="Page",
new_name="page",
),
migrations.RenameField(
model_name="individualstatpagefield",
old_name="Page",
new_name="page",
),
migrations.AlterField(
model_name="page",
name="duration",
field=models.DurationField(blank=True, default=datetime.timedelta(0), null=True),
),
]
Empty file.
107 changes: 107 additions & 0 deletions backend/wrapped/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from datetime import timedelta

from django.contrib.auth import get_user_model
from django.db import models


User = get_user_model()


class StatKey(models.Model):
key = models.CharField(max_length=50, primary_key=True, null=False, blank=False)

def __str__(self) -> str:
return self.key

class Meta:
abstract = True


class IndividualStatKey(StatKey):
pass


class GlobalStatKey(StatKey):
pass


class Semester(models.Model):
semester = models.CharField(max_length=5, primary_key=True, null=False, blank=False)
pages = models.ManyToManyField("Page", blank=True)


class GlobalStat(models.Model):

key = models.ForeignKey(GlobalStatKey, on_delete=models.CASCADE)
value = models.CharField(max_length=50, null=False, blank=False)
semester = models.ForeignKey(Semester, on_delete=models.CASCADE)

class Meta:
unique_together = ("key", "semester")

def __str__(self):
return f"Global -- {self.key}-{str(self.semester)} : {self.value}"


class IndividualStat(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
key = models.ForeignKey(IndividualStatKey, on_delete=models.CASCADE)

value = models.CharField(max_length=50, null=False, blank=False)
semester = models.ForeignKey(Semester, on_delete=models.CASCADE)

class Meta:
unique_together = ("key", "semester", "user")

def __str__(self) -> str:
return f"User: {self.user} -- {self.key}-{str(self.semester)} : {self.value}"


class Page(models.Model):

name = models.CharField(max_length=50, primary_key=True, null=False, blank=False)
template_path = models.CharField(max_length=50, null=False, blank=False)
individual_stats = models.ManyToManyField(
IndividualStatKey, through="IndividualStatPageField", blank=True
)
global_stats = models.ManyToManyField(GlobalStatKey, through="GlobalStatPageField", blank=True)
duration = models.DurationField(blank=True, null=True, default=timedelta(minutes=0))

def __str__(self):
return f"{self.name}"


class IndividualStatPageField(models.Model):
individual_stat_key = models.ForeignKey(
IndividualStatKey, null=False, blank=False, default=None, on_delete=models.CASCADE
)
page = models.ForeignKey(Page, null=False, blank=False, on_delete=models.CASCADE)
text_field_name = models.CharField(max_length=50, null=False, blank=False)

def get_value(self, user, semester):
return (
IndividualStat.objects.filter(
user=user, key=self.individual_stat_key, semester=semester
)
.first()
.value
)

def __str__(self):
return f"{self.page} -> {self.text_field_name} : {self.individual_stat_key}"


class GlobalStatPageField(models.Model):
global_stat_key = models.ForeignKey(
GlobalStatKey, null=False, blank=False, default=None, on_delete=models.CASCADE
)
page = models.ForeignKey(Page, null=False, blank=False, on_delete=models.CASCADE)
text_field_name = models.CharField(max_length=50, null=False, blank=False)

def get_value(self, _user, semester):
return (
GlobalStat.objects.filter(key=self.global_stat_key.key, semester=semester).first().value
)

def __str__(self):
return f"{self.page} -> {self.text_field_name} : {self.global_stat_key}"
Loading

0 comments on commit d1044c2

Please sign in to comment.