#!/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.conf import settings
from django.contrib.auth.models import User
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from ..team.models import Team
from .models import DataFormat
# ----------------------------------------------------------
DEFAULT_DATAFORMAT_TEXT = """{
}"""
# ----------------------------------------------------------
[docs]def view(request, author, name, version=None):
"""Shows the dataformat"""
# Retrieves the dataformat
if version:
dataformat = get_object_or_404(
DataFormat,
author__username__iexact=author,
name__iexact=name,
version=int(version),
)
else:
dataformat = DataFormat.objects.filter(
author__username__iexact=author, name__iexact=name
).order_by("-version")
if not dataformat:
raise Http404()
else:
dataformat = dataformat[0]
(has_access, _) = dataformat.accessibility_for(request.user)
if not has_access:
raise Http404()
owner = request.user == dataformat.author
# Users the object can be shared with
users = User.objects.exclude(
username__in=settings.ACCOUNTS_TO_EXCLUDE_FROM_TEAMS
).order_by("username")
# Render the page
return render(
request,
"dataformats/view.html",
{
"dataformat": dataformat,
"owner": owner,
"users": users,
"teams": Team.objects.for_user(request.user, True),
},
)
# ----------------------------------------------------------
[docs]def diff(request, author1, name1, version1, author2, name2, version2):
"""Shows the difference between two data formats. The Web API is used to retrieve the
details about the data formats and check the accessibility.
Answers to the following URLs
<dataformats>/diff/<author1>/<name1>/<version1>/<author2>/<name2>/<version2>/
"""
return render(
request,
"dataformats/diff.html",
{
"dataformat1_author": author1,
"dataformat1_name": name1,
"dataformat1_version": version1,
"dataformat2_author": author2,
"dataformat2_name": name2,
"dataformat2_version": version2,
},
)
# ----------------------------------------------------------
[docs]def ls(request, author_name):
"""List all accessible dataformats 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)
# orders dataformats so that the latest information is displayed first
objects = DataFormat.objects.from_author_and_public(
request.user, author_name
).order_by("-creation_date")
objects = DataFormat.filter_latest_versions(objects)
return render(
request,
"dataformats/list.html",
dict(objects=objects, author=author, owner=(request.user == author)),
)
# ----------------------------------------------------------
[docs]def public_ls(request):
"""List all publicly accessible data formats"""
# orders so that latest is displayed first
objects = DataFormat.objects.public().order_by("-creation_date")
objects = DataFormat.filter_latest_versions(objects)
return render(
request,
"dataformats/list.html",
dict(objects=objects, author=request.user, owner=False,), # anonymous
)