Skip to content

Commit

Permalink
Merge pull request #138 from PnX-SI/workshop_remove_repository
Browse files Browse the repository at this point in the history
Workshop remove repository
  • Loading branch information
TheoLechemia authored May 10, 2023
2 parents 1361294 + 0f3ff73 commit 2d04753
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 298 deletions.
93 changes: 47 additions & 46 deletions backend/gn_module_export/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from flask_admin.contrib.sqla import ModelView
from flask_admin.helpers import is_form_submitted
from flask_admin.babel import gettext
from werkzeug.exceptions import NotFound, BadRequest
from werkzeug.exceptions import NotFound, BadRequest, Forbidden
from wtforms import validators

from pypnusershub.db.models import User
Expand All @@ -46,10 +46,10 @@


import gn_module_export.tasks # noqua: F401
from .repositories import ExportObjectQueryRepository, generate_swagger_spec
from .repositories import generate_swagger_spec
from .models import Export, CorExportsRoles, Licences, ExportSchedules, UserRepr
from .utils_export import thread_export_data
from .commands import commands
from .tasks import generate_export

LOGGER = current_app.logger
LOGGER.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -374,54 +374,43 @@ def getOneExportThread(id_export, export_format):
"""
Run export with thread
"""
# Test if export exists
if (
id_export < 1
or export_format not in current_app.config["EXPORTS"]["export_format_map"]
):

filters = {f: request.args.get(f) for f in request.args}
data = dict(request.get_json())
user = g.current_user

# Test format
if export_format not in current_app.config["EXPORTS"]["export_format_map"]:
return to_json_resp(
{
"api_error": "invalid_export",
"message": "Invalid export or export not found",
"message": "Invalid export format",
},
status=404,
status=500,
)

filters = {f: request.args.get(f) for f in request.args}
data = dict(request.get_json())
export = Export.query.get(id_export)
if not export:
return jsonify([])

if not export.has_instance_permission(user.id_role):
raise Forbidden

# Test email
# Alternative email in payload
email_to = None
if "email" in data:
email_to = data["email"]

@copy_current_request_context
def get_data(id_export, export_format, role, filters, email_to):
thread_export_data(id_export, export_format, role, filters, email_to)

exp = ExportObjectQueryRepository(id_export=id_export, role=g.current_user)

# Test if user have an email
try:
user = g.current_user
if not user.email and not email_to: # TODO add more test
raise BadRequest("User doesn't have email")
except NoResultFound:
raise NotFound("User doesn't exist")

# Run export
a = threading.Thread(
name="export_data",
target=get_data,
kwargs={
"id_export": id_export,
"export_format": export_format,
"role": g.current_user,
"filters": filters,
"email_to": [email_to] if (email_to) else [user.email],
},
if not user.email and not email_to: # TODO add more test
raise BadRequest("User doesn't have email")

generate_export.delay(
export_id=id_export,
export_format=export_format,
scheduled=False,
skip_newer_than=None,
)
a.start()

return to_json_resp(
{
Expand Down Expand Up @@ -509,24 +498,36 @@ def get_one_export_api(id_export):
order by : @TODO
"""

user = g.current_user
export = Export.query.get(id_export)

if not export:
return jsonify([])
if not export.has_instance_permission(user.id_role):
raise Forbidden

limit = request.args.get("limit", default=1000, type=int)
offset = request.args.get("offset", default=0, type=int)

if limit > 1000:
limit = 1000

args = request.args.to_dict()
if "limit" in args:
args.pop("limit")
if "offset" in args:
args.pop("offset")
filters = {f: args.get(f) for f in args}

exprep = ExportObjectQueryRepository(
id_export=id_export,
role=g.current_user,
filters=filters,
limit=limit,
offset=offset,
)
data = exprep.get_export_with_logging()
query = export.get_view_query(limit=limit, offset=offset, filters=filters)

data = query.return_query()

export_license = (export.as_dict(fields=["licence"])).get("licence", None)
data["license"] = dict()
data["license"]["name"] = export_license.get("name_licence", None)
data["license"]["href"] = export_license.get("url_licence", None)

return data


Expand Down
60 changes: 44 additions & 16 deletions backend/gn_module_export/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from geonature.utils.env import DB
from utils_flask_sqla.serializers import serializable

from utils_flask_sqla_geo.generic import GenericQueryGeo

from pypnusershub.db.models import User
from geonature.core.users.models import CorRole

Expand Down Expand Up @@ -82,14 +84,55 @@ class Export(DB.Model):
id_licence = DB.Column(
DB.Integer(), DB.ForeignKey(Licences.id_licence), nullable=False
)
allowed_roles = DB.relationship("CorExportsRoles")

licence = DB.relationship("Licences")

def __str__(self):
return "{}".format(self.label)

__repr__ = __str__

def has_instance_permission(self, id_role=None):
if self.public:
return True

user = User.query.get(id_role)
if not user:
return False
if user in self.allowed_roles:
return True

return False

def get_view_query(self, limit, offset, filters=None):
return GenericQueryGeo(
DB,
self.view_name,
self.schema_name,
filters,
limit,
offset,
self.geometry_field,
)


class CorExportsRoles(DB.Model):
__tablename__ = "cor_exports_roles"
__table_args__ = {"schema": "gn_exports"}
id_export = DB.Column(
DB.Integer, DB.ForeignKey(Export.id), primary_key=True, nullable=False
)

id_role = DB.Column(
DB.Integer, DB.ForeignKey(User.id_role), primary_key=True, nullable=False
)

# export = DB.relationship("Export", cascade="all,delete")
# role = DB.relationship("UserRepr")


Export.allowed_roles = DB.relationship(User, secondary=CorExportsRoles.__table__)


@serializable
class ExportLog(DB.Model):
Expand Down Expand Up @@ -117,21 +160,6 @@ def record(cls, adict):
DB.session.rollback()


class CorExportsRoles(DB.Model):
__tablename__ = "cor_exports_roles"
__table_args__ = {"schema": "gn_exports"}
id_export = DB.Column(
DB.Integer(), DB.ForeignKey(Export.id), primary_key=True, nullable=False
)

id_role = DB.Column(
DB.Integer, DB.ForeignKey(User.id_role), primary_key=True, nullable=False
)

export = DB.relationship("Export", lazy="joined", cascade="all,delete")
role = DB.relationship("UserRepr", lazy="joined")


class ExportSchedules(DB.Model):
__tablename__ = "t_export_schedules"
__table_args__ = {"schema": "gn_exports"}
Expand Down
Loading

0 comments on commit 2d04753

Please sign in to comment.