Commit 0e7d7bb4 authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[experiments][all] Pre-commit cleanup

parent d9b54f85
Pipeline #42589 passed with stage
in 14 minutes and 54 seconds
This diff is collapsed.
......@@ -25,48 +25,42 @@
# #
###############################################################################
import simplejson
import functools
import simplejson
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.core.exceptions import ValidationError
from rest_framework.response import Response
from rest_framework import permissions
from django.shortcuts import get_object_or_404
from rest_framework import generics
from rest_framework import permissions
from rest_framework import serializers
from rest_framework.views import APIView
from rest_framework.reverse import reverse
from rest_framework.exceptions import ParseError
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.views import APIView
import beat.core.hash
import beat.core.algorithm
import beat.core.hash
import beat.core.toolchain
from .models import Experiment
from .serializers import (
ExperimentSerializer,
ExperimentResultsSerializer,
ExperimentCreationSerializer,
)
from .permissions import IsDatabaseAccessible
from ..common.responses import BadRequestResponse, ForbiddenResponse
from ..common.api import (
ShareView,
ListContributionView,
ListCreateContributionView,
RetrieveUpdateDestroyContributionView,
)
from ..common.mixins import CommonContextMixin
from ..common.api import ListContributionView
from ..common.api import ListCreateContributionView
from ..common.api import RetrieveUpdateDestroyContributionView
from ..common.api import ShareView
from ..common.exceptions import ShareError
from ..common.mixins import CommonContextMixin
from ..common.responses import BadRequestResponse
from ..common.responses import ForbiddenResponse
from ..common.serializers import SharingSerializer
from ..common.utils import validate_restructuredtext, ensure_html, py3_cmp
from ..common.utils import ensure_html
from ..common.utils import py3_cmp
from ..common.utils import validate_restructuredtext
from ..toolchains.models import Toolchain
from .models import Experiment
from .permissions import IsDatabaseAccessible
from .serializers import ExperimentCreationSerializer
from .serializers import ExperimentResultsSerializer
from .serializers import ExperimentSerializer
# ----------------------------------------------------------
......
......@@ -25,15 +25,19 @@
# #
###############################################################################
from ..common.apps import CommonAppConfig
from django.utils.translation import ugettext_lazy as _
from ..common.apps import CommonAppConfig
class ExperimentsConfig(CommonAppConfig):
name = 'beat.web.experiments'
verbose_name = _('Experiments')
name = "beat.web.experiments"
verbose_name = _("Experiments")
def ready(self):
super(ExperimentsConfig, self).ready()
from .signals import on_team_delete
from actstream import registry
registry.register(self.get_model('Experiment'))
from .signals import on_team_delete # noqa: F401
registry.register(self.get_model("Experiment"))
......@@ -25,31 +25,24 @@
# #
###############################################################################
import simplejson as json
import beat.core
from datetime import datetime
from rest_framework import serializers
import simplejson as json
from django.contrib.humanize.templatetags.humanize import naturaltime
from rest_framework import exceptions as drf_exceptions
from rest_framework import serializers
from django.contrib.humanize.templatetags.humanize import naturaltime
import beat.core
from ..common import fields as beat_fields
from ..common.serializers import ShareableSerializer
from ..common.utils import validate_restructuredtext
from ..common.utils import annotate_full_name
from ..common import fields as beat_fields
from ..ui.templatetags.markup import restructuredtext
from ..common.utils import validate_restructuredtext
from ..toolchains.models import Toolchain
from .models.experiment import validate_experiment
from .models import Experiment
from ..ui.templatetags.markup import restructuredtext
from .models import Block
from .models import Experiment
from .models.experiment import validate_experiment
# ----------------------------------------------------------
......
......@@ -25,13 +25,12 @@
# #
###############################################################################
from django.db import models
from django.dispatch import receiver
from ..team.models import Team
from .models import Experiment, Block
from datetime import datetime
from .models import Experiment
# These two auto-delete files from filesystem when they are unneeded:
......@@ -70,7 +69,8 @@ def auto_delete_file_on_change(sender, instance, **kwargs):
old_descr.delete(save=False)
#_________ Algorithms _________
# _________ Algorithms _________
def build_user_algorithm_set(user):
all_algorithms = []
......@@ -80,6 +80,7 @@ def build_user_algorithm_set(user):
return set(all_algorithms)
def process_algorithms(team):
team_algorithms = set(team.shared_algorithms.all() | team.usable_algorithms.all())
......@@ -90,7 +91,8 @@ def process_algorithms(team):
algorithm.share(public=False, users=[member])
#_________ Toolchains _________
# _________ Toolchains _________
def build_user_toolchain_set(user):
all_toolchains = []
......@@ -113,6 +115,6 @@ def process_toolchains(team):
@receiver(models.signals.pre_delete, sender=Team)
def on_team_delete(sender, **kwargs):
team = kwargs.get('instance')
team = kwargs.get("instance")
process_algorithms(team)
process_toolchains(team)
......@@ -26,26 +26,25 @@
###############################################################################
'''Utilities for experiment management'''
"""Utilities for experiment management"""
import logging
from django.db.models import Count
from .models import CachedFile
import logging
logger = logging.getLogger(__name__)
def list_orphaned_cachedfiles():
'''Lists orphaned cache files that do not exist in the disk either'''
"""Lists orphaned cache files that do not exist in the disk either"""
q = CachedFile.objects.annotate(Count('blocks')).filter(blocks__count__lt=1)
q = CachedFile.objects.annotate(Count("blocks")).filter(blocks__count__lt=1)
return [c for c in q if not c.exists()]
def cleanup_orphaned_cachedfiles():
'''Cleans-up orphaned cache files that do not exist in the disk either'''
"""Cleans-up orphaned cache files that do not exist in the disk either"""
for c in list_orphaned_cachedfiles():
logger.info("Removing orphaned CachedFile object `%s'..." % c.hash)
......
......@@ -25,26 +25,27 @@
# #
###############################################################################
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.http import Http404
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models.functions import Coalesce
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
from django.shortcuts import render
from .models import Experiment
from ..toolchains.models import Toolchain
from ..team.models import Team
from ..toolchains.models import Toolchain
from .models import Experiment
#----------------------------------------------------------
# ----------------------------------------------------------
@login_required
def new_from_toolchain(request, toolchain_author_name, toolchain_name,
toolchain_version):
'''Sets up a new experiment from a toolchain name'''
def new_from_toolchain(
request, toolchain_author_name, toolchain_name, toolchain_version
):
"""Sets up a new experiment from a toolchain name"""
# Retrieve the toolchain
toolchain = get_object_or_404(
......@@ -56,23 +57,22 @@ def new_from_toolchain(request, toolchain_author_name, toolchain_name,
# Check that the user can access it
has_access = toolchain.accessibility_for(request.user)[0]
if not(has_access): raise Http404()
if not (has_access):
raise Http404()
return render(request,
'experiments/setup.html',
{
'toolchain': toolchain,
'action': 'new',
})
return render(
request, "experiments/setup.html", {"toolchain": toolchain, "action": "new"}
)
#----------------------------------------------------------
# ----------------------------------------------------------
@login_required
def fork(request, author_name, toolchain_author_name,
toolchain_name, toolchain_version, name):
'''Sets up a new experiment from an experiment fork'''
def fork(
request, author_name, toolchain_author_name, toolchain_name, toolchain_version, name
):
"""Sets up a new experiment from an experiment fork"""
# Retrieve the experiment
experiment = get_object_or_404(
......@@ -81,28 +81,31 @@ def fork(request, author_name, toolchain_author_name,
toolchain__author__username=toolchain_author_name,
toolchain__name=toolchain_name,
toolchain__version=toolchain_version,
name=name
name=name,
)
# Check that the user can access it
(has_access, accessibility) = experiment.accessibility_for(request.user)
if not(has_access): raise Http404()
if not (has_access):
raise Http404()
return render(
request,
"experiments/setup.html",
{
"toolchain": experiment.toolchain,
"experiment": experiment,
"action": "fork",
},
)
return render(request,
'experiments/setup.html',
{
'toolchain': experiment.toolchain,
'experiment': experiment,
'action': 'fork',
})
# ----------------------------------------------------------
#----------------------------------------------------------
@login_required
def reset(request, toolchain_author_name,
toolchain_name, toolchain_version, name):
'''Resets the current experiment so it can run again'''
def reset(request, toolchain_author_name, toolchain_name, toolchain_version, name):
"""Resets the current experiment so it can run again"""
# Retrieve the experiment
experiment = get_object_or_404(
......@@ -111,19 +114,21 @@ def reset(request, toolchain_author_name,
toolchain__author__username=toolchain_author_name,
toolchain__name=toolchain_name,
toolchain__version=toolchain_version,
name=name
name=name,
)
if not experiment.deletable(): raise Http404()
if not experiment.deletable():
raise Http404()
experiment.reset()
return redirect(experiment)
def view(request, author_name, toolchain_author_name, toolchain_name,
toolchain_version, name):
'''Views an experiment no matter its present state'''
def view(
request, author_name, toolchain_author_name, toolchain_name, toolchain_version, name
):
"""Views an experiment no matter its present state"""
# Retrieve the experiment
experiment = get_object_or_404(
......@@ -132,80 +137,92 @@ def view(request, author_name, toolchain_author_name, toolchain_name,
toolchain__author__username=toolchain_author_name,
toolchain__name=toolchain_name,
toolchain__version=toolchain_version,
name=name
name=name,
)
# Check that the user can access it
(has_access, accessibility) = experiment.accessibility_for(request.user)
if not(has_access): raise Http404()
if not (has_access):
raise Http404()
if experiment.status == Experiment.PENDING:
if request.user.is_anonymous: raise Http404()
return render(request,
'experiments/setup.html',
{
'toolchain': experiment.toolchain,
'experiment': experiment,
'action': 'pending',
})
if request.user.is_anonymous:
raise Http404()
return render(
request,
"experiments/setup.html",
{
"toolchain": experiment.toolchain,
"experiment": experiment,
"action": "pending",
},
)
# Users the object can be shared with
users = User.objects.exclude(username__in=settings.ACCOUNTS_TO_EXCLUDE_FROM_TEAMS).order_by('username')
users = User.objects.exclude(
username__in=settings.ACCOUNTS_TO_EXCLUDE_FROM_TEAMS
).order_by("username")
# The experiment was already done, show results
return render(request,
'experiments/view.html',
{
'experiment': experiment,
'owner': experiment.author == request.user,
'users': users,
'teams': Team.objects.for_user(request.user, True)
})
return render(
request,
"experiments/view.html",
{
"experiment": experiment,
"owner": experiment.author == request.user,
"users": users,
"teams": Team.objects.for_user(request.user, True),
},
)
#----------------------------------------------------------
# ----------------------------------------------------------
def ls(request, author_name):
'''List all accessible experiments to the request user'''
"""List all accessible experiments to the request user"""
if not author_name: return public_ls(request)
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 so that experiments that the latest information is displayed first
objects = Experiment.objects.from_author_and_public(request.user,
author_name).annotate(updated=Coalesce('end_date', 'start_date',
'creation_date')).order_by('-updated')
objects = (
Experiment.objects.from_author_and_public(request.user, author_name)
.annotate(updated=Coalesce("end_date", "start_date", "creation_date"))
.order_by("-updated")
)
if request.user.is_anonymous:
objects = objects.exclude(status=Experiment.PENDING)
owner = (request.user == author)
owner = request.user == author
return render(request,
'experiments/list.html',
dict(
objects=objects,
author=author,
owner=owner,
))
return render(
request,
"experiments/list.html",
dict(objects=objects, author=author, owner=owner,),
)
#----------------------------------------------------------
# ----------------------------------------------------------
def public_ls(request):
'''List all publicly accessible experiments'''
"""List all publicly accessible experiments"""
# orders so that recent objects are displayed first
objects = Experiment.objects.public().exclude(status=Experiment.PENDING).annotate(updated=Coalesce('end_date', 'start_date', 'creation_date')).order_by('-updated')
return render(request,
'experiments/list.html',
dict(
objects=objects,
author=request.user, #anonymous
owner=False,
))
objects = (
Experiment.objects.public()
.exclude(status=Experiment.PENDING)
.annotate(updated=Coalesce("end_date", "start_date", "creation_date"))
.order_by("-updated")
)
return render(
request,
"experiments/list.html",
dict(objects=objects, author=request.user, owner=False,), # anonymous
)
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