Skip to content

Commit

Permalink
fix: merge version into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
Aydawka committed Nov 10, 2023
2 parents 28cd897 + a30d3cd commit de6a17e
Show file tree
Hide file tree
Showing 37 changed files with 1,246 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 120
ignore= W293
ignore= W293,W503
1 change: 1 addition & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"MD024": {
"siblings_only": true
},
"MD033": false,
"MD036": false,
"MD046": false
}
2 changes: 1 addition & 1 deletion alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

config = context.config
section = config.config_ini_section
print(section, environ.get("FAIRHUB_DATABASE_URL"))

config.set_section_option(
section, "FAIRHUB_DATABASE_URL", str(environ.get("FAIRHUB_DATABASE_URL"))
)
Expand Down
6 changes: 5 additions & 1 deletion apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .dataset_metadata.dataset_rights import api as rights
from .dataset_metadata.dataset_subject import api as subject
from .dataset_metadata.dataset_title import api as title
from .file import api as file_api
from .participant import api as participants_api
from .study import api as study_api
from .study_metadata.study_arm import api as arm
Expand Down Expand Up @@ -96,7 +97,9 @@


@api.route("/echo", endpoint="echo")
class HelloWorld(Resource):
class HelloEverynyan(Resource):
"""Test if the server is active"""

@api.response(200, "Success")
@api.response(400, "Validation Error")
def get(self):
Expand All @@ -106,6 +109,7 @@ def get(self):


api.add_namespace(study_api)
api.add_namespace(file_api)
api.add_namespace(dataset_api)
api.add_namespace(participants_api)
api.add_namespace(contributors_api)
Expand Down
55 changes: 32 additions & 23 deletions apis/authentication.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""This module is used to authenticate users to the system and
handle few authentication errors. Also, it sets token for logged user
along with expiration date"""
import datetime
import importlib
import os
import re

# import config
import uuid
from datetime import timezone
from typing import Any, Union
Expand Down Expand Up @@ -37,11 +38,16 @@


class UnauthenticatedException(Exception):
"""Exception raised when a user is not authenticated."""

# TODO: Implement this exception
pass


@api.route("/auth/signup")
class SignUpUser(Resource):
"""SignUpUser class is used to sign up new users to the system"""

@api.response(200, "Success")
@api.response(400, "Validation Error")
# @api.marshal_with(signup_model)
Expand All @@ -50,7 +56,14 @@ def post(self):
"""signs up the new users and saves data in DB"""
data: Union[Any, dict] = request.json
if os.environ.get("FLASK_ENV") != "testing":
if data["email_address"] not in ["[email protected]"]:
bypassed_emails = [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
]

if data["email_address"] not in bypassed_emails:
invite = model.StudyInvitedContributor.query.filter_by(
email_address=data["email_address"]
).one_or_none()
Expand Down Expand Up @@ -144,6 +157,8 @@ def validate_password(instance):

@api.route("/auth/login")
class Login(Resource):
"""Login class is used to login users to the system"""

@api.response(200, "Success")
@api.response(400, "Validation Error")
# @api.marshal_with(login_model)
Expand All @@ -156,9 +171,8 @@ def post(self):
email_address = data["email_address"]

def validate_is_valid_email(instance):
print("within is_valid_email")
email_address = instance
print(email_address)

try:
validate_email(email_address)
return True
Expand Down Expand Up @@ -352,16 +366,10 @@ def is_granted(permission: str, study=None):
return permission in role[contributor.permission]


#
# def is_study_metadata(study_id: int):
# study_obj = model.Study.query.get(study_id)
# if not is_granted("study_metadata", study_obj):
# return "Access denied, you can not delete study", 403
#


@api.route("/auth/logout")
class Logout(Resource):
"""Logout class is used to log out users from the system"""

@api.response(200, "Success")
@api.response(400, "Validation Error")
def post(self):
Expand All @@ -379,14 +387,15 @@ def post(self):
return resp


@api.route("/auth/current-users")
class CurrentUsers(Resource):
"""function is used to see all logged users in
the system. For now, it is used for testing purposes"""
# @api.route("/auth/current-users")
# class CurrentUsers(Resource):
# """function is used to see all logged users in
# the system. For now, it is used for testing purposes"""

@api.response(200, "Success")
@api.response(400, "Validation Error")
def get(self):
if not g.user:
return None
return g.user.to_dict()
# @api.response(200, "Success")
# @api.response(400, "Validation Error")
# def get(self):
# """returns all logged users in the system"""
# if not g.user:
# return None
# return g.user.to_dict()
30 changes: 30 additions & 0 deletions apis/dataset_metadata/dataset_access.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""API for dataset access metadata"""

from flask import request
from flask_restx import Resource, fields
from jsonschema import ValidationError, validate

import model
from apis.authentication import is_granted
Expand All @@ -19,11 +22,14 @@

@api.route("/study/<study_id>/dataset/<dataset_id>/metadata/access")
class DatasetAccessResource(Resource):
"""Dataset Access Resource"""

@api.doc("access")
@api.response(200, "Success")
@api.response(400, "Validation Error")
# @api.marshal_with(dataset_access)
def get(self, study_id: int, dataset_id: int): # pylint: disable= unused-argument
"""Get dataset access"""
dataset_ = model.Dataset.query.get(dataset_id)
dataset_access_ = dataset_.dataset_access
return dataset_access_.to_dict()
Expand All @@ -32,10 +38,34 @@ def get(self, study_id: int, dataset_id: int): # pylint: disable= unused-argume
@api.response(200, "Success")
@api.response(400, "Validation Error")
def put(self, study_id: int, dataset_id: int): # pylint: disable= unused-argument
"""Update dataset access"""
study_obj = model.Study.query.get(study_id)

if not is_granted("dataset_metadata", study_obj):
return "Access denied, you can not make any change in dataset metadata", 403

schema = {
"type": "object",
"additionalProperties": False,
"properties": {
"description": {"type": "string", "minLength": 1},
"type": {"type": "string", "minLength": 1},
"url": {"type": "string"},
"url_last_checked": {"type": ["integer", "null"]},
},
"required": [
"description",
"type",
"url",
"url_last_checked",
],
}

try:
validate(instance=request.json, schema=schema)
except ValidationError as err:
return err.message, 400

dataset_ = model.Dataset.query.get(dataset_id)
dataset_.dataset_access.update(request.json)
model.db.session.commit()
Expand Down
59 changes: 58 additions & 1 deletion apis/dataset_metadata/dataset_alternate_identifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""API endpoints for dataset alternate identifier"""
from typing import Any, Union

from flask import request
from flask_restx import Resource, fields
from jsonschema import ValidationError, validate

import model
from apis.authentication import is_granted
Expand All @@ -20,11 +22,14 @@

@api.route("/study/<study_id>/dataset/<dataset_id>/metadata/alternative-identifier")
class DatasetAlternateIdentifierResource(Resource):
"""Dataset Alternate Identifier Resource"""

@api.doc("identifier")
@api.response(200, "Success")
@api.response(400, "Validation Error")
# @api.marshal_with(dataset_identifier)
def get(self, study_id: int, dataset_id: int): # pylint: disable = unused-argument
"""Get dataset alternate identifier"""
dataset_ = model.Dataset.query.get(dataset_id)
dataset_identifier_ = dataset_.dataset_alternate_identifier
return [d.to_dict() for d in dataset_identifier_]
Expand All @@ -33,10 +38,59 @@ def get(self, study_id: int, dataset_id: int): # pylint: disable = unused-argum
@api.response(200, "Success")
@api.response(400, "Validation Error")
def post(self, study_id: int, dataset_id: int):
"""Update dataset alternate identifier"""
study_obj = model.Study.query.get(study_id)

if not is_granted("dataset_metadata", study_obj):
return "Access denied, you can not make any change in dataset metadata", 403
# pylint: disable= unused-argument

schema = {
"type": "array",
"items": {
"type": "object",
"additionalProperties": False,
"properties": {
"id": {"type": "string"},
"identifier": {
"type": "string",
"minLength": 1,
},
"type": {
"type": "string",
"enum": [
"ARK",
"arXiv",
"bibcode",
"DOI",
"EAN13",
"EISSN",
"Handle",
"IGSN",
"ISBN",
"ISSN",
"ISTC",
"LISSN",
"LSID",
"PMID",
"PURL",
"UPC",
"URL",
"URN",
"w3id",
"Other",
],
},
},
"required": ["identifier", "type"],
},
"uniqueItems": True,
}

try:
validate(request.json, schema)
except ValidationError as e:
return e.message, 400

data: Union[Any, dict] = request.json
data_obj = model.Dataset.query.get(dataset_id)
list_of_elements = []
Expand All @@ -63,12 +117,15 @@ def post(self, study_id: int, dataset_id: int):
"metadata/alternative-identifier/<identifier_id>"
)
class DatasetAlternateIdentifierUpdate(Resource):
"""Dataset Alternate Identifier Update Resource"""

@api.doc("delete identifier")
@api.response(200, "Success")
@api.response(400, "Validation Error")
def delete(
self, study_id: int, dataset_id: int, identifier_id: int
): # pylint: disable= unused-argument
"""Delete dataset alternate identifier"""
dataset_identifier_ = model.DatasetAlternateIdentifier.query.get(
identifier_id
)
Expand Down
38 changes: 38 additions & 0 deletions apis/dataset_metadata/dataset_consent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""API for dataset consent metadata"""
from flask import request
from flask_restx import Resource, fields
from jsonschema import ValidationError, validate

import model
from apis.authentication import is_granted
Expand All @@ -22,11 +24,14 @@

@api.route("/study/<study_id>/dataset/<dataset_id>/metadata/consent")
class DatasetConsentResource(Resource):
"""Dataset Consent Resource"""

@api.doc("consent")
@api.response(200, "Success")
@api.response(400, "Validation Error")
# @api.marshal_with(dataset_consent)
def get(self, study_id: int, dataset_id: int): # pylint: disable= unused-argument
"""Get dataset consent"""
dataset_ = model.Dataset.query.get(dataset_id)
dataset_consent_ = dataset_.dataset_consent
return dataset_consent_.to_dict()
Expand All @@ -35,9 +40,42 @@ def get(self, study_id: int, dataset_id: int): # pylint: disable= unused-argume
@api.response(200, "Success")
@api.response(400, "Validation Error")
def put(self, study_id: int, dataset_id: int):
"""Update dataset consent"""
study_obj = model.Study.query.get(study_id)

if not is_granted("dataset_metadata", study_obj):
return "Access denied, you can not make any change in dataset metadata", 403

schema = {
"type": "object",
"additionalProperties": False,
"properties": {
"type": {"type": "string", "minLength": 1},
"details": {
"type": "string",
},
"genetic_only": {"type": "boolean"},
"geog_restrict": {"type": "boolean"},
"no_methods": {"type": "boolean"},
"noncommercial": {"type": "boolean"},
"research_type": {"type": "boolean"},
},
"required": [
"type",
"details",
"genetic_only",
"geog_restrict",
"no_methods",
"noncommercial",
"research_type",
],
}

try:
validate(instance=request.json, schema=schema)
except ValidationError as err:
return err.message, 400

data = request.json
dataset_ = model.Dataset.query.get(dataset_id)
dataset_.dataset_consent.update(data)
Expand Down
Loading

0 comments on commit de6a17e

Please sign in to comment.