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

Created models for penn wrapped -- issue with pip files though #309

Merged
merged 11 commits into from
Nov 17, 2024
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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to prefix with stat_ because it's a bit redundant

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

Check warning on line 14 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L14

Added line #L14 was not covered by tests

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}"

Check warning on line 43 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L43

Added line #L43 was not covered by tests


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}"

Check warning on line 57 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L57

Added line #L57 was not covered by tests


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}"

Check warning on line 71 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L71

Added line #L71 was not covered by tests


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this something more descriptive to its actual inherent meaning. Maybe IndividualStatPageField (and the other GlobalStatPageField

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 (

Check warning on line 82 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L82

Added line #L82 was not covered by tests
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}"

Check warning on line 91 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L91

Added line #L91 was not covered by tests


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 (

Check warning on line 102 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L102

Added line #L102 was not covered by tests
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}"

Check warning on line 107 in backend/wrapped/models.py

View check run for this annotation

Codecov / codecov/patch

backend/wrapped/models.py#L107

Added line #L107 was not covered by tests
Loading
Loading