-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from mnqrt/feat/adrian/gpa-calculator
feat: add gpa calculator
- Loading branch information
Showing
6 changed files
with
212 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Generated by Django 3.1.2 on 2024-07-10 04:21 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0007_alter_review_rating_beneficial_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='UserCumulativeGPA', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('cumulative_gpa', models.FloatField(default=0)), | ||
('total_gpa', models.FloatField(default=0)), | ||
('total_sks', models.PositiveIntegerField(default=0)), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.profile')), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='UserGPA', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('given_semester', models.PositiveSmallIntegerField(editable=False)), | ||
('total_sks', models.PositiveIntegerField()), | ||
('semester_gpa', models.FloatField()), | ||
('userCumulativeGPA', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='main.usercumulativegpa')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import logging | ||
|
||
from rest_framework.decorators import api_view | ||
from rest_framework import status | ||
from .serializers import UserCumulativeGPASerializer, UserGPASerializer | ||
|
||
from .utils import response, validate_body, check_notexist_and_create_user_cumulative_gpa, validate_body_minimum, add_semester_gpa, delete_semester_gpa | ||
from .models import Profile, UserGPA, UserCumulativeGPA | ||
from django.db.models import F | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@api_view(['GET', 'POST', 'DELETE', 'PUT']) | ||
def gpa_calculator(request): | ||
|
||
user = Profile.objects.get(username=str(request.user)) | ||
|
||
if request.method == 'GET': | ||
user_cumulative_gpa = check_notexist_and_create_user_cumulative_gpa(user) | ||
|
||
user_gpas = UserGPA.objects.filter(userCumulativeGPA = user_cumulative_gpa) | ||
return response(data={'all_semester_gpa':UserGPASerializer(user_gpas, many=True).data, 'cumulative_gpa': UserCumulativeGPASerializer(user_cumulative_gpa).data}) | ||
|
||
if request.method == 'POST': | ||
is_valid = validate_body(request, ['semester_gpa', 'total_sks']) | ||
|
||
if is_valid != None : | ||
return is_valid | ||
|
||
semester_gpa = request.data.get('semester_gpa') | ||
total_sks = request.data.get('total_sks') | ||
|
||
user_cumulative_gpa = check_notexist_and_create_user_cumulative_gpa(user) | ||
|
||
user_gpa = UserGPA.objects.create(userCumulativeGPA=user_cumulative_gpa, \ | ||
total_sks=total_sks, \ | ||
semester_gpa=semester_gpa) | ||
|
||
add_semester_gpa(user_cumulative_gpa, | ||
total_sks=total_sks, | ||
semester_gpa=semester_gpa) | ||
|
||
return response(data=UserGPASerializer(user_gpa).data, status=status.HTTP_201_CREATED) | ||
|
||
if request.method == 'DELETE': | ||
user_cumulative_gpa = check_notexist_and_create_user_cumulative_gpa(user) | ||
|
||
user_gpa = UserGPA.objects.filter(userCumulativeGPA=user_cumulative_gpa).order_by('given_semester').last() | ||
|
||
if user_gpa is None: | ||
return response(error="There is no gpa to delete.", status=status.HTTP_404_NOT_FOUND) | ||
|
||
delete_semester_gpa(user_cumulative_gpa, | ||
total_sks=user_gpa.total_sks, | ||
semester_gpa=user_gpa.semester_gpa) | ||
|
||
user_gpa.delete() | ||
|
||
return response(status=status.HTTP_200_OK) | ||
|
||
if request.method == 'PUT': | ||
is_valid = validate_body(request, ['given_semester']) | ||
if is_valid != None: | ||
return is_valid | ||
|
||
is_valid = validate_body_minimum(request, ['total_sks', 'semester_gpa']) | ||
if is_valid != None: | ||
return is_valid | ||
|
||
given_semester = request.data.get('given_semester') | ||
|
||
user_cumulative_gpa = check_notexist_and_create_user_cumulative_gpa(user) | ||
user_gpa = UserGPA.objects.filter(userCumulativeGPA=user_cumulative_gpa, | ||
given_semester=given_semester).first() | ||
|
||
if user_gpa is None: | ||
return response(error="There is no object with given_semester={}.".format(given_semester), status=status.HTTP_404_NOT_FOUND) | ||
|
||
total_sks = request.data.get('total_sks') or user_gpa.total_sks | ||
semester_gpa = request.data.get('semester_gpa') or user_gpa.semester_gpa | ||
|
||
#Updates Cumulative GPA / Indeks Prestasi Kumulatif | ||
delete_semester_gpa(user_cumulative_gpa, | ||
total_sks=user_gpa.total_sks, | ||
semester_gpa=user_gpa.semester_gpa) | ||
add_semester_gpa(user_cumulative_gpa, | ||
total_sks=total_sks, | ||
semester_gpa=semester_gpa) | ||
|
||
user_gpa.total_sks = total_sks | ||
user_gpa.semester_gpa = semester_gpa | ||
user_gpa.save() | ||
|
||
return response(data=UserGPASerializer(user_gpa).data, status=status.HTTP_200_OK) |