From 59fab7d0f19f15784f50dac9c4f1c4932fae8018 Mon Sep 17 00:00:00 2001 From: Uditi Mehta Date: Thu, 10 Oct 2024 10:12:32 -0400 Subject: [PATCH] remove redundan and duplicate code --- api/preprints/serializers.py | 142 +++-------------------------------- 1 file changed, 10 insertions(+), 132 deletions(-) diff --git a/api/preprints/serializers.py b/api/preprints/serializers.py index 28ca617ee70..b8ad259aa3e 100644 --- a/api/preprints/serializers.py +++ b/api/preprints/serializers.py @@ -366,6 +366,7 @@ def require_admin_permission(): raise exceptions.ValidationError(detail=str(e)) if 'why_no_data' in validated_data: + require_admin_permission() try: preprint.update_why_no_data(auth, validated_data['why_no_data']) save_preprint = True @@ -384,6 +385,15 @@ def require_admin_permission(): preprint.update_data_links(auth, []) save_preprint = True + if 'has_prereg_links' in validated_data: + require_admin_permission() + + try: + preprint.update_has_prereg_links(auth, validated_data['has_prereg_links']) + save_preprint = True + except PreprintStateError as e: + raise exceptions.ValidationError(detail=str(e)) + if 'why_no_prereg' in validated_data: require_admin_permission() try: @@ -488,68 +498,6 @@ def require_admin_permission(): preprint.custom_publication_citation = validated_data['custom_publication_citation'] or None save_preprint = True - if 'has_coi' in validated_data: - try: - preprint.update_has_coi(auth, validated_data['has_coi']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'conflict_of_interest_statement' in validated_data: - try: - preprint.update_conflict_of_interest_statement(auth, validated_data['conflict_of_interest_statement']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'has_data_links' in validated_data: - try: - preprint.update_has_data_links(auth, validated_data['has_data_links']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'why_no_data' in validated_data: - try: - preprint.update_why_no_data(auth, validated_data['why_no_data']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'data_links' in validated_data: - try: - preprint.update_data_links(auth, validated_data['data_links']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'has_prereg_links' in validated_data: - try: - preprint.update_has_prereg_links(auth, validated_data['has_prereg_links']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'why_no_prereg' in validated_data: - try: - preprint.update_why_no_prereg(auth, validated_data['why_no_prereg']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'prereg_links' in validated_data: - try: - preprint.update_prereg_links(auth, validated_data['prereg_links']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'prereg_link_info' in validated_data: - try: - preprint.update_prereg_link_info(auth, validated_data['prereg_link_info']) - save_preprint = True - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - if published is not None: if not preprint.primary_file: raise exceptions.ValidationError( @@ -575,76 +523,6 @@ def require_admin_permission(): return preprint - def handle_author_assertions(self, preprint, validated_data, auth): - author_assertions = { - 'has_coi', - 'conflict_of_interest_statement', - 'has_data_links', - 'why_no_data', - 'data_links', - 'why_no_prereg', - 'prereg_links', - 'has_prereg_links', - 'prereg_link_info', - } - if author_assertions & validated_data.keys(): - if not preprint.is_admin_contributor(auth.user): - raise exceptions.PermissionDenied('User must be admin to add author assertions') - - if 'has_coi' in validated_data: - try: - preprint.update_has_coi(auth, validated_data['has_coi']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'conflict_of_interest_statement' in validated_data: - try: - preprint.update_conflict_of_interest_statement(auth, validated_data['conflict_of_interest_statement']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'has_data_links' in validated_data: - try: - preprint.update_has_data_links(auth, validated_data['has_data_links']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'why_no_data' in validated_data: - try: - preprint.update_why_no_data(auth, validated_data['why_no_data']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'data_links' in validated_data: - try: - preprint.update_data_links(auth, validated_data['data_links']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'has_prereg_links' in validated_data: - try: - preprint.update_has_prereg_links(auth, validated_data['has_prereg_links']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'why_no_prereg' in validated_data: - try: - preprint.update_why_no_prereg(auth, validated_data['why_no_prereg']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'prereg_links' in validated_data: - try: - preprint.update_prereg_links(auth, validated_data['prereg_links']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - - if 'prereg_link_info' in validated_data: - try: - preprint.update_prereg_link_info(auth, validated_data['prereg_link_info']) - except PreprintStateError as e: - raise exceptions.ValidationError(detail=str(e)) - def set_field(self, func, val, auth, save=False): try: func(val, auth)