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-07-30 13:30:42 +02:00
from intervention . forms import NewInterventionForm , EditInterventionForm , ShareInterventionForm
2021-07-01 13:36:07 +02:00
from intervention . models import Intervention
from intervention . tables import InterventionTable
from konova . contexts import BaseContext
from konova . decorators import *
2021-07-26 15:16:16 +02:00
from konova . forms import RemoveForm , SimpleGeomForm , NewDocumentForm
2021-08-02 10:14:34 +02:00
from konova . utils . message_templates import FORM_INVALID
2021-07-01 13:36:07 +02:00
@login_required
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-21 15:40:34 +02:00
next_version = None , # only newest versions
2021-07-22 10:00:59 +02:00
) . order_by (
2021-08-02 11:52:20 +02:00
" -created__timestamp "
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
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-07-26 15:16:16 +02:00
template = form . template
if request . method == " POST " :
if form . is_valid ( ) :
doc = form . save ( )
messages . success (
request ,
_ ( " Document ' {} ' added " ) . format ( doc . title )
)
return redirect ( request . META . get ( " HTTP_REFERER " , " home " ) )
else :
messages . info (
request ,
2021-08-02 10:14:34 +02:00
FORM_INVALID
2021-07-26 15:16:16 +02:00
)
return redirect ( request . META . get ( " HTTP_REFERER " , " home " ) )
elif request . method == " GET " :
context = {
" form " : form
}
context = BaseContext ( request , context ) . context
return render ( request , template , context )
else :
raise NotImplementedError
2021-07-01 13:36:07 +02:00
@login_required
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-07-22 13:19:14 +02:00
has_access = intervention . has_access ( user = request . user )
geom_form = SimpleGeomForm (
instance = intervention
)
2021-07-01 13:36:07 +02:00
context = {
" intervention " : intervention ,
2021-08-02 10:53:34 +02:00
" compensations " : compensations ,
2021-07-22 13:19:14 +02:00
" has_access " : has_access ,
" geom_form " : geom_form ,
2021-07-01 13:36:07 +02:00
}
2021-07-22 16:06:13 +02:00
if not has_access :
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
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 :
"""
template = " konova/form.html "
2021-07-01 15:08:22 +02:00
obj = Intervention . objects . get ( id = id )
# ToDo
2021-07-01 13:36:07 +02:00
form = RemoveForm (
object_to_remove = obj ,
2021-07-01 15:08:22 +02:00
remove_post_url = reverse ( " intervention:remove " , args = ( id , ) ) ,
2021-07-01 13:36:07 +02:00
cancel_url = reverse ( " intervention:index " ) ,
)
context = {
" form " : form ,
}
context = BaseContext ( request , context ) . context
return render ( request , template , context )
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
if intervention . has_access ( user ) :
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 :
"""
user = request . user
intervention = get_object_or_404 ( Intervention , id = id )
form = ShareInterventionForm ( request . POST or None , instance = intervention , request = request )
2021-07-30 15:23:46 +02:00
if request . method == " POST " :
if form . is_valid ( ) :
form . save ( )
messages . info (
request ,
_ ( " Share settings updated " )
)
return redirect ( request . META . get ( " HTTP_REFERER " , " home " ) )
elif request . method == " GET " :
context = {
" form " : form ,
}
context = BaseContext ( request , context ) . context
return render ( request , form . template , context )
else :
2021-07-30 13:30:42 +02:00
raise NotImplementedError