Commit 278cf7a2 authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[team][all] Pre-commit cleanup

parent b1803f2d
Pipeline #42677 passed with stage
in 15 minutes and 5 seconds
......@@ -29,7 +29,9 @@ from django.contrib import admin
from .models import Team
class TeamAdmin(admin.ModelAdmin):
filter_horizontal = ['members']
filter_horizontal = ["members"]
admin.site.register(Team, TeamAdmin)
......@@ -26,24 +26,22 @@
###############################################################################
from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from django.db.models import Q
from django.shortcuts import get_object_or_404
from rest_framework import exceptions as drf_exceptions
from rest_framework import generics
from rest_framework import permissions
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework import exceptions as drf_exceptions
from ..common.mixins import CommonContextMixin
from .models import Team
from .permissions import HasPrivacyLevel
from .permissions import IsOwner
from .serializers import FullTeamSerializer
from .serializers import SimpleTeamSerializer
from .serializers import TeamCreationSerializer
from .serializers import TeamUpdateSerializer
from .models import Team
from .permissions import IsOwner, HasPrivacyLevel
from ..common.mixins import CommonContextMixin
# ----------------------------------------------------------
......
......@@ -25,15 +25,19 @@
# #
###############################################################################
from ..common.apps import CommonAppConfig
from django.utils.translation import ugettext_lazy as _
from ..common.apps import CommonAppConfig
class TeamConfig(CommonAppConfig):
name = 'beat.web.team'
verbose_name = _('Team')
name = "beat.web.team"
verbose_name = _("Team")
def ready(self):
super(TeamConfig, self).ready()
from .signals.handlers import on_added_to_team
from actstream import registry
registry.register(self.get_model('Team'))
from .signals.handlers import on_added_to_team # noqa: F401
registry.register(self.get_model("Team"))
......@@ -25,10 +25,10 @@
# #
###############################################################################
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Q
from django.urls import reverse
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
......
......@@ -29,6 +29,7 @@ from rest_framework import permissions
from .models import Team
class IsOwner(permissions.BasePermission):
"""
The logged in user should also be the owner
......@@ -37,6 +38,7 @@ class IsOwner(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
return obj.owner == request.user
class HasPrivacyLevel(permissions.BasePermission):
"""
Check whether the user has enough rights to view the team
......@@ -49,7 +51,9 @@ class HasPrivacyLevel(permissions.BasePermission):
elif obj.privacy_level == Team.PRIVATE:
return False
elif obj.privacy_level == Team.MEMBERS and obj.members.filter(username=request.user.username):
elif obj.privacy_level == Team.MEMBERS and obj.members.filter(
username=request.user.username
):
return True
else:
......
......@@ -26,16 +26,14 @@
###############################################################################
from django.contrib.auth.models import User
from rest_framework.reverse import reverse
from rest_framework import serializers
from rest_framework.reverse import reverse
from ..common.models import Contribution
from .models import Team
from .signals import added_to_team
#----------------------------------------------------------
# ----------------------------------------------------------
class BasicTeamSerializer(serializers.ModelSerializer):
......@@ -48,91 +46,92 @@ class BasicTeamSerializer(serializers.ModelSerializer):
class Meta:
model = Team
fields = ['short_description', 'is_owner', 'accessibility']
fields = ["short_description", "is_owner", "accessibility"]
def get_name(self, obj):
return obj.fullname()
def get_is_owner(self, obj):
return (obj.owner == self.context.get('user'))
return obj.owner == self.context.get("user")
def get_accessibility(self, obj):
if obj.privacy_level == Team.PUBLIC:
return 'public'
return "public"
elif obj.privacy_level == Team.MEMBERS:
return 'members'
return "members"
else:
return 'private'
return "private"
def get_members(self, obj):
return map(lambda x: x.username, obj.members.iterator())
#----------------------------------------------------------
# ----------------------------------------------------------
class SimpleTeamSerializer(BasicTeamSerializer):
class Meta(BasicTeamSerializer.Meta):
fields = ['name', 'short_description', 'is_owner', 'accessibility']
fields = ["name", "short_description", "is_owner", "accessibility"]
#----------------------------------------------------------
# ----------------------------------------------------------
class FullTeamSerializer(BasicTeamSerializer):
class Meta(BasicTeamSerializer.Meta):
fields = ['name', 'short_description', 'is_owner', 'accessibility', 'members']
fields = ["name", "short_description", "is_owner", "accessibility", "members"]
#----------------------------------------------------------
# ----------------------------------------------------------
class CreatedTeamSerializer(BasicTeamSerializer):
object_view = serializers.SerializerMethodField()
class Meta(BasicTeamSerializer.Meta):
fields = ['name', 'short_description', 'accessibility', 'members', 'object_view']
fields = [
"name",
"short_description",
"accessibility",
"members",
"object_view",
]
def get_object_view(self, obj):
return reverse('teams:view', args=[obj.owner.username, obj.name])
return reverse("teams:view", args=[obj.owner.username, obj.name])
#----------------------------------------------------------
# ----------------------------------------------------------
class UpdatedTeamSerializer(BasicTeamSerializer):
class Meta(BasicTeamSerializer.Meta):
fields = ['short_description', 'accessibility', 'members']
fields = ["short_description", "accessibility", "members"]
#----------------------------------------------------------
# ----------------------------------------------------------
class TeamCreationSerializer(serializers.ModelSerializer):
accessibility = serializers.CharField(source='privacy_level')
accessibility = serializers.CharField(source="privacy_level")
members = serializers.ListField(child=serializers.CharField())
class Meta:
model = Team
fields = ['name', 'short_description', 'accessibility', 'members']
fields = ["name", "short_description", "accessibility", "members"]
def create(self, validated_data):
members = validated_data['members']
del validated_data['members']
members = validated_data["members"]
del validated_data["members"]
team = None
try:
team = Team.objects.get(owner=self.context['request'].user, name=validated_data['name'])
except:
pass
if self.Meta.model.objects.filter(
owner=self.context["request"].user, name=validated_data["name"]
).exists():
raise serializers.ValidationError("A team with this name already exists")
if team is not None:
raise serializers.ValidationError('A team with this name already exists')
team = self.Meta.model.objects.create(owner=self.context['request'].user, **validated_data)
team = self.Meta.model.objects.create(
owner=self.context["request"].user, **validated_data
)
if members:
for member in members:
......@@ -142,14 +141,22 @@ class TeamCreationSerializer(serializers.ModelSerializer):
return team
def update(self, instance, validated_data):
instance.short_description = validated_data.get('short_description', instance.short_description)
instance.privacy_level = validated_data.get('privacy_level', instance.privacy_level)
instance.short_description = validated_data.get(
"short_description", instance.short_description
)
instance.privacy_level = validated_data.get(
"privacy_level", instance.privacy_level
)
instance.save()
members = validated_data.get('members', [])
members = validated_data.get("members", [])
members_to_remove = [user for user in instance.members.all() if user not in members]
members_to_add = [user for user in members if user not in instance.members.all()]
members_to_remove = [
user for user in instance.members.all() if user not in members
]
members_to_add = [
user for user in members if user not in instance.members.all()
]
for user in members_to_remove:
instance.members.remove(user)
......@@ -166,9 +173,9 @@ class TeamCreationSerializer(serializers.ModelSerializer):
return Contribution.sanitize_name(value)
def validate_accessibility(self, value):
if value == 'public':
if value == "public":
return Team.PUBLIC
elif value == 'members':
elif value == "members":
return Team.MEMBERS
else:
return Team.PRIVATE
......@@ -181,13 +188,12 @@ class TeamCreationSerializer(serializers.ModelSerializer):
return serializer.data
#----------------------------------------------------------
# ----------------------------------------------------------
class TeamUpdateSerializer(TeamCreationSerializer):
class Meta(TeamCreationSerializer.Meta):
fields = ['short_description', 'accessibility', 'members']
fields = ["short_description", "accessibility", "members"]
def to_representation(self, obj):
serializer = UpdatedTeamSerializer(obj)
......
......@@ -25,25 +25,21 @@
# #
###############################################################################
import json
import os
import shutil
import json
from django.conf import settings
from django.contrib.auth.models import User
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from django.contrib.auth.models import User
from django.conf import settings
from ..common.testutils import tearDownModule # noqa test runner will call it
from ..dataformats.models import DataFormat
from .models import Team
from .serializers import SimpleTeamSerializer
# ----------------------------------------------------------
......
......@@ -26,16 +26,15 @@
###############################################################################
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
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 .models import Team
#----------------------------------------------------------
# ----------------------------------------------------------
@login_required
......@@ -45,14 +44,18 @@ def create(request):
The user must be authenticated before it can add a new team
"""
return render(request,
'team/edition.html',
{
'users': User.objects.exclude(username__in=settings.ACCOUNTS_TO_EXCLUDE_FROM_TEAMS).order_by('username'),
})
return render(
request,
"team/edition.html",
{
"users": User.objects.exclude(
username__in=settings.ACCOUNTS_TO_EXCLUDE_FROM_TEAMS
).order_by("username"),
},
)
#----------------------------------------------------------
# ----------------------------------------------------------
@login_required
......@@ -62,23 +65,27 @@ def edit(request, author_name, name):
The user must be authenticated before it can edit a team
"""
if author_name != request.user.username: raise Http404()
if author_name != request.user.username:
raise Http404()
# Retrieves the team
team = get_object_or_404(Team,
owner__username__iexact=author_name,
name__iexact=name
)
team = get_object_or_404(
Team, owner__username__iexact=author_name, name__iexact=name
)
return render(request,
'team/edition.html',
{
'team': team,
'users': User.objects.exclude(username__in=settings.ACCOUNTS_TO_EXCLUDE_FROM_TEAMS).order_by('username'),
})
return render(
request,
"team/edition.html",
{
"team": team,
"users": User.objects.exclude(
username__in=settings.ACCOUNTS_TO_EXCLUDE_FROM_TEAMS
).order_by("username"),
},
)
#----------------------------------------------------------
# ----------------------------------------------------------
def view(request, author_name, name):
......@@ -86,18 +93,17 @@ def view(request, author_name, name):
"""
team = get_object_or_404(Team, name=name, owner__username=author_name)
return render(request,
'team/team.html',
{'team': team})
return render(request, "team/team.html", {"team": team})
#----------------------------------------------------------
# ----------------------------------------------------------
def ls(request, author_name):
'''List all accessible teams to the request user'''
"""List all accessible teams to the request user"""
if not author_name: return public_ls(request)
if not author_name:
return public_ls(request)
author = get_object_or_404(User, username=author_name)
......@@ -108,25 +114,25 @@ def ls(request, author_name):
else:
objects = Team.objects.for_user(request.user, True).filter(owner=author)
return render(request,
'team/list.html',
dict(
objects=objects,
author=author,
owner=(request.user == author),
))
return render(
request,
"team/list.html",
dict(objects=objects, author=author, owner=(request.user == author),),
)
#----------------------------------------------------------
# ----------------------------------------------------------
def public_ls(request):
'''List all accessible teams to the request user'''
return render(request,
'team/list.html',
dict(
objects=Team.objects.public(),
author=request.user, #anonymous
owner=False,
))
"""List all accessible teams to the request user"""
return render(
request,
"team/list.html",
dict(
objects=Team.objects.public(),
author=request.user, # anonymous
owner=False,
),
)
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