Skip to content

Commit

Permalink
feat: Implement anonymous datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Jul 16, 2024
1 parent 9f9ddd8 commit 630915f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ __pycache__/
.idea
uploads/
app.log
app.log.*
.DS_Store
rosemary.egg-info/
build/
Expand Down
7 changes: 7 additions & 0 deletions app/modules/dataset/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,12 @@ def convert_publication_type(self, value):
def get_authors(self):
return [author.get_author() for author in self.authors]

def get_anonymous_authors(self):
return [{
"name": "Anonymous",
"affiliation": "Anonymous",
"orcid": ""
}]

def get_feature_models(self):
return [fm.get_feature_model() for fm in self.feature_models]
26 changes: 13 additions & 13 deletions app/modules/dataset/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
from enum import Enum
from typing import List

from flask import request
from sqlalchemy import Boolean, Enum as SQLAlchemyEnum
Expand Down Expand Up @@ -66,7 +67,7 @@ class DSMetaData(db.Model):
ds_metrics_id = db.Column(db.Integer, db.ForeignKey('ds_metrics.id'))
ds_metrics = db.relationship('DSMetrics', uselist=False, backref='ds_meta_data', cascade="all, delete")
authors = db.relationship('Author', backref='ds_meta_data', lazy=True, cascade="all, delete")
dataset_anonymous = db.Column(Boolean, default=False)
dataset_anonymous = db.Column(Boolean, default=False)


class DataSet(db.Model):
Expand All @@ -79,36 +80,35 @@ class DataSet(db.Model):
ds_meta_data = db.relationship('DSMetaData', backref=db.backref('data_set', uselist=False))
feature_models = db.relationship('FeatureModel', backref='data_set', lazy=True, cascade="all, delete")

def name(self):
def name(self) -> str:
return self.ds_meta_data.title

def files(self):
def files(self) -> List[any]:
return [file for fm in self.feature_models for file in fm.files]

def delete(self):
db.session.delete(self)
db.session.commit()

def get_cleaned_publication_type(self):
def get_cleaned_publication_type(self) -> str:
return self.ds_meta_data.publication_type.name.replace('_', ' ').title()

def get_zenodo_url(self):
def get_zenodo_url(self) -> str:
return f'https://zenodo.org/record/{self.ds_meta_data.deposition_id}' if self.ds_meta_data.dataset_doi else None

def get_files_count(self):
def get_files_count(self) -> int:
return sum(len(fm.files) for fm in self.feature_models)

def get_file_total_size(self):
def get_file_total_size(self) -> int:
return sum(file.size for fm in self.feature_models for file in fm.files)

def get_file_total_size_for_human(self):
def get_file_total_size_for_human(self) -> str:
from app.modules.dataset.services import SizeService
return SizeService().get_human_readable_size(self.get_file_total_size())

def get_uvlhub_doi(self):
def get_uvlhub_doi(self) -> str:
from app.modules.dataset.services import DataSetService
return DataSetService().get_uvlhub_doi(self)

def is_anonymous(self) -> bool:
return self.ds_meta_data.dataset_anonymous

def to_dict(self):
return {
'title': self.ds_meta_data.title,
Expand Down
14 changes: 12 additions & 2 deletions app/modules/dataset/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from flask import request

from app.modules.auth.services import AuthenticationService
from app.modules.dataset.forms import DataSetForm
from app.modules.dataset.models import DSViewRecord, DataSet, DSMetaData
from app.modules.dataset.repositories import (
AuthorRepository,
Expand Down Expand Up @@ -92,7 +93,7 @@ def total_dataset_downloads(self) -> int:
def total_dataset_views(self) -> int:
return self.dsviewrecord_repostory.total_dataset_views()

def create_from_form(self, form, current_user) -> DataSet:
def create_from_form(self, form: DataSetForm, current_user) -> DataSet:
main_author = {
"name": f"{current_user.profile.surname}, {current_user.profile.name}",
"affiliation": current_user.profile.affiliation,
Expand All @@ -101,7 +102,16 @@ def create_from_form(self, form, current_user) -> DataSet:
try:
logger.info(f"Creating dsmetadata...: {form.get_dsmetadata()}")
dsmetadata = self.dsmetadata_repository.create(**form.get_dsmetadata())
for author_data in [main_author] + form.get_authors():

dsmetadata_info = form.get_dsmetadata()
is_anonymous = dsmetadata_info.get('dataset_anonymous', False)

if is_anonymous:
author_list = form.get_anonymous_authors()
else:
author_list = [main_author] + form.get_authors()

for author_data in author_list:
author = self.author_repository.create(commit=False, ds_meta_data_id=dsmetadata.id, **author_data)
dsmetadata.authors.append(author)

Expand Down
4 changes: 4 additions & 0 deletions app/modules/dataset/templates/dataset/view_dataset.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ <h1><b>{{ dataset.ds_meta_data.title }}</b></h1>

</div>

{% if not dataset.is_anonymous() %}

<div class="row mb-2">

<div class="col-md-4 col-12">
Expand All @@ -69,6 +71,8 @@ <h1><b>{{ dataset.ds_meta_data.title }}</b></h1>

</div>

{% endif %}

<div class="row mb-2">

<div class="col-md-4 col-12">
Expand Down
5 changes: 4 additions & 1 deletion app/modules/zenodo/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def test_full_connection(self) -> Response:
return jsonify(
{
"success": False,
"messages": f"Failed to create test deposition on Zenodo. Response code: {response.status_code}",
"messages": (
f"Failed to create test deposition on Zenodo. "
f"Response code: {response.status_code}, {response.json()}"
),
}
)

Expand Down

0 comments on commit 630915f

Please sign in to comment.