Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into update/endpoint_lgd_publications
  • Loading branch information
dglemos committed Oct 30, 2024
2 parents 886cc7f + ec25c73 commit 7ae8b12
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework import serializers
from django.db import connection
from datetime import datetime
import itertools

from ..models import (Panel, Attrib,
LGDPanel, LocusGenotypeDisease, LGDVariantGenccConsequence,
Expand Down Expand Up @@ -42,6 +43,7 @@ class LocusGenotypeDiseaseSerializer(serializers.ModelSerializer):
last_updated = serializers.SerializerMethodField()
date_created = serializers.SerializerMethodField()
comments = serializers.SerializerMethodField(allow_null=True)
curators = serializers.SerializerMethodField(allow_null=True)
is_reviewed = serializers.IntegerField(allow_null=True, required=False)

def get_locus(self, id):
Expand Down Expand Up @@ -254,6 +256,49 @@ def get_date_created(self, id):

return date

def get_curators(self, id):
"""
List of curators who worked on the LGD record.
Dependency: this method depends on the history table.
Note: entries that were migrated from the old db have limited info details.
"""
list_curators = set()
lgd_obj = self.instance
# Check LGD record history
history_records = lgd_obj.history.all().values('history_user__first_name', 'history_user__last_name')
# Check LGD cross cutting modifier history
history_records_ccm = LGDCrossCuttingModifier.history.filter(lgd=lgd_obj).values(
'history_user__first_name', 'history_user__last_name')
# Check LGD panel history
history_records_lgdpanel = LGDPanel.history.filter(lgd=lgd_obj).values(
'history_user__first_name', 'history_user__last_name')
# Check LGD phenotype history
history_records_lgdpheno = LGDPhenotype.history.filter(lgd=lgd_obj).values(
'history_user__first_name', 'history_user__last_name')
# Check LGD publication history
history_records_lgdpublication = LGDPublication.history.filter(lgd=lgd_obj).values(
'history_user__first_name', 'history_user__last_name')
# Check LGD variation GenCC consequence history
history_records_lgdvarcons = LGDVariantGenccConsequence.history.filter(lgd=lgd_obj).values(
'history_user__first_name', 'history_user__last_name')
# Check LGD variation type history
history_records_lgdvartype = LGDVariantType.history.filter(lgd=lgd_obj).values(
'history_user__first_name', 'history_user__last_name')
# Check LGD variation type description history
history_records_lgdvartype_desc = LGDVariantTypeDescription.history.filter(lgd=lgd_obj).values(
'history_user__first_name', 'history_user__last_name')

for record in itertools.chain(history_records, history_records_ccm, history_records_lgdpanel,
history_records_lgdpheno, history_records_lgdpublication,
history_records_lgdvarcons, history_records_lgdvartype,
history_records_lgdvartype_desc):
first_name = record.get('history_user__first_name')
last_name = record.get('history_user__last_name')
list_curators.add(f"{first_name} {last_name}")

return list_curators

def check_user_permission(self, id, user_panels):
"""
Check if user has permission to update this G2P record.
Expand Down
50 changes: 18 additions & 32 deletions gene2phenotype_project/gene2phenotype_app/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,14 @@ def panels_names(self, id):

def check_panel_permission(self, panels):
"""
Check if user has permission to edit the panels.
Check if user has permission to edit the inputted panels.
Args:
self: user
panels: a list of panels
panels: a list of panels
Returns:
True if user has permission to edit all panels from the list
False if user does not have permission to edit at least one panel
True if user has permission to edit all panels from the list
False if user does not have permission to edit at least one panel
"""
user_login = self.context.get('user')

Expand All @@ -111,32 +110,7 @@ class Meta:

class CreateUserSerializer(serializers.ModelSerializer):
"""
Serializer for creating a new user.
This serializer is used to validate and create a new user object. It extends
`ModelSerializer` to automatically handle the fields related to the `User` model.
Methods:
- create(validated_data):
Overrides the default `create` method to create a user using
`create_user` method, which ensures that the password is hashed
before storing it in the database.
Fields:
- username: The username of the user.
- email: The email of the user. It has a `UniqueValidator` to ensure that
the email is unique in the system.
- password: The password for the user. This field is write-only and
has a minimum length of 5 characters to ensure password strength.
- first_name: The user's first name.
- last_name: The user's last name.
Meta Options:
- model: Specifies the `User` model to serialize.
- fields: Lists the fields included in the serialization.
- extra_kwargs:
- password: Write-only field with a minimum length of 5 characters.
- email: Includes a `UniqueValidator` to enforce unique email addresses.
This serializer is used to validate and create a new user object.
Usage:
This serializer can be used to create a new user by passing validated
Expand All @@ -145,12 +119,24 @@ class CreateUserSerializer(serializers.ModelSerializer):
"""

def create(self, validated_data):
"""
This method creates a user using the `create_user` method, which ensures that
the password is hashed before storing it in the database.
validated_data has the following fields:
- username: The username
- email: The email of the user (email is unique in the system).
- password: The password for the user. This field is write-only and
has a minimum length of 5 characters to ensure password strength.
- first_name: The user's first name.
- last_name: The user's last name.
"""
return User.objects.create_user(**validated_data)

class Meta:
model = User
fields = ['username', 'email', 'password', 'first_name', 'last_name']
extra_kwargs = {'password': {'write_only': True, 'min_length': 5}, 'email': {
extra_kwargs = {'password': {'write_only': True, 'min_length': 5}, 'email': {
'validators': [
UniqueValidator(
queryset=User.objects.all()
Expand Down
12 changes: 2 additions & 10 deletions gene2phenotype_project/gene2phenotype_app/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,19 @@ def list(self, request, *args, **kwargs):

class CreateUserView(generics.CreateAPIView):
"""
view for creating a new user.
View for creating a new user.
This view handles POST requests to create a new user using the `CreateUserSerializer`.
It is based on Django's `CreateAPIView` which provides the default implementation
for handling object creation.
Attributes:
- serializer_class: Specifies the serializer to be used, which is
`CreateUserSerializer`. This serializer handles validation and user
creation.
- permission_classes: Sets the permission policy for this view. In this case,
`AllowAny` is used, meaning that any user (authenticated or not) can
access this endpoint to create a new user.
Usage:
Send a POST request with the required user details (username, email,
password, first_name, last_name) to this API to create a new user account.
"""

serializer_class = CreateUserSerializer
permission_classes = (permissions.AllowAny,)
permission_classes = [permissions.IsAuthenticated]


class LoginView(KnoxLoginView):
Expand Down

0 comments on commit 7ae8b12

Please sign in to comment.