#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.shortcuts import render, redirect
from django.shortcuts import get_object_or_404
from django.conf import settings
from django.views.generic import TemplateView
from django.contrib.auth.models import User
from django.db.models import Q
from django.db.models.functions import Coalesce
from django.http import Http404
from ..ui.templatetags.markup import restructuredtext
from .models import Report
import simplejson as json
# Permissions for viewing a report is complicated:
# - 'E' indicates permissions to view the editable version of the report (usually accessed by author and report name)
# - 'V' indicates permissions to view the view-only version of the report (usually accessed by report number)
# - 'R' indicates that the user will be redirected from the editable version to the view-only version (but not given a 404)
# | *Report State* X *User Type* | Editable | Locked | Published |
# | -------------------------------------: | :------: | :----: | :-------: |
# | Report Author | E, V | R, V | R, V |
# | BEAT User | V | V | V |
# | Anonymous | | V | V |
# | Public (Published) Reports List Reader | | | V |
[docs]def show_actionbar(request, report):
correct_status = report.status == 'E' or report.status == 'L'
correct_user = request.user == report.author
is_admin = request.user.is_staff
return (correct_status and correct_user) or is_admin
#------------------------------------------------
[docs]def by_number(request, number):
# get the query from the DB
obj = get_object_or_404(Report, number=int(number))
isAnon = request.user.is_anonymous()
if obj.status == 'E' and isAnon:
# return 404
raise Http404('No %s matches the given query.' % Report._meta.object_name)
return render(request,
'reports/report.html',
{
'report_number' : number,
'owner': False,
'report': obj,
'USE_HTTPS_GRAVATAR': settings.USE_HTTPS_GRAVATAR,
'show_actionbar': show_actionbar(request, obj)
})
#------------------------------------------------
[docs]def for_author(request, author_name, report_name):
# get the query from the DB
obj = get_object_or_404(Report,
author__username = author_name,
name = report_name)
isAuthor = request.user.username == obj.author.username
isEditable = obj.status == 'E'
isPublished = obj.status == 'P'
isLocked = obj.status == 'L'
# if its the author and its locked, redirect to numbered url
# same if its published
if isPublished or (isAuthor and isLocked):
return redirect(obj)
# only valid when the author is accessing it and its editable
if isEditable and isAuthor:
return render(request,
'reports/report.html',
{
'author' : author_name,
'report_name' : report_name,
'owner' : (request.user == obj.author),
'report' : obj,
'USE_HTTPS_GRAVATAR': settings.USE_HTTPS_GRAVATAR,
'show_actionbar': show_actionbar(request, obj)
})
# return 404
raise Http404('No %s matches the given query.' % Report._meta.object_name)
#------------------------------------------------
[docs]def ls(request, author_name):
'''List all accessible reports to the request user'''
if not author_name: return public_ls(request)
# check that the user exists on the system
author = get_object_or_404(User, username=author_name)
owner = (request.user==author)
if owner:
# user wants to retrieve all available objects, including his
objects = Report.objects.filter(Q(status=Report.PUBLISHED)|Q(author=author))
else:
# user wants to retrieve objects belonging to another user
objects = Report.objects.filter(author=author, status=Report.PUBLISHED)
objects = objects.annotate(updated=Coalesce('publication_date', 'creation_date',)).order_by('-updated')
return render(request,
'reports/list.html',
dict(
objects=objects,
author=author,
owner=owner,
))
#------------------------------------------------
[docs]def public_ls(request):
'''List all publicly accessible reports'''
objects = Report.objects.filter(status=Report.PUBLISHED).order_by('publication_date')
objects = objects.annotate(updated=Coalesce('publication_date', 'creation_date',)).order_by('-updated')
return render(request,
'reports/list.html',
dict(
objects=objects,
author=request.user,
owner=False,
))
#------------------------------------------------
[docs]class PartialGroupView(TemplateView):
[docs] def get_template_names(self):
if 'template_name' in self.kwargs:
self.template_name = 'reports/partials/' + self.kwargs.get('template_name')
return super(PartialGroupView, self).get_template_names()
[docs] def get_context_data(self, **kwargs):
context = super(PartialGroupView, self).get_context_data(**kwargs)
return context