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

G2P-418 and G2P-432 #98

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion gene2phenotype_project/gene2phenotype_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ class Meta:
class Panel(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100, unique=True, null=False)
description = models.CharField(max_length=255, null=True)
description = models.CharField(max_length=255, null=False, default="before_G2P_2025") #default was added because this is the default settings for django when changing from null=True to null=False
is_visible = models.SmallIntegerField(null=False)

def __str__(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .user import UserSerializer, LoginSerializer, CreateUserSerializer, AddUserToPanelSerializer, LogoutSerializer, ChangePasswordSerializer, VerifyEmailSerializer, PasswordResetSerializer

from .panel import PanelDetailSerializer, LGDPanelSerializer
from .panel import PanelCreateSerializer, PanelDetailSerializer, LGDPanelSerializer

from .publication import PublicationSerializer, LGDPublicationSerializer, LGDPublicationListSerializer

Expand Down
76 changes: 76 additions & 0 deletions gene2phenotype_project/gene2phenotype_app/serializers/panel.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,86 @@
from rest_framework import serializers
from django.db.models import Q
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist

from ..models import Panel, User, UserPanel, LGDPanel, Attrib

from ..utils import get_date_now


class PanelCreateSerializer(serializers.ModelSerializer):
"""
Panel Creation Serializer

Args:
name: short name of the panel (mandatory)
description: complete name of the panel (mandatory)
is_visible: panel visible to authenticated or non authenticated users


Raises:
serializers.ValidationError: Raises a validation error when the panel exists

Returns:
_type_: A created panel
"""
name = serializers.CharField(required=True)
description = serializers.CharField(required=True)
is_visible = serializers.BooleanField(required=True)

def validate(self, attrs):
"""
Validate the request data

Args:
attrs (_type_): A dictionary like object containing the request data

Raises:
serializers.ValidationError: Raises a validation error when the panel exists

Returns:
_type_: A validated request object
"""
name = attrs.get('name')
if Panel.objects.filter(name=name, is_visible=1).exists():
raise serializers.ValidationError({'message': 'Can not create an existing panel'})

return attrs

def create(self, validated_data):
"""
Creation of the panel if panel does not exist
Updating the panel if is_visible = 0

Args:
validated_data (_type_): validated request object

Returns:
_type_: Created panel
"""
name = validated_data.get('name')
description = validated_data.get('description')
is_visible = validated_data.get("is_visible")
try:
panel = Panel.objects.get(name=name)

if panel.is_visible:
raise serializers.ValidationError({"message" : f"{name} exists!"})

if not panel.is_visible:
raise serializers.ValidationError({"message" : f"{name} exist. It is only visible to authenticated users"})

except Panel.DoesNotExist:
try:
panel = Panel.objects.create(name=name, description=description, is_visible=is_visible)
return panel
except IntegrityError as e:
raise serializers.ValidationError({"message": f"Database error: {str(e)}"})

class Meta:
model = Panel
fields = ['name', 'description', 'is_visible']

class PanelDetailSerializer(serializers.ModelSerializer):
"""
Serializer for the Panel model.
Expand Down
80 changes: 76 additions & 4 deletions gene2phenotype_project/gene2phenotype_app/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from rest_framework_simplejwt.tokens import RefreshToken, TokenError
from rest_framework.validators import UniqueValidator
from django.contrib.auth.models import update_last_login
from django.core.exceptions import ObjectDoesNotExist


from ..utils import CustomMail
from ..models import User, UserPanel, Panel
Expand Down Expand Up @@ -230,10 +232,40 @@ class Meta:
}

class AddUserToPanelSerializer(serializers.ModelSerializer):
"""
Adds User to a Panel serializer
Args:
serializers (_type_):
user : Serializers CharField
panel : Serializers ListField that only accepts CharField and can not be empty

Raises:
serializers.ValidationError: If UserEmail is not sent in the request
serializers.ValidationError: If User does not exists
serializers.ValidationError: If Panel does not exists
serializers.ValidationError: If UserPanel already exists

Returns:
_type_: Created UserPanel or Updated UserPanel
"""
user = serializers.CharField(write_only=True)
panel = serializers.ListField(child=serializers.CharField(), allow_empty=False, write_only=True)

def validate(self, attrs):
"""
Validation step for the AddUserPanelSerializer

Args:
attrs (_type_): The request data that will be validated

Raises:
serializers.ValidationError: If UserEmail is not sent in the request
serializers.ValidationError: If User does not exists
serializers.ValidationError: If Panel does not exists

Returns:
_type_: Validated data
"""
user = attrs.get('user', '')
if user is None:
raise serializers.ValidationError({'message': 'user email is required to add user to panel'})
Expand All @@ -251,24 +283,64 @@ def validate(self, attrs):


def create(self, validated_data):
"""
Creates the User panel, taking the User and a list of panels from validated data
Also calls the update function if UserPanel already exists

Args:
validated_data (_type_): A dictionary object containing a list of panels and user_email

Raises:
serializers.ValidationError: If UserPanel already exists and it is not deleted

Returns:
_type_: Created UserPanel
"""
user_email = validated_data.get('user')
panels = validated_data.get('panel')

user_obj = User.objects.get(email=user_email)
for panel in panels:
panel_obj = Panel.objects.get(name=panel)

if UserPanel.objects.filter(user=user_obj, panel=panel_obj, is_deleted=1).exists():
raise serializers.ValidationError({'message': f"User {user_email} used to exists in this {panel}, Update instead"})

self.update(user_obj, panel_obj)
continue

if not UserPanel.objects.filter(user=user_obj, panel=panel_obj, is_deleted=0).exists():
user_panel = UserPanel.objects.create(user=user_obj, panel=panel_obj, is_deleted=0)
else:
raise serializers.ValidationError({'message': f"User {user_email} already exists in this {panel}"})

return user_panel

def update(self, user, panel):
"""
Updates the UserPanel, if UserPanel exists
Changes from is_deleted = 1 to is_deleted = 0

Args:
user (_type_): user object
panel (_type_): Panel object

Returns:
_type_: The updated user panel
"""
try:
user_panel = UserPanel.objects.get(user=user, panel=panel, is_deleted=1)
user_panel.is_deleted = 0
user_panel.save()


user_info = {
"message" : f"{user} has been updated in this {panel}"
}

except ObjectDoesNotExist:
return {
"message": f"{user} does not exist in this panel "
}

return user_info
class Meta:
model = UserPanel
fields = ['user', 'panel']
Expand Down
Loading