Commit bf777c01 authored by Samuel GAIST's avatar Samuel GAIST Committed by Samuel GAIST

[algorithms][views] Remove all edition related views

parent 96484181
......@@ -25,186 +25,18 @@
# #
###############################################################################
import simplejson as json
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import Http404
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from beat.core import prototypes
from ..common.texts import Messages
from ..common.utils import ensure_string
from ..team.models import Team
from ..ui.templatetags.markup import restructuredtext
from .models import Algorithm
# ----------------------------------------------------------
@login_required
def create(request, name=None):
"""Creates a new algorithm or a new version of an existing algorithm
The user must be authenticated before it can add a new algorithm
"""
parameters = {
"algorithm_author": request.user.username,
"algorithm_name": name,
"algorithm_version": 1,
"short_description": "",
"description": "",
"messages": Messages,
"edition": False,
"plot_account": settings.PLOT_ACCOUNT,
}
# Retrieves the existing algorithm (if necessary)
if name is not None:
previous_versions = Algorithm.objects.filter(
author=request.user, name__iexact=name,
).order_by("-version")
if len(previous_versions) == 0:
raise Http404()
previous_version = previous_versions[0]
description = ensure_string(previous_version.description)
parameters["algorithm_version"] = previous_version.version + 1
parameters["declaration"] = previous_version.declaration_string.replace(
"\n", ""
)
parameters["short_description"] = previous_version.short_description
parameters["description"] = description.replace("\n", "\\n")
parameters["html_description"] = restructuredtext(description).replace("\n", "")
parameters["algorithm_language"] = previous_version.json_language
parameters["algorithm_language_name"] = previous_version.language_fullname()
parameters["binary"] = previous_version.is_binary()
parameters["new_version"] = True
if not previous_version.is_binary():
parameters["source_code"] = previous_version.source_code_file.read()
else:
parameters["source_code"] = ensure_string(
prototypes.binary_load("algorithm.py")
).replace(
"\n\n # TODO: Implement this algorithm\n\n",
"\n # TODO: Implement this algorithm\n",
)
declaration, errors = prototypes.load("algorithm")
declaration["language"] = "python"
parameters["declaration"] = json.dumps(declaration)
return render(request, "algorithms/edition.html", parameters)
# ----------------------------------------------------------
@login_required
def fork(request, author, name, version):
"""Creates a new algorithm by forking an existing algorithm
The user must be authenticated before it can fork an algorithm
"""
# Retrieves the forked algorithm based on user accessible algorithms
fork_of = get_object_or_404(
Algorithm.objects.for_user(request.user, True),
author__username__iexact=author,
name__iexact=name,
version=int(version),
)
# The only case a user can't fork an algorithm is if it's usable rather
# than shared
if fork_of.sharing == Algorithm.USABLE and fork_of.author != request.user:
raise Http404()
description = ensure_string(fork_of.description)
parameters = {
"original_author": author,
"algorithm_author": request.user.username,
"algorithm_name": name,
"algorithm_version": fork_of.version,
"algorithm_language": fork_of.json_language,
"algorithm_language_name": fork_of.language_fullname(),
"declaration": fork_of.declaration_string.replace("\n", ""),
"short_description": fork_of.short_description,
"description": description.replace("\n", "\\n"),
"html_description": restructuredtext(description).replace("\n", ""),
"messages": Messages,
"fork_of": fork_of,
"edition": False,
"binary": fork_of.is_binary(),
"plot_account": settings.PLOT_ACCOUNT,
}
if not fork_of.is_binary():
parameters["source_code"] = fork_of.source_code_file.read()
return render(request, "algorithms/edition.html", parameters)
# ----------------------------------------------------------
@login_required
def edit(request, author, name, version):
"""Allows the modification of the algorithm
The user must be authenticated before it can edit an algorithm
"""
if author != request.user.username:
raise Http404()
# Retrieves the algorithm
algorithm = get_object_or_404(
Algorithm,
author__username__iexact=author,
name__iexact=name,
version=int(version),
)
if not algorithm.modifiable():
return HttpResponseForbidden(
"Algorithm %s is not modifiable" % algorithm.fullname()
)
description = ensure_string(algorithm.description)
parameters = {
"algorithm_author": request.user.username,
"algorithm_name": name,
"algorithm_version": algorithm.version,
"algorithm_language": algorithm.json_language,
"algorithm_language_name": algorithm.language_fullname(),
"declaration": algorithm.declaration_string.replace("\n", ""),
"short_description": algorithm.short_description,
"description": description.replace("\n", "\\n"),
"html_description": restructuredtext(description).replace("\n", ""),
"messages": Messages,
"edition": True,
"binary": algorithm.is_binary(),
"plot_account": settings.PLOT_ACCOUNT,
}
if not algorithm.is_binary():
parameters["source_code"] = algorithm.source_code_file.read()
return render(request, "algorithms/edition.html", parameters)
# ----------------------------------------------------------
def view(request, author, name, version=None):
"""Shows the algorithm. The Web API is used to retrieve the details about
the algorithm and check the accessibility.
......
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