Skip to content

Commit

Permalink
feat: Improve dataset url
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Jul 8, 2024
1 parent 89a894a commit 633e42a
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 61 deletions.
7 changes: 3 additions & 4 deletions app/modules/dataset/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from datetime import datetime
from enum import Enum

Expand Down Expand Up @@ -105,8 +104,8 @@ def get_file_total_size_for_human(self):
return get_human_readable_size(self.get_file_total_size())

def get_uvlhub_doi(self):
domain = os.getenv('DOMAIN', 'localhost')
return f'http://{domain}/doi/{self.ds_meta_data.dataset_doi}'
from app.modules.dataset.services import DataSetService
return DataSetService().get_uvlhub_doi(self)

def to_dict(self):
return {
Expand All @@ -120,7 +119,7 @@ def to_dict(self):
'publication_doi': self.ds_meta_data.publication_doi,
'dataset_doi': self.ds_meta_data.dataset_doi,
'tags': self.ds_meta_data.tags.split(",") if self.ds_meta_data.tags else [],
'url': f'{request.host_url.rstrip("/")}/dataset/view/{self.id}',
'url': self.get_uvlhub_doi(),
'download': f'{request.host_url.rstrip("/")}/dataset/download/{self.id}',
'zenodo': self.get_zenodo_url(),
'files': [file.to_dict() for fm in self.feature_models for file in fm.files],
Expand Down
71 changes: 21 additions & 50 deletions app/modules/dataset/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,38 +389,6 @@ def download_dataset(dataset_id):
return resp


@dataset_bp.route("/dataset/view/<int:dataset_id>", methods=["GET"])
def view_dataset(dataset_id):
dataset = dataset_service.get_or_404(dataset_id)

# Get the cookie from the request or generate a new one if it does not exist
user_cookie = request.cookies.get("view_cookie")
if not user_cookie:
user_cookie = str(uuid.uuid4())

# Check if the view record already exists for this cookie
existing_record = DSViewRecord.query.filter_by(
user_id=current_user.id if current_user.is_authenticated else None,
dataset_id=dataset_id,
view_cookie=user_cookie
).first()

if not existing_record:
# Record the view in your database
DSViewRecordService().create(
user_id=current_user.id if current_user.is_authenticated else None,
dataset_id=dataset_id,
view_date=datetime.now(timezone.utc),
view_cookie=user_cookie,
)

# Save the cookie to the user's browser
resp = make_response(render_template("dataset/view_dataset.html", dataset=dataset))
resp.set_cookie("view_cookie", user_cookie)

return resp


@dataset_bp.route("/file/download/<int:file_id>", methods=["GET"])
def download_file(file_id):
file = FileService().get_or_404(file_id)
Expand Down Expand Up @@ -511,34 +479,37 @@ def view_file(file_id):

@dataset_bp.route("/doi/<path:doi>/", methods=["GET"])
def subdomain_index(doi):
# Busca el dataset por DOI

ds_meta_data = dsmetadata_service.filter_by_doi(doi)

if not ds_meta_data:
abort(404)

dataset = ds_meta_data.data_set
if dataset:
dataset_id = dataset.id
user_cookie = request.cookies.get("view_cookie", str(uuid.uuid4()))

# Registra la vista del dataset
# Get the cookie from the request or generate a new one if it does not exist
user_cookie = request.cookies.get("view_cookie")
if not user_cookie:
user_cookie = str(uuid.uuid4())

# Check if the view record already exists for this cookie
existing_record = DSViewRecord.query.filter_by(
user_id=current_user.id if current_user.is_authenticated else None,
dataset_id=dataset.id,
view_cookie=user_cookie
).first()

if not existing_record:
# Record the view in your database
DSViewRecordService().create(
user_id=current_user.id if current_user.is_authenticated else None,
dataset_id=dataset_id,
dataset_id=dataset.id,
view_date=datetime.now(timezone.utc),
view_cookie=user_cookie,
)

# Prepara la respuesta y establece la cookie
resp = make_response(
render_template("dataset/view_dataset.html", dataset=dataset)
)
resp.set_cookie(
"view_cookie", user_cookie, max_age=30 * 24 * 60 * 60
) # Ejemplo: cookie expira en 30 días
# Save the cookie to the user's browser
resp = make_response(render_template("dataset/view_dataset.html", dataset=dataset))
resp.set_cookie("view_cookie", user_cookie)

return resp
else:
# Aquí puedes manejar el caso de que el DOI no corresponda a un dataset existente
# Por ejemplo, mostrar un error 404 o redirigir a una página de error
return "Dataset not found", 404
return resp
5 changes: 5 additions & 0 deletions app/modules/dataset/services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Optional

from app.modules.dataset.models import DataSet, DSMetaData
Expand Down Expand Up @@ -63,6 +64,10 @@ def total_dataset_views(self) -> int:
def total_feature_model_views(self) -> int:
return self.fileviewrecord_repository.total_feature_model_views()

def get_uvlhub_doi(self, dataset: DataSet) -> str:
domain = os.getenv('DOMAIN', 'localhost')
return f'http://{domain}/doi/{dataset.ds_meta_data.dataset_doi}'


class FeatureModelService(BaseService):
def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions app/modules/dataset/templates/dataset/list_datasets.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h1 class="h3 mb-3">My datasets</h1>
{% for dataset in datasets %}
<tr>
<td>
<a href="{{ url_for('dataset.view_dataset', dataset_id=dataset.id) }}">
<a href="{{ dataset.get_uvlhub_doi() }}">
{{ dataset.ds_meta_data.title }}
</a>
</td>
Expand All @@ -34,7 +34,7 @@ <h1 class="h3 mb-3">My datasets</h1>
<td><a href="{{ dataset.get_uvlhub_doi() }}"
target="_blank">{{ dataset.get_uvlhub_doi() }}</a></td>
<td>
<a href="{{ url_for('dataset.view_dataset', dataset_id=dataset.id) }}">
<a href="{{ dataset.get_uvlhub_doi() }}">
<i data-feather="eye"></i>
</a>
<a href="{{ url_for('dataset.download_dataset', dataset_id=dataset.id) }}">
Expand Down
4 changes: 2 additions & 2 deletions app/modules/explore/templates/explore/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ <h3 class="h3 mb-3">
<div class="card">
<div class="card-body">
<div class="d-flex align-items-center justify-content-between">
<h3><a href="/dataset/view/${dataset.id}">${dataset.title}</a></h3>
<h3><a href="${dataset.url}">${dataset.title}</a></h3>
<div>
<span class="badge bg-primary" style="cursor: pointer;" onclick="set_publication_type_as_query('${dataset.publication_type}')">${dataset.publication_type}</span>
</div>
Expand Down Expand Up @@ -265,7 +265,7 @@ <h3><a href="/dataset/view/${dataset.id}">${dataset.title}</a></h3>
</div>
<div class="col-md-8 col-12">
<a href="/dataset/view/${dataset.id}" class="btn btn-outline-primary btn-sm" id="search" style="border-radius: 5px;">
<a href="${dataset.url}" class="btn btn-outline-primary btn-sm" id="search" style="border-radius: 5px;">
View dataset
</a>
<a href="/dataset/download/${dataset.id}" class="btn btn-outline-primary btn-sm" id="search" style="border-radius: 5px;">
Expand Down
2 changes: 1 addition & 1 deletion app/modules/profile/templates/profile/summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h1 class="h3 mb-3">User datasets</h1>
{% for dataset in datasets %}
<tr>
<td>
<a href="{{ url_for('dataset.view_dataset', dataset_id=dataset.id) }}">
<a href="{{ dataset.get_uvlhub_doi() }}">
{{ dataset.ds_meta_data.title }}
</a>
</td>
Expand Down
4 changes: 2 additions & 2 deletions app/modules/public/templates/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h1 class="h2 mb-3">
<div class="d-flex align-items-center justify-content-between">
<h2>

<a href="/dataset/view/{{ dataset.id }}">
<a href="{{ dataset.get_uvlhub_doi() }}">
{{ dataset.ds_meta_data.title }}
</a>

Expand Down Expand Up @@ -86,7 +86,7 @@ <h2>

<div class="row mt-4">
<div class="col-12">
<a href="/dataset/view/{{ dataset.id }}" class="btn btn-outline-primary btn-sm"
<a href="{{ dataset.get_uvlhub_doi() }}" class="btn btn-outline-primary btn-sm"
style="border-radius: 5px;">
<i data-feather="eye" class="center-button-icon"></i>
View dataset
Expand Down

0 comments on commit 633e42a

Please sign in to comment.