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

[planner/backend] Model and API implementation #949

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions apps/planner/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from enum import Enum, auto
from typing import Dict, List, Tuple, Union, cast

from django.core.validators import MinValueValidator, MaxValueValidator
from django.db import models

from apps.session.models import UserProfile

from apps.subject.models import Department
from apps.subject.models import Department, Course


# change into - TODO
Expand Down Expand Up @@ -168,10 +168,23 @@ def credit_for_track(cls: Credit, user: UserProfile, track: Track) -> Dict[DeptC


class Planner(models.Model):
user = models.ForeignKey(UserProfile, on_delete=models.CASCADE, db_index=True)
user = models.ForeignKey(UserProfile,
related_name="planners", on_delete=models.CASCADE, db_index=True)

entrance_year = models.IntegerField(db_index=True)
# TODO add track
# TODO add to_json() function
track = models.ForeignKey(Track, on_delete=models.CASCADE, db_index=True)
arrange_order = models.SmallIntegerField(db_index=True)

def to_json(self, nested=False):
result = {
"id": self.id,
"arrange_order": self.arrange_order,
}
return result

@classmethod
def get_related_planners(cls, user, entrance_year, track):
return Planner.objects.filter(user=user, entrance_year=entrance_year, track=track)


class PlannerItem(models.Model):
Expand Down
22 changes: 22 additions & 0 deletions apps/planner/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.db.models import F, Case, When

from .models import Planner


def reorder_planner(planner: Planner, target_arrange_order: int):
related_planners = Planner.get_related_planners(planner.user,
planner.entrance_year, planner.track)
original_arrange_order = planner.arrange_order

if target_arrange_order < original_arrange_order:
related_planners.filter(arrange_order__gte=target_arrange_order,
arrange_order__lte=original_arrange_order) \
.update(arrange_order=Case(When(arrange_order=original_arrange_order,
then=target_arrange_order),
default=F("arrange_order")+1))
elif target_arrange_order > original_arrange_order:
related_planners.filter(arrange_order__gte=original_arrange_order,
arrange_order__lte=target_arrange_order) \
.update(arrange_order=Case(When(arrange_order=original_arrange_order,
then=target_arrange_order),
default=F("arrange_order")-1))
Loading