2021-07-01 13:36:07 +02:00
from django . contrib import messages
from django . contrib . auth . decorators import login_required
from django . utils . translation import gettext_lazy as _
from django . http import HttpRequest
2021-07-01 15:08:22 +02:00
from django . shortcuts import render , get_object_or_404
2021-07-01 13:36:07 +02:00
2021-08-04 15:19:06 +02:00
from intervention . forms import NewInterventionForm , EditInterventionForm , ShareInterventionForm , NewRevocationForm , \
2021-08-30 11:29:15 +02:00
RunCheckForm , NewDeductionForm
2021-08-04 13:32:35 +02:00
from intervention . models import Intervention , Revocation
2021-07-01 13:36:07 +02:00
from intervention . tables import InterventionTable
from konova . contexts import BaseContext
from konova . decorators import *
2021-08-26 15:45:24 +02:00
from konova . forms import SimpleGeomForm , NewDocumentForm , RemoveModalForm , RecordModalForm
2021-08-10 13:57:03 +02:00
from konova . sub_settings . django_settings import DEFAULT_DATE_FORMAT
2021-08-10 17:19:42 +02:00
from konova . utils . message_templates import FORM_INVALID , INTERVENTION_INVALID
2021-08-02 16:23:29 +02:00
from konova . utils . user_checks import in_group
2021-07-01 13:36:07 +02:00
@login_required
2021-08-03 09:45:41 +02:00
@any_group_check
2021-07-01 13:36:07 +02:00
def index_view ( request : HttpRequest ) :
"""
2021-07-01 15:08:22 +02:00
Renders the index view for Interventions
2021-07-01 13:36:07 +02:00
Args :
request ( HttpRequest ) : The incoming request
Returns :
A rendered view
"""
template = " generic_index.html "
2021-07-22 10:00:59 +02:00
# Filtering by user access is performed in table filter inside of InterventionTableFilter class
2021-07-20 14:05:44 +02:00
interventions = Intervention . objects . filter (
2021-08-02 11:52:20 +02:00
deleted = None , # not deleted
2021-07-20 14:05:44 +02:00
)
2021-07-01 13:36:07 +02:00
table = InterventionTable (
request = request ,
queryset = interventions
)
context = {
" table " : table ,
}
context = BaseContext ( request , context ) . context
return render ( request , template , context )
@login_required
2021-08-02 16:23:29 +02:00
@default_group_required
2021-07-01 13:36:07 +02:00
def new_view ( request : HttpRequest ) :
"""
Renders a view for a new intervention creation
Args :
request ( HttpRequest ) : The incoming request
Returns :
"""
template = " konova/form.html "
form = NewInterventionForm ( request . POST or None )
if request . method == " POST " :
if form . is_valid ( ) :
intervention = form . save ( request . user )
2021-07-01 15:08:22 +02:00
messages . success ( request , _ ( " Intervention {} added " ) . format ( intervention . title ) )
return redirect ( " intervention:index " )
2021-07-01 13:36:07 +02:00
else :
messages . error ( request , _ ( " Invalid input " ) )
else :
# For clarification: nothing in this case
pass
context = {
" form " : form ,
}
context = BaseContext ( request , context ) . context
return render ( request , template , context )
2021-07-26 15:16:16 +02:00
@login_required
def new_document_view ( request : HttpRequest , id : str ) :
""" Renders a form for uploading new documents
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : The intervention ' s id to which the new document will be related
Returns :
"""
2021-07-26 15:34:30 +02:00
intervention = get_object_or_404 ( Intervention , id = id )
form = NewDocumentForm ( request . POST or None , request . FILES or None , instance = intervention , user = request . user )
2021-08-04 15:19:06 +02:00
return form . process_request (
request ,
msg_success = _ ( " Document added " )
)
2021-07-26 15:16:16 +02:00
2021-07-01 13:36:07 +02:00
@login_required
2021-08-03 09:45:41 +02:00
@any_group_check
2021-07-01 13:36:07 +02:00
def open_view ( request : HttpRequest , id : str ) :
2021-07-22 13:19:14 +02:00
""" Renders a detail view for viewing an intervention ' s data
2021-07-01 13:36:07 +02:00
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : The intervention ' s id
Returns :
"""
2021-07-26 14:35:19 +02:00
template = " intervention/detail/view.html "
2021-08-02 10:53:34 +02:00
# Fetch data, filter out deleted related data
2021-07-01 13:36:07 +02:00
intervention = get_object_or_404 ( Intervention , id = id )
2021-08-02 10:53:34 +02:00
compensations = intervention . compensations . filter (
2021-08-02 11:52:20 +02:00
deleted = None ,
2021-08-02 10:53:34 +02:00
)
2021-08-02 16:23:29 +02:00
_user = request . user
is_data_shared = intervention . is_shared_with ( user = _user )
2021-07-22 13:19:14 +02:00
geom_form = SimpleGeomForm (
instance = intervention
)
2021-08-04 13:32:35 +02:00
# Inform user about revocation
if intervention . legal . revocation :
messages . error (
request ,
_ ( " This intervention has a revocation from {} " ) . format ( intervention . legal . revocation . date . strftime ( DEFAULT_DATE_FORMAT ) ) ,
extra_tags = " danger " ,
)
2021-07-01 13:36:07 +02:00
context = {
" intervention " : intervention ,
2021-08-02 10:53:34 +02:00
" compensations " : compensations ,
2021-08-02 16:23:29 +02:00
" has_access " : is_data_shared ,
2021-07-22 13:19:14 +02:00
" geom_form " : geom_form ,
2021-08-04 08:41:21 +02:00
" is_default_member " : in_group ( _user , DEFAULT_GROUP ) ,
" is_zb_member " : in_group ( _user , ZB_GROUP ) ,
" is_ets_member " : in_group ( _user , ETS_GROUP ) ,
2021-08-10 13:57:03 +02:00
" LANIS_LINK " : intervention . get_LANIS_link ( )
2021-07-01 13:36:07 +02:00
}
2021-07-22 16:06:13 +02:00
2021-08-02 16:23:29 +02:00
if not is_data_shared :
2021-07-22 16:06:13 +02:00
messages . info ( request , _ ( " Remember: This data has not been shared with you, yet. This means you can only read but can not edit or perform any actions like running a check or recording. " ) )
2021-07-01 13:36:07 +02:00
context = BaseContext ( request , context ) . context
return render ( request , template , context )
@login_required
def edit_view ( request : HttpRequest , id : str ) :
"""
Renders a view for editing interventions
Args :
request ( HttpRequest ) : The incoming request
Returns :
"""
template = " konova/form.html "
intervention = get_object_or_404 ( Intervention , id = id )
if request . method == " POST " :
form = EditInterventionForm ( request . POST or None , instance = intervention )
if form . is_valid ( ) :
intervention = form . save ( request . user )
messages . success ( request , _ ( " {} edited " ) . format ( intervention ) )
return redirect ( " intervention:index " )
else :
messages . error ( request , _ ( " Invalid input " ) )
form = EditInterventionForm ( instance = intervention )
context = {
" form " : form ,
}
context = BaseContext ( request , context ) . context
return render ( request , template , context )
@login_required
2021-08-02 16:23:29 +02:00
@default_group_required
2021-07-01 13:36:07 +02:00
def remove_view ( request : HttpRequest , id : str ) :
2021-07-01 15:08:22 +02:00
""" Renders a remove view for this intervention
2021-07-01 13:36:07 +02:00
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : The uuid id as string
Returns :
"""
2021-07-01 15:08:22 +02:00
obj = Intervention . objects . get ( id = id )
2021-08-02 15:39:33 +02:00
identifier = obj . identifier
form = RemoveModalForm ( request . POST or None , instance = obj , user = request . user )
return form . process_request (
request ,
_ ( " {} removed " ) . format ( identifier ) ,
redirect_url = reverse ( " intervention:index " )
2021-07-01 13:36:07 +02:00
)
2021-07-30 13:30:42 +02:00
2021-08-04 13:32:35 +02:00
@login_required
@default_group_required
def remove_revocation_view ( request : HttpRequest , id : str ) :
""" Renders a remove view for a revocation
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : The revocation ' s id as string
Returns :
"""
obj = Revocation . objects . get ( id = id )
form = RemoveModalForm ( request . POST or None , instance = obj , user = request . user )
return form . process_request (
request ,
_ ( " Revocation removed " ) ,
)
2021-07-30 13:30:42 +02:00
@login_required
def share_view ( request : HttpRequest , id : str , token : str ) :
""" Performs sharing of an intervention
If token given in url is not valid , the user will be redirected to the dashboard
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : Intervention ' s id
token ( str ) : Access token for intervention
Returns :
"""
user = request . user
intervention = get_object_or_404 ( Intervention , id = id )
# Check tokens
if intervention . access_token == token :
# Send different messages in case user has already been added to list of sharing users
2021-08-02 16:23:29 +02:00
if intervention . is_shared_with ( user ) :
2021-07-30 13:30:42 +02:00
messages . info (
request ,
_ ( " {} has already been shared with you " ) . format ( intervention . identifier )
)
else :
messages . success (
request ,
_ ( " {} has been shared with you " ) . format ( intervention . identifier )
)
intervention . users . add ( user )
return redirect ( " intervention:open " , id = id )
else :
messages . error (
request ,
_ ( " Share link invalid " ) ,
extra_tags = " danger " ,
)
return redirect ( " home " )
@login_required
def create_share_view ( request : HttpRequest , id : str ) :
""" Renders sharing form for an intervention
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : Intervention ' s id
Returns :
"""
intervention = get_object_or_404 ( Intervention , id = id )
form = ShareInterventionForm ( request . POST or None , instance = intervention , request = request )
2021-08-19 13:02:31 +02:00
return form . process_request (
request ,
msg_success = _ ( " Share settings updated " )
)
2021-07-30 13:30:42 +02:00
2021-08-04 15:19:06 +02:00
@login_required
def run_check_view ( request : HttpRequest , id : str ) :
""" Renders check form for an intervention
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : Intervention ' s id
Returns :
"""
intervention = get_object_or_404 ( Intervention , id = id )
form = RunCheckForm ( request . POST or None , instance = intervention , user = request . user )
2021-08-10 17:19:42 +02:00
return form . process_request (
request ,
msg_success = _ ( " Check performed " ) ,
msg_error = INTERVENTION_INVALID
)
2021-08-04 15:19:06 +02:00
2021-08-04 13:32:35 +02:00
@login_required
def new_revocation_view ( request : HttpRequest , id : str ) :
""" Renders sharing form for an intervention
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : Intervention ' s id
Returns :
"""
intervention = get_object_or_404 ( Intervention , id = id )
form = NewRevocationForm ( request . POST or None , request . FILES or None , instance = intervention , user = request . user )
2021-08-19 13:02:31 +02:00
return form . process_request (
request ,
msg_success = _ ( " Revocation added " )
)
2021-08-04 13:32:35 +02:00
2021-08-05 12:54:28 +02:00
@login_required
def log_view ( request : HttpRequest , id : str ) :
""" Renders a log view using modal
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : The compensation ' s id
Returns :
"""
intervention = get_object_or_404 ( Intervention , id = id )
template = " modal/modal_generic.html "
body_template = " log.html "
context = {
" modal_body_template " : body_template ,
2021-08-19 13:02:31 +02:00
" log " : intervention . log . all ( ) ,
2021-08-05 12:54:28 +02:00
" modal_title " : _ ( " Log " ) ,
}
context = BaseContext ( request , context ) . context
return render ( request , template , context )
2021-08-10 10:42:04 +02:00
@login_required
@default_group_required
2021-08-30 11:29:15 +02:00
def new_deduction_view ( request : HttpRequest , id : str ) :
""" Renders a modal form view for creating deductions
2021-08-10 10:42:04 +02:00
Args :
request ( HttpRequest ) : The incoming request
2021-08-30 11:29:15 +02:00
id ( str ) : The intervention ' s id which shall benefit from this deduction
2021-08-10 10:42:04 +02:00
Returns :
"""
intervention = get_object_or_404 ( Intervention , id = id )
2021-08-30 11:29:15 +02:00
form = NewDeductionForm ( request . POST or None , instance = intervention , user = request . user )
2021-08-10 10:42:04 +02:00
return form . process_request (
request ,
2021-08-30 11:29:15 +02:00
msg_success = _ ( " Deduction added " )
2021-08-10 10:42:04 +02:00
)
2021-08-10 17:19:42 +02:00
@login_required
@conservation_office_group_required
def record_view ( request : HttpRequest , id : str ) :
""" Renders a modal form for recording an intervention
Args :
request ( HttpRequest ) : The incoming request
id ( str ) : The intervention ' s id
Returns :
"""
intervention = get_object_or_404 ( Intervention , id = id )
2021-08-26 15:45:24 +02:00
form = RecordModalForm ( request . POST or None , instance = intervention , user = request . user )
2021-08-10 17:19:42 +02:00
msg_succ = _ ( " {} unrecorded " ) if intervention . recorded else _ ( " {} recorded " )
msg_succ = msg_succ . format ( intervention . identifier )
return form . process_request (
request ,
msg_succ ,
msg_error = _ ( " There are errors on this intervention: " )
)