From 439f0b98addb5a24f4aa4954b43fc0db997501c8 Mon Sep 17 00:00:00 2001 From: dglemos Date: Wed, 23 Oct 2024 10:49:12 +0100 Subject: [PATCH 1/2] Return list of curators --- .../serializers/locus_genotype_disease.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/gene2phenotype_project/gene2phenotype_app/serializers/locus_genotype_disease.py b/gene2phenotype_project/gene2phenotype_app/serializers/locus_genotype_disease.py index 75a466bd..8dc05ece 100644 --- a/gene2phenotype_project/gene2phenotype_app/serializers/locus_genotype_disease.py +++ b/gene2phenotype_project/gene2phenotype_app/serializers/locus_genotype_disease.py @@ -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, @@ -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): @@ -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. From 08ce2c5336a181bfccf3694d93d16e5bf2c31be0 Mon Sep 17 00:00:00 2001 From: dglemos Date: Fri, 25 Oct 2024 15:11:51 +0100 Subject: [PATCH 2/2] Update permission to create users --- .../gene2phenotype_app/serializers/user.py | 50 +++++++------------ .../gene2phenotype_app/views/user.py | 12 +---- 2 files changed, 20 insertions(+), 42 deletions(-) diff --git a/gene2phenotype_project/gene2phenotype_app/serializers/user.py b/gene2phenotype_project/gene2phenotype_app/serializers/user.py index d8068460..5dd41e1c 100644 --- a/gene2phenotype_project/gene2phenotype_app/serializers/user.py +++ b/gene2phenotype_project/gene2phenotype_app/serializers/user.py @@ -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') @@ -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 @@ -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() diff --git a/gene2phenotype_project/gene2phenotype_app/views/user.py b/gene2phenotype_project/gene2phenotype_app/views/user.py index 843cdbb7..10331df9 100644 --- a/gene2phenotype_project/gene2phenotype_app/views/user.py +++ b/gene2phenotype_project/gene2phenotype_app/views/user.py @@ -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):