Skip to content

Commit

Permalink
Remove progress indicator and ally app, fix N+1 queries (#1613)
Browse files Browse the repository at this point in the history
* fix some n+1 querys

* upgrade python

* remove allies app (migrations done)

* remove progress tracker (ROY-FE)

* more N+1 queries

* add basename to role router
  • Loading branch information
mwvolo authored Feb 24, 2025
1 parent 8736d14 commit 6c9cc93
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.11.8
3.13
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.6 on 2025-02-21 21:35

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("allies", "0012_remove_ally_adaptive_courseware_and_more"),
]

operations = [
migrations.RemoveField(
model_name="ally",
name="page_ptr",
),
migrations.DeleteModel(
name="AllySubject",
),
migrations.DeleteModel(
name="Ally",
),
]
10 changes: 0 additions & 10 deletions allies/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
from django.db import models
from wagtail.models import Page
from snippets.models import Subject

#TODO: remove this app after all migrations have been applied
class AllySubject(models.Model):
pass


class Ally(Page):
pass

16 changes: 16 additions & 0 deletions api/migrations/0013_delete_progresstracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Generated by Django 5.0.12 on 2025-02-22 00:27

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("api", "0012_alter_webviewsettings_options"),
]

operations = [
migrations.DeleteModel(
name="ProgressTracker",
),
]
8 changes: 0 additions & 8 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
from books.models import Book


class ProgressTracker(models.Model):
account_id = models.IntegerField()
progress = models.IntegerField()

def __str__(self):
return self.account_id


class CustomizationRequest(models.Model):
email = models.CharField(max_length=255)
num_students = models.IntegerField()
Expand Down
18 changes: 1 addition & 17 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from salesforce.models import Adopter
from wagtail.images.models import Image
from wagtail.documents.models import Document
from api.models import ProgressTracker, CustomizationRequest
from api.models import CustomizationRequest


class AdopterSerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -53,22 +53,6 @@ class Meta:
)


class ProgressSerializer(serializers.HyperlinkedModelSerializer):

def create(self, validated_data):
progress, created = ProgressTracker.objects.update_or_create(
account_id=validated_data.get('account_id', None),
defaults={'progress':validated_data.get('progress', None)})

return progress

class Meta:
model = ProgressTracker
fields = ('account_id',
'progress',
)


class ModuleListingField(serializers.StringRelatedField):
def to_internal_value(self, value):
return value
Expand Down
3 changes: 1 addition & 2 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from django.urls import include, path
from rest_framework import routers

from .views import AdopterViewSet, ImageViewSet, DocumentViewSet, ProgressViewSet, customize_request, sticky_note, footer, schools, mapbox, flags, errata_fields, give_today, webview_settings
from .views import AdopterViewSet, ImageViewSet, DocumentViewSet, customize_request, sticky_note, footer, schools, mapbox, flags, errata_fields, give_today, webview_settings

router = routers.DefaultRouter()
router.register(r'images', ImageViewSet)
router.register(r'documents', DocumentViewSet)
router.register(r'adopters', AdopterViewSet)
router.register(r'progress', ProgressViewSet)

urlpatterns = [
path('', include(router.urls)),
Expand Down
16 changes: 2 additions & 14 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from wagtail.images.models import Image
from wagtail.documents.models import Document
from wagtail.models import Site
from .models import ProgressTracker, FeatureFlag, WebviewSettings
from .serializers import AdopterSerializer, ImageSerializer, DocumentSerializer, ProgressSerializer, CustomizationRequestSerializer
from .models import FeatureFlag, WebviewSettings
from .serializers import AdopterSerializer, ImageSerializer, DocumentSerializer, CustomizationRequestSerializer


class AdopterViewSet(viewsets.ReadOnlyModelViewSet):
Expand All @@ -39,18 +39,6 @@ def get_queryset(self):
return Document.objects.all()


class ProgressViewSet(viewsets.ReadOnlyModelViewSet):
queryset = ProgressTracker.objects.all()
serializer_class = ProgressSerializer

def get_queryset(self):
queryset = ProgressTracker.objects.all()
account_id = self.request.query_params.get('account_id', None)
if account_id is not None:
queryset = queryset.filter(account_id=account_id)
return queryset


def sticky_note(request):
sticky_note = StickyNote.for_site(Site.find_for_request(request))

Expand Down
1 change: 1 addition & 0 deletions snippets/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def __str__(self):

register_snippet(AmazonBookBlurb)


class ContentWarning(TranslatableMixin, models.Model):
content_warning = models.TextField()

Expand Down
2 changes: 1 addition & 1 deletion snippets/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from . import views

router = routers.SimpleRouter()
router.register('roles', views.RoleViewSet)
router.register('roles', views.RoleViewSet, basename="Roles")
router.register('subjects', views.SubjectList, basename="Subjects")
router.register('k12subjects', views.K12SubjectList, basename="K12Subjects")
router.register('erratacontent', views.ErrataContentViewSet, basename="ErrataContent")
Expand Down
15 changes: 6 additions & 9 deletions snippets/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from rest_framework import viewsets

from .models import Role, Subject, K12Subject, ErrataContent, SubjectCategory, GiveBanner, BlogContentType, \
BlogCollection, NoWebinarMessage, WebinarCollection, AmazonBookBlurb
from .serializers import RoleSerializer, SubjectSerializer, K12SubjectSerializer, ErrataContentSerializer, \
Expand All @@ -8,19 +6,18 @@
WebinarCollectionSerializer, AmazonBookBlurbSerializer


from rest_framework import generics, viewsets
from rest_framework import viewsets
from django_filters.rest_framework import DjangoFilterBackend

SPANISH_LOCALE_ID = 2
ENGLISH_LOCALE_ID = 1


class RoleViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Role.objects.all()
serializer_class = RoleSerializer

def get_queryset(self):
queryset = Role.objects.all().order_by('display_name')
queryset = Role.objects.order_by('display_name')
name = self.request.query_params.get('name', None)
locale = self.request.query_params.get('locale', None)
if name is not None:
Expand All @@ -37,7 +34,7 @@ class SubjectList(viewsets.ReadOnlyModelViewSet):
filter_backends = (DjangoFilterBackend,)

def get_queryset(self):
queryset = Subject.objects.all().order_by('name')
queryset = Subject.objects.order_by('name')
name = self.request.query_params.get('name', None)
locale = self.request.query_params.get('locale', None)
if name is not None:
Expand All @@ -46,6 +43,7 @@ def get_queryset(self):
queryset = queryset.filter(locale=convert_locale(locale))
return queryset


class K12SubjectList(viewsets.ReadOnlyModelViewSet):
serializer_class = K12SubjectSerializer
filter_backends = (DjangoFilterBackend,)
Expand All @@ -60,6 +58,7 @@ def get_queryset(self):
queryset = queryset.filter(locale=convert_locale(locale))
return queryset


class ErrataContentViewSet(viewsets.ReadOnlyModelViewSet):
serializer_class = ErrataContentSerializer
filter_backends = (DjangoFilterBackend,)
Expand Down Expand Up @@ -129,7 +128,5 @@ def convert_locale(locale):


def convert_subject_name(subject):
subjects = Subject.objects.all()
result = subjects.filter(name=subject)
return result[0].id
return Subject.objects.filter(name=subject).values_list('id', flat=True).first()

16 changes: 7 additions & 9 deletions webinars/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import json

from django.db import models
from wagtail import blocks
from wagtail.fields import StreamField
Expand Down Expand Up @@ -59,14 +57,14 @@ class Webinar(models.Model):
@property
def selected_subjects(self):
prep_value = self.webinar_subjects.get_prep_value()
subjects = []
for s in prep_value:
for x in s['value']:
subject_id = x['value']['subject']
subject = Subject.objects.filter(id=subject_id)
subjects.append(str(subject[0]))
return subjects
subject_ids = [x['value']['subject'] for s in prep_value for x in s['value']]

subjects_map = {
subj.id: str(subj) for subj in Subject.objects.filter(id__in=subject_ids)
}

subjects = [subjects_map[x['value']['subject']] for s in prep_value for x in s['value']]
return subjects

@property
def selected_collections(self):
Expand Down

0 comments on commit 6c9cc93

Please sign in to comment.