Skip to content
Snippets Groups Projects
Commit efc1e53e authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[toolchains][all] Pre-commit cleanup

parent 06fc40c5
No related branches found
No related tags found
2 merge requests!366Cleanup toolchains,!342Django 3 migration
Pipeline #42685 passed
...@@ -29,60 +29,50 @@ from django import forms ...@@ -29,60 +29,50 @@ from django import forms
from django.contrib import admin from django.contrib import admin
from django.core.files.base import ContentFile from django.core.files.base import ContentFile
from .models import Toolchain as ToolchainModel
from ..ui.forms import CodeMirrorJSONFileField, CodeMirrorRSTFileField, \
NameField
from ..common.texts import Messages from ..common.texts import Messages
from ..ui.forms import CodeMirrorJSONFileField
from ..ui.forms import CodeMirrorRSTFileField
from ..ui.forms import NameField
from .models import Toolchain as ToolchainModel
# ----------------------------------------------------------
#----------------------------------------------------------
class ToolchainModelForm(forms.ModelForm): class ToolchainModelForm(forms.ModelForm):
name = NameField( name = NameField(
widget=forms.TextInput(attrs=dict(size=80)), widget=forms.TextInput(attrs=dict(size=80)), help_text=Messages["name"],
help_text=Messages['name'],
) )
declaration_file = CodeMirrorJSONFileField( declaration_file = CodeMirrorJSONFileField(
label='Declaration', label="Declaration", help_text=Messages["json"],
help_text=Messages['json'],
) )
description_file = CodeMirrorRSTFileField( description_file = CodeMirrorRSTFileField(
label='Description', label="Description",
required=False, required=False,
allow_empty_file=True, allow_empty_file=True,
help_text=Messages['description'], help_text=Messages["description"],
) )
class Meta: class Meta:
model = ToolchainModel model = ToolchainModel
exclude = [] exclude = []
widgets = { widgets = {
'short_description': forms.TextInput( "short_description": forms.TextInput(attrs=dict(size=100),),
attrs=dict(size=100), "errors": forms.Textarea(attrs=dict(readonly=1, cols=150,),),
),
'errors': forms.Textarea(
attrs=dict(readonly=1,cols=150,),
),
} }
def clean_declaration(self): def clean_declaration(self):
"""Cleans-up the file data, make sure it is really new""" """Cleans-up the file data, make sure it is really new"""
new_declaration = self.cleaned_data['declaration_file'].read() new_declaration = self.cleaned_data["declaration_file"].read()
old_declaration = '' old_declaration = ""
if self.instance and self.instance.declaration_file.name is not None: if self.instance and self.instance.declaration_file.name is not None:
old_declaration = self.instance.declaration_string old_declaration = self.instance.declaration_string
if new_declaration == old_declaration: if new_declaration == old_declaration:
self.changed_data.remove('declaration_file') self.changed_data.remove("declaration_file")
content_file = ContentFile(old_declaration) content_file = ContentFile(old_declaration)
content_file.name = self.instance.declaration_file.name content_file.name = self.instance.declaration_file.name
return content_file return content_file
...@@ -90,55 +80,61 @@ class ToolchainModelForm(forms.ModelForm): ...@@ -90,55 +80,61 @@ class ToolchainModelForm(forms.ModelForm):
# we don't validate toolchains - they should be saved in any state # we don't validate toolchains - they should be saved in any state
# if that works out, then we return the passed file # if that works out, then we return the passed file
self.cleaned_data['declaration_file'].seek(0) #reset ContentFile readout self.cleaned_data["declaration_file"].seek(0) # reset ContentFile readout
return self.cleaned_data['declaration_file'] return self.cleaned_data["declaration_file"]
def clean(self): def clean(self):
"""Cleans-up the input data, make sure it overall validates""" """Cleans-up the input data, make sure it overall validates"""
# make sure we don't pass back a str field as 'file' # make sure we don't pass back a str field as 'file'
if 'declaration_file' in self.data and \ if "declaration_file" in self.data and isinstance(
isinstance(self.data['declaration_file'], str): self.data["declaration_file"], str
):
mutable_data = self.data.copy() mutable_data = self.data.copy()
mutable_data['declaration_file'] = ContentFile(self.data['declaration_file'], name='unsaved') mutable_data["declaration_file"] = ContentFile(
self.data["declaration_file"], name="unsaved"
)
self.data = mutable_data self.data = mutable_data
#---------------------------------------------------------- # ----------------------------------------------------------
def rehash_toolchain(modeladmin, request, queryset): def rehash_toolchain(modeladmin, request, queryset):
"""Recalculates the hash of an toolchain""" """Recalculates the hash of an toolchain"""
for q in queryset: q.save() for q in queryset:
q.save()
rehash_toolchain.short_description = 'Rehash selected toolchains' rehash_toolchain.short_description = "Rehash selected toolchains"
class Toolchain(admin.ModelAdmin): class Toolchain(admin.ModelAdmin):
list_display = ('id', list_display = (
'author', "id",
'name', "author",
'version', "name",
'short_description', "version",
'creation_date', "short_description",
'hash', "creation_date",
'previous_version', "hash",
'fork_of', "previous_version",
'sharing', "fork_of",
) "sharing",
search_fields = ['author__username', )
'name', search_fields = [
'short_description', "author__username",
'previous_version__author__username', "name",
'previous_version__name', "short_description",
'fork_of__name' "previous_version__author__username",
] "previous_version__name",
list_display_links = ('id', 'name') "fork_of__name",
list_filter = ('sharing', ) ]
readonly_fields = ('hash', 'errors', 'short_description') list_display_links = ("id", "name")
list_filter = ("sharing",)
readonly_fields = ("hash", "errors", "short_description")
actions = [ actions = [
rehash_toolchain, rehash_toolchain,
...@@ -146,40 +142,32 @@ class Toolchain(admin.ModelAdmin): ...@@ -146,40 +142,32 @@ class Toolchain(admin.ModelAdmin):
form = ToolchainModelForm form = ToolchainModelForm
filter_horizontal = [ filter_horizontal = ["shared_with", "shared_with_team"]
'shared_with',
'shared_with_team'
]
fieldsets = ( fieldsets = (
(None, (None, dict(fields=("name", "author"),),),
dict( (
fields=('name', 'author'), "Documentation",
), dict(
), classes=("collapse",), fields=("short_description", "description_file"),
('Documentation', ),
dict( ),
classes=('collapse',), (
fields=('short_description', 'description_file'), "Versioning",
), dict(
), classes=("collapse",),
('Versioning', fields=("version", "previous_version", "fork_of"),
dict( ),
classes=('collapse',), ),
fields=('version', 'previous_version', 'fork_of'), (
), "Sharing",
), dict(
('Sharing', classes=("collapse",),
dict( fields=("sharing", "shared_with", "shared_with_team"),
classes=('collapse',), ),
fields=('sharing', 'shared_with', 'shared_with_team'), ),
), ("Source code", dict(fields=("hash", "declaration_file", "errors"),),),
),
('Source code',
dict(
fields=('hash', 'declaration_file', 'errors'),
),
),
) )
admin.site.register(ToolchainModel, Toolchain) admin.site.register(ToolchainModel, Toolchain)
...@@ -26,21 +26,16 @@ ...@@ -26,21 +26,16 @@
############################################################################### ###############################################################################
from ..common.api import ( from ..common.api import CheckContributionNameView
CheckContributionNameView, from ..common.api import ListContributionView
ShareView, from ..common.api import ListCreateContributionView
ListCreateContributionView, from ..common.api import RetrieveUpdateDestroyContributionView
RetrieveUpdateDestroyContributionView, from ..common.api import ShareView
)
from .models import Toolchain from .models import Toolchain
from .serializers import ToolchainSerializer
from .serializers import FullToolchainSerializer from .serializers import FullToolchainSerializer
from .serializers import ToolchainCreationSerializer from .serializers import ToolchainCreationSerializer
from .serializers import ToolchainModSerializer from .serializers import ToolchainModSerializer
from .serializers import ToolchainSerializer
from ..common.api import ListContributionView
# ---------------------------------------------------------- # ----------------------------------------------------------
......
...@@ -25,18 +25,21 @@ ...@@ -25,18 +25,21 @@
# # # #
############################################################################### ###############################################################################
from ..common.apps import CommonAppConfig
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from ..common.apps import CommonAppConfig
class ToolchainsConfig(CommonAppConfig): class ToolchainsConfig(CommonAppConfig):
name = 'beat.web.toolchains' name = "beat.web.toolchains"
verbose_name = _('Toolchains') verbose_name = _("Toolchains")
def ready(self): def ready(self):
super(ToolchainsConfig, self).ready() super(ToolchainsConfig, self).ready()
from .signals import auto_delete_file_on_delete, auto_delete_file_on_change
from actstream import registry from actstream import registry
registry.register(self.get_model('Toolchain'))
from .signals import auto_delete_file_on_change # noqa: F401
from .signals import auto_delete_file_on_delete # noqa: F401
registry.register(self.get_model("Toolchain"))
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
############################################################################### ###############################################################################
import simplejson import simplejson
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
...@@ -41,8 +40,7 @@ from ..common.models import get_contribution_declaration_filename ...@@ -41,8 +40,7 @@ from ..common.models import get_contribution_declaration_filename
from ..common.models import get_contribution_description_filename from ..common.models import get_contribution_description_filename
from ..common.storage import OverwriteStorage from ..common.storage import OverwriteStorage
# ----------------------------------------------------------
#----------------------------------------------------------
def validate_toolchain(declaration): def validate_toolchain(declaration):
...@@ -51,39 +49,49 @@ def validate_toolchain(declaration): ...@@ -51,39 +49,49 @@ def validate_toolchain(declaration):
toolchain = beat.core.toolchain.Toolchain(settings.PREFIX, declaration) toolchain = beat.core.toolchain.Toolchain(settings.PREFIX, declaration)
if not toolchain.valid: if not toolchain.valid:
errors = 'The toolchain declaration is **invalid**. Errors:\n * ' + \ errors = (
'\n * '.join(toolchain.errors) "The toolchain declaration is **invalid**. Errors:\n * "
+ "\n * ".join(toolchain.errors)
)
raise SyntaxError(errors) raise SyntaxError(errors)
return toolchain return toolchain
#---------------------------------------------------------- # ----------------------------------------------------------
class ToolchainStorage(OverwriteStorage): class ToolchainStorage(OverwriteStorage):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(ToolchainStorage, self).__init__(*args, location=settings.TOOLCHAINS_ROOT, **kwargs) super(ToolchainStorage, self).__init__(
*args, location=settings.TOOLCHAINS_ROOT, **kwargs
)
#---------------------------------------------------------- # ----------------------------------------------------------
class ToolchainManager(StoredContributionManager): class ToolchainManager(StoredContributionManager):
def create_toolchain(
def create_toolchain(self, author, name, short_description='', description='', self,
declaration=None, version=1, previous_version=None, author,
fork_of=None): name,
short_description="",
description="",
declaration=None,
version=1,
previous_version=None,
fork_of=None,
):
# Create the database representation of the toolchain # Create the database representation of the toolchain
toolchain = self.model( toolchain = self.model(
author = author, author=author,
name = self.model.sanitize_name(name), name=self.model.sanitize_name(name),
version = version, version=version,
sharing = self.model.PRIVATE, sharing=self.model.PRIVATE,
previous_version = previous_version, previous_version=previous_version,
fork_of = fork_of, fork_of=fork_of,
) )
# Check the provided declaration # Check the provided declaration
...@@ -95,11 +103,11 @@ class ToolchainManager(StoredContributionManager): ...@@ -95,11 +103,11 @@ class ToolchainManager(StoredContributionManager):
else: else:
tc = beat.core.toolchain.Toolchain(settings.PREFIX, data=None) tc = beat.core.toolchain.Toolchain(settings.PREFIX, data=None)
declaration = tc.data declaration = tc.data
elif not(isinstance(declaration, dict)): elif not (isinstance(declaration, dict)):
declaration = simplejson.loads(declaration) declaration = simplejson.loads(declaration)
if len(short_description) > 0: if len(short_description) > 0:
declaration['description'] = short_description declaration["description"] = short_description
toolchain.declaration = declaration toolchain.declaration = declaration
...@@ -116,18 +124,18 @@ class ToolchainManager(StoredContributionManager): ...@@ -116,18 +124,18 @@ class ToolchainManager(StoredContributionManager):
toolchain.save() toolchain.save()
if toolchain.errors: if toolchain.errors:
toolchain.delete() # undo saving to respect current API toolchain.delete() # undo saving to respect current API
return (None, toolchain.errors) return (None, toolchain.errors)
return (toolchain, None) return (toolchain, None)
#---------------------------------------------------------- # ----------------------------------------------------------
class Toolchain(StoredContribution): class Toolchain(StoredContribution):
#_____ Constants _______ # _____ Constants _______
DEFAULT_TOOLCHAIN_TEXT = """\ DEFAULT_TOOLCHAIN_TEXT = """\
{ {
"blocks": [], "blocks": [],
...@@ -137,68 +145,68 @@ class Toolchain(StoredContribution): ...@@ -137,68 +145,68 @@ class Toolchain(StoredContribution):
} }
""" """
#_____ Fields __________ # _____ Fields __________
declaration_file = models.FileField(storage=ToolchainStorage(), declaration_file = models.FileField(
upload_to=get_contribution_declaration_filename, storage=ToolchainStorage(),
blank=True, null=True, upload_to=get_contribution_declaration_filename,
max_length=200, blank=True,
db_column='declaration' null=True,
) max_length=200,
db_column="declaration",
description_file = models.FileField(storage=ToolchainStorage(), )
upload_to=get_contribution_description_filename,
blank=True, null=True, description_file = models.FileField(
max_length=200, storage=ToolchainStorage(),
db_column='description' upload_to=get_contribution_description_filename,
) blank=True,
null=True,
max_length=200,
db_column="description",
)
# read-only parameters that are updated at every save(), if required # read-only parameters that are updated at every save(), if required
errors = models.TextField(blank=True, null=True, errors = models.TextField(
help_text="Errors detected while validating the toolchain. Automatically set by the platform.") blank=True,
null=True,
help_text="Errors detected while validating the toolchain. Automatically set by the platform.",
)
objects = ToolchainManager() objects = ToolchainManager()
# _____ Utilities __________
#_____ Utilities __________
def get_absolute_url(self): def get_absolute_url(self):
return reverse( return reverse(
'toolchains:view', "toolchains:view", args=(self.author.username, self.name, self.version,),
args=(self.author.username, self.name, self.version,),
) )
def get_api_update_url(self): def get_api_update_url(self):
'''Returns the endpoint to update this object''' """Returns the endpoint to update this object"""
return reverse( return reverse(
'api_toolchains:object', "api_toolchains:object",
args=(self.author.username, self.name, self.version,), args=(self.author.username, self.name, self.version,),
) )
def get_api_share_url(self): def get_api_share_url(self):
'''Returns the endpoint to share this object''' """Returns the endpoint to share this object"""
return reverse( return reverse(
'api_toolchains:share', "api_toolchains:share",
args=(self.author.username, self.name, self.version,), args=(self.author.username, self.name, self.version,),
) )
def get_new_experiment_url(self): def get_new_experiment_url(self):
'''Returns the view to create a new experiment from self''' """Returns the view to create a new experiment from self"""
return reverse( return reverse(
'experiments:new-from-toolchain', "experiments:new-from-toolchain",
args=(self.author.username, self.name, self.version,), args=(self.author.username, self.name, self.version,),
) )
# _____ Overrides __________
#_____ Overrides __________
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
...@@ -206,20 +214,24 @@ class Toolchain(StoredContribution): ...@@ -206,20 +214,24 @@ class Toolchain(StoredContribution):
declaration = self.declaration declaration = self.declaration
# Compute the hash of the content # Compute the hash of the content
content_hash = beat.core.hash.hashJSON(declaration, 'description') content_hash = beat.core.hash.hashJSON(declaration, "description")
content_modified = (content_hash != self.hash) content_modified = content_hash != self.hash
if content_modified: if content_modified:
# toolchains can be saved even if they are not valid... # toolchains can be saved even if they are not valid...
wrapper = None wrapper = None
errors = '' errors = ""
try: try:
wrapper = validate_toolchain(declaration) wrapper = validate_toolchain(declaration)
except Exception as e: except Exception as e:
errors = str(e) errors = str(e)
self.hash = content_hash self.hash = content_hash
self.short_description = wrapper.description if (wrapper is not None) and (wrapper.description is not None) else '' self.short_description = (
wrapper.description
if (wrapper is not None) and (wrapper.description is not None)
else ""
)
# Store the errors (if applicable) # Store the errors (if applicable)
if errors is not None and not errors.strip(): if errors is not None and not errors.strip():
...@@ -227,7 +239,7 @@ class Toolchain(StoredContribution): ...@@ -227,7 +239,7 @@ class Toolchain(StoredContribution):
else: else:
self.errors = errors self.errors = errors
else: else:
self.short_description = declaration.get('description', '') self.short_description = declaration.get("description", "")
# Ensures that the sharing informations are consistent # Ensures that the sharing informations are consistent
if self.sharing == Contribution.USABLE: if self.sharing == Contribution.USABLE:
...@@ -236,11 +248,10 @@ class Toolchain(StoredContribution): ...@@ -236,11 +248,10 @@ class Toolchain(StoredContribution):
# Invoke the base implementation # Invoke the base implementation
super(Toolchain, self).save(*args, **kwargs) super(Toolchain, self).save(*args, **kwargs)
# _____ Methods __________
#_____ Methods __________
def is_valid(self): def is_valid(self):
return (self.errors is None) return self.errors is None
def modifiable(self): def modifiable(self):
return (self.experiments.count() == 0) and super(Toolchain, self).modifiable() return (self.experiments.count() == 0) and super(Toolchain, self).modifiable()
......
...@@ -27,19 +27,15 @@ ...@@ -27,19 +27,15 @@
from rest_framework import serializers from rest_framework import serializers
from ..common.serializers import ( import beat.core.toolchain
ContributionSerializer,
ContributionCreationSerializer,
ContributionModSerializer,
)
from ..attestations.serializers import AttestationSerializer from ..attestations.serializers import AttestationSerializer
from ..common.serializers import ContributionCreationSerializer
from ..common.serializers import ContributionModSerializer
from ..common.serializers import ContributionSerializer
from ..experiments.serializers import ExperimentSerializer from ..experiments.serializers import ExperimentSerializer
from .models import Toolchain from .models import Toolchain
import beat.core.toolchain
# ---------------------------------------------------------- # ----------------------------------------------------------
......
...@@ -30,8 +30,7 @@ from django.dispatch import receiver ...@@ -30,8 +30,7 @@ from django.dispatch import receiver
from .models import Toolchain from .models import Toolchain
# ----------------------------------------------------------
#----------------------------------------------------------
# These two auto-delete files from filesystem when they are unneeded: # These two auto-delete files from filesystem when they are unneeded:
...@@ -46,7 +45,7 @@ def auto_delete_file_on_delete(sender, instance, **kwargs): ...@@ -46,7 +45,7 @@ def auto_delete_file_on_delete(sender, instance, **kwargs):
instance.description_file.delete(save=False) instance.description_file.delete(save=False)
#---------------------------------------------------------- # ----------------------------------------------------------
@receiver(models.signals.pre_save, sender=Toolchain) @receiver(models.signals.pre_save, sender=Toolchain)
......
...@@ -25,25 +25,22 @@ ...@@ -25,25 +25,22 @@
# # # #
############################################################################### ###############################################################################
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 Http404
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.shortcuts import render from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.functions import Coalesce
from beat.core import prototypes
from .models import Toolchain
from ..team.models import Team
from ..reports.models import Report
from ..common.texts import Messages from ..common.texts import Messages
from ..common.utils import ensure_string from ..common.utils import ensure_string
from ..reports.models import Report
from ..team.models import Team
from ..ui.templatetags.markup import restructuredtext from ..ui.templatetags.markup import restructuredtext
from .models import Toolchain
from beat.core import prototypes
import simplejson as json
@login_required @login_required
...@@ -53,22 +50,22 @@ def create(request, name=None): ...@@ -53,22 +50,22 @@ def create(request, name=None):
The user must be authenticated before it can add a new toolchain The user must be authenticated before it can add a new toolchain
""" """
parameters = {'toolchain_author': request.user.username, parameters = {
'toolchain_name': name, "toolchain_author": request.user.username,
'toolchain_version': 1, "toolchain_name": name,
'short_description': '', "toolchain_version": 1,
'description': '', "short_description": "",
'errors': '', "description": "",
'edition': False, "errors": "",
'messages': Messages, "edition": False,
} "messages": Messages,
}
# Retrieves the existing toolchain (if necessary) # Retrieves the existing toolchain (if necessary)
if name is not None: if name is not None:
previous_versions = Toolchain.objects.filter( previous_versions = Toolchain.objects.filter(
author=request.user, author=request.user, name__iexact=name,
name__iexact=name, ).order_by("-version")
).order_by('-version')
if len(previous_versions) == 0: if len(previous_versions) == 0:
raise Http404() raise Http404()
...@@ -76,19 +73,23 @@ def create(request, name=None): ...@@ -76,19 +73,23 @@ def create(request, name=None):
description = ensure_string(previous_version.description) description = ensure_string(previous_version.description)
parameters['toolchain_version'] = previous_version.version + 1 parameters["toolchain_version"] = previous_version.version + 1
parameters['declaration'] = previous_version.declaration_string.replace('\n', '') parameters["declaration"] = previous_version.declaration_string.replace(
parameters['short_description'] = previous_version.short_description "\n", ""
parameters['description'] = description.replace('\n', '\\n') )
parameters['html_description'] = restructuredtext(description).replace('\n', '') parameters["short_description"] = previous_version.short_description
parameters['errors'] = previous_version.errors.replace('\n', '\\n') if previous_version.errors is not None else '' parameters["description"] = description.replace("\n", "\\n")
parameters["html_description"] = restructuredtext(description).replace("\n", "")
parameters["errors"] = (
previous_version.errors.replace("\n", "\\n")
if previous_version.errors is not None
else ""
)
else: else:
declaration, errors = prototypes.load('toolchain') declaration, errors = prototypes.load("toolchain")
parameters['declaration'] = json.dumps(declaration) parameters["declaration"] = json.dumps(declaration)
return render(request, return render(request, "toolchains/edition.html", parameters)
'toolchains/edition.html',
parameters)
@login_required @login_required
...@@ -99,30 +100,30 @@ def fork(request, author, name, version): ...@@ -99,30 +100,30 @@ def fork(request, author, name, version):
""" """
# Retrieves the forked toolchain # Retrieves the forked toolchain
fork_of = get_object_or_404(Toolchain.objects.for_user(request.user, True), fork_of = get_object_or_404(
author__username__iexact=author, Toolchain.objects.for_user(request.user, True),
name__iexact=name, author__username__iexact=author,
version=int(version) name__iexact=name,
) version=int(version),
)
description = ensure_string(fork_of.description) description = ensure_string(fork_of.description)
errors = ensure_string(fork_of.errors) errors = ensure_string(fork_of.errors)
parameters = {'toolchain_author': request.user.username, parameters = {
'toolchain_name': name, "toolchain_author": request.user.username,
'toolchain_version': 1, "toolchain_name": name,
'fork_of': fork_of, "toolchain_version": 1,
'declaration': fork_of.declaration_string.replace('\n', ''), "fork_of": fork_of,
'short_description': fork_of.short_description, "declaration": fork_of.declaration_string.replace("\n", ""),
'description': description.replace('\n', '\\n'), "short_description": fork_of.short_description,
'errors': errors.replace('\n', '\\n'), "description": description.replace("\n", "\\n"),
'edition': False, "errors": errors.replace("\n", "\\n"),
'messages': Messages, "edition": False,
} "messages": Messages,
}
return render(request, return render(request, "toolchains/edition.html", parameters)
'toolchains/edition.html',
parameters)
@login_required @login_required
...@@ -136,29 +137,33 @@ def edit(request, author, name, version): ...@@ -136,29 +137,33 @@ def edit(request, author, name, version):
raise Http404() raise Http404()
# Retrieves the toolchain # Retrieves the toolchain
toolchain = get_object_or_404(Toolchain, toolchain = get_object_or_404(
author__username__iexact=author, Toolchain,
name__iexact=name, author__username__iexact=author,
version=int(version) name__iexact=name,
) version=int(version),
)
description = ensure_string(toolchain.description) description = ensure_string(toolchain.description)
errors = ensure_string(toolchain.errors) errors = ensure_string(toolchain.errors)
# Render the page # Render the page
return render(request, return render(
'toolchains/edition.html', request,
{'toolchain_author': request.user.username, "toolchains/edition.html",
'toolchain_name': name, {
'toolchain_version': toolchain.version, "toolchain_author": request.user.username,
'declaration': toolchain.declaration_string.replace('\n', ''), "toolchain_name": name,
'short_description': toolchain.short_description, "toolchain_version": toolchain.version,
'description': description.replace('\n', '\\n'), "declaration": toolchain.declaration_string.replace("\n", ""),
'html_description': restructuredtext(description).replace('\n', ''), "short_description": toolchain.short_description,
'errors': errors.replace('\n', '\\n'), "description": description.replace("\n", "\\n"),
'edition': True, "html_description": restructuredtext(description).replace("\n", ""),
'messages': Messages, "errors": errors.replace("\n", "\\n"),
}) "edition": True,
"messages": Messages,
},
)
def view(request, author, name, version=None): def view(request, author, name, version=None):
...@@ -175,8 +180,9 @@ def view(request, author, name, version=None): ...@@ -175,8 +180,9 @@ def view(request, author, name, version=None):
version=int(version), version=int(version),
) )
else: else:
toolchain = Toolchain.objects.filter(author__username__iexact=author, toolchain = Toolchain.objects.filter(
name__iexact=name).order_by('-version') author__username__iexact=author, name__iexact=name
).order_by("-version")
if not toolchain: if not toolchain:
raise Http404() raise Http404()
else: else:
...@@ -187,25 +193,29 @@ def view(request, author, name, version=None): ...@@ -187,25 +193,29 @@ def view(request, author, name, version=None):
if not has_access: if not has_access:
raise Http404() raise Http404()
owner = (request.user == toolchain.author) owner = request.user == toolchain.author
reports = None reports = None
if not request.user.is_anonymous: #fetch user reports, if any if not request.user.is_anonymous: # fetch user reports, if any
reports = Report.objects.filter(author=request.user) reports = Report.objects.filter(author=request.user)
# Users the object can be shared with # 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")
# Render the page # Render the page
return render(request, return render(
'toolchains/view.html', request,
{ "toolchains/view.html",
'toolchain': toolchain, {
'owner': owner, "toolchain": toolchain,
'reports': reports, "owner": owner,
'users': users, "reports": reports,
'teams': Team.objects.for_user(request.user, True) "users": users,
}) "teams": Team.objects.for_user(request.user, True),
},
)
def diff(request, author1, name1, version1, author2, name2, version2): def diff(request, author1, name1, version1, author2, name2, version2):
...@@ -220,7 +230,8 @@ def diff(request, author1, name1, version1, author2, name2, version2): ...@@ -220,7 +230,8 @@ def diff(request, author1, name1, version1, author2, name2, version2):
version=int(version1), version=int(version1),
) )
has_access, _ = toolchain1.accessibility_for(request.user) has_access, _ = toolchain1.accessibility_for(request.user)
if not has_access: raise Http404() if not has_access:
raise Http404()
toolchain2 = get_object_or_404( toolchain2 = get_object_or_404(
Toolchain, Toolchain,
...@@ -229,49 +240,47 @@ def diff(request, author1, name1, version1, author2, name2, version2): ...@@ -229,49 +240,47 @@ def diff(request, author1, name1, version1, author2, name2, version2):
version=int(version2), version=int(version2),
) )
has_access, _ = toolchain2.accessibility_for(request.user) has_access, _ = toolchain2.accessibility_for(request.user)
if not has_access: raise Http404() if not has_access:
raise Http404()
return render(request, return render(
'toolchains/diff.html', request,
{ "toolchains/diff.html",
'toolchain1': toolchain1, {"toolchain1": toolchain1, "toolchain2": toolchain2},
'toolchain2': toolchain2, )
})
def ls(request, author_name): def ls(request, author_name):
'''List all accessible toolchains to the request user''' """List all accessible toolchains 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 # check that the user exists on the system
author = get_object_or_404(User, username=author_name) author = get_object_or_404(User, username=author_name)
# orders toolchains so that the latest information is displayed first # orders toolchains so that the latest information is displayed first
objects = Toolchain.objects.from_author_and_public(request.user, objects = Toolchain.objects.from_author_and_public(
author_name).order_by('-creation_date') request.user, author_name
).order_by("-creation_date")
objects = Toolchain.filter_latest_versions(objects) objects = Toolchain.filter_latest_versions(objects)
return render(request, return render(
'toolchains/list.html', request,
dict( "toolchains/list.html",
objects=objects, dict(objects=objects, author=author, owner=(request.user == author),),
author=author, )
owner=(request.user==author),
))
def public_ls(request): def public_ls(request):
'''List all publicly accessible objects''' """List all publicly accessible objects"""
# orders so that more recent are first # orders so that more recent are first
objects = Toolchain.objects.public().order_by('-creation_date') objects = Toolchain.objects.public().order_by("-creation_date")
objects = Toolchain.filter_latest_versions(objects) objects = Toolchain.filter_latest_versions(objects)
return render(request, return render(
'toolchains/list.html', request,
dict( "toolchains/list.html",
objects=objects, dict(objects=objects, author=request.user, owner=False,), # anonymous
author=request.user, #anonymous )
owner=False,
))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment