Commit 59f518da authored by hypsug0's avatar hypsug0
Browse files

feat: add permissions to Sighting Edit API

parent 71fb597c
......@@ -24,6 +24,7 @@
},
"editor.formatOnSave": true
},
"autoDocstring.docstringFormat": "sphinx",
"editor.formatOnSave": false,
"sqltools.connections": [
{
......
......@@ -17,6 +17,8 @@ from sights.observation.serializers import (
GeoSightingSerializer,
)
from .permissions import SightingEditPermission
# from rest_framework.permissions import IsAuthenticated
......@@ -49,8 +51,9 @@ class GeoJSONSighting(
class EditSighting(ModelViewSet):
serializer_class = EditSightingSerializerTest
permission_class = [
permission_classes = [
IsAuthenticated,
SightingEditPermission,
]
def get_queryset(self):
......@@ -62,11 +65,9 @@ class EditSighting(ModelViewSet):
return qs
def perform_create(self, serializer):
logger.debug(f"<perform_create>\nSELF: {self}\nSERIALIZER{serializer}")
serializer.save(created_by=self.request.user)
def perform_update(self, serializer):
logger.debug(f"<perform_update>\nSELF: {self}\nSERIALIZER{serializer}")
serializer.save(updated_by=self.request.user)
......
import logging
from rest_framework.permissions import BasePermission
logger = logging.getLogger(__name__)
logger.debug(f"type BasePermission {type(BasePermission)}")
class SightingEditPermission(BasePermission):
"""Sighting Edit API permissions
:param BasePermission: [description]
:type BasePermission: [type]
"""
def has_permission(self, request, view) -> bool:
"""[summary]
:param request: [description]
:type request: [type]
:param view: [description]
:type view: [type]
:return: [description]
:rtype: [type]
"""
logger.debug(f"type self {type(self)}")
logger.debug(f"type request {type(request)}")
logger.debug(f"type view {type(view)}")
if view.action in ["list", "create", "retrieve", "update", "partial_update", "destroy"]:
return request.user.is_authenticated
else:
return False
def has_object_permission(self, request, view, obj) -> bool:
user = request.user
logger.debug(f"Action: {view.action}")
logger.debug(f"type self {type(self)}")
logger.debug(f"type request {type(request)}")
logger.debug(f"type view {type(view)}")
logger.debug(f"type obj {type(obj)}")
# Deny actions on objects if the user is not authenticated
if not user.is_authenticated:
return False
if view.action in ["retrieve", "update", "partial_update", "destroy"]:
logger.debug(f"cond: user == obj.created_by > {user == obj.created_by}")
logger.debug(
f"cond: user == obj.session.created_by > {user == obj.session.created_by}"
)
logger.debug(f"cond: user.edit_all_data > {user.edit_all_data}")
logger.debug(f"cond: user.is_staff > {user.is_staff}")
logger.debug(
f"cond: (user.is_resp and obj.session.place.territory in user.resp_territory.all()) > {(user.is_resp and obj.session.place.territory in user.resp_territory.all())}"
)
return (
user == obj.created_by
or user == obj.session.created_by
or user.is_staff
or user.edit_all_data
or (user.is_resp and obj.session.place.territory in user.resp_territory.all())
)
else:
return False
......@@ -294,7 +294,7 @@ class SightingSearch(LoginRequiredMixin, TemplateView):
context = super(SightingSearch, self).get_context_data(**kwargs)
enable_territory = Territory.objects.filter(coverage=True).all()
print(enable_territory)
context['enable_territory']=enable_territory
context["enable_territory"] = enable_territory
context["icon"] = "fa fa-fw fa-eye"
context["title"] = _("Rechercher une observation")
context[
......
......@@ -23,7 +23,6 @@ class GeoJSONPlaces(
def get_queryset(self):
qs = super(GeoJSONPlaces, self).get_queryset()
logger.debug(f"PLaceSearch API {qs}")
return qs.order_by("-timestamp_update").all()
......
......@@ -342,7 +342,7 @@ Observations de la session {{object.name}}
<div v-if="form_fields.includes('gland_coul')" class="form-group">
<label class="control-label" for="GlandCoul">Couleur des glandes salivaires</label>
<dict-select :options="filteredDict(dicts.BiomGlandCoul)" v-model="cd.gland_coul"/>
</div>form-inline
</div>
<div v-if="form_fields.includes('gland_taille')" class="form-group">
<label class="control-label" for="GlandTaille">Taille des glandes salivaires</label>
<dict-select :options="filteredDict(dicts.BiomGlandTaille)" v-model="cd.gland_taille"/>
......@@ -366,25 +366,23 @@ Observations de la session {{object.name}}
<dict-select :options="filteredDict(dicts.BiomChinspot)" v-model="cd.chinspot"/>
</div>
<div v-if="form_fields.includes('usure_dent')" class="form-group">
<label class="control-label" for="SecualStatus">Usure des dents</label>
<dict-select :options="filteredDict(dicts.BiomDent)" v-model="cd.usure_dent"/>
</div>
<span @click="moreCountDetailForm = !moreCountDetailForm"><i class="fas fa-2x"
:class="moreCountDetailForm ? 'fa-caret-up ':'fa-caret-down'"></i></span>
<div id="moreCountDetailForm" v-if="moreCountDetailForm">
<div class="checkbox form-inline">
<label class="control-label" for="determiner">Déterminateur</label>
<user-select-one id="determiner" v-model="cd.manipulator"/>
<label class="control-label" Vuejs Dynamic Dropdown Select Menu using json
<user-select-one id="determiner" class="form-control" v-model="cd.manipulator"/>
</div>
</div>
<div class="checkbox form-inline">
<label class="control-label" for="determiner">Validateur</label>
<user-select-one id="determiner" v-model="cd.validator"/>
<div>
<label class="col-sm-2 control-label" for="determiner">Validateur</label>
<div class="col-sm-10">
<user-select-one id="determiner" class="form-control" v-model="cd.validator"/>
</div>
</div>
<div class="form-group">
<label for="cdComment">Commentaire</label>
<div>
<label class="col-sm-2 control-label" for="cdComment">Commentaire</label>
<div class="col-sm-10">
<textarea id="cdComment" placeholder="Commentaire"
v-model:title="cd.comment" v-model:content="cd.comment"
class="form-control" rows="3"></textarea>
class="form-control" style="width:100%" rows="3"></textarea>
</div>
</div>
</div>
<div class="btn btn-danger btn-sm pull-right" @click="delCountDetail(selectedItem,icd)" tooltip><i
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment