This commit is contained in:
virusdefender 2018-05-07 08:06:29 +08:00
parent 29f75a011b
commit aad46b67cc
12 changed files with 102 additions and 1 deletions

View File

@ -48,7 +48,7 @@ class admin_role_required(BasePermissionDecorator):
class problem_permission_required(admin_role_required):
def check_permission(self):
if not super(problem_permission_required, self).check_permission():
if not super().check_permission():
return False
if self.request.user.problem_permission == ProblemPermission.NONE:
return False

0
group/__init__.py Normal file
View File

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2018-05-04 02:10
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Group',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.TextField()),
('description', models.TextField()),
('create_time', models.DateTimeField(auto_now_add=True)),
('created_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='my_groups', to=settings.AUTH_USER_MODEL)),
('members', models.ManyToManyField(to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'group',
},
),
]

View File

13
group/models.py Normal file
View File

@ -0,0 +1,13 @@
from django.db import models
from account.models import User
class Group(models.Model):
name = models.TextField()
description = models.TextField()
create_time = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, related_name="my_groups")
members = models.ManyToManyField(User)
class Meta:
db_table = "group"

12
group/serializers.py Normal file
View File

@ -0,0 +1,12 @@
from utils.api import serializers
from utils.api._serializers import UsernameSerializer
from .models import Group
class GroupSerializer(serializers.ModelSerializer):
created_by = UsernameSerializer()
class Meta:
model = Group
fields = "__all__"

0
group/tests.py Normal file
View File

0
group/urls/__init__.py Normal file
View File

7
group/urls/admin.py Normal file
View File

@ -0,0 +1,7 @@
from django.conf.urls import url
from ..views.admin import GroupAPI
urlpatterns = [
url(r"^group/?$", GroupAPI.as_view(), name="group_api"),
]

0
group/views/__init__.py Normal file
View File

35
group/views/admin.py Normal file
View File

@ -0,0 +1,35 @@
from account.decorators import admin_role_required, ensure_created_by
from utils.api import APIView, validate_serializer
from ..models import Group
from ..serializers import GroupSerializer
class GroupAPI(APIView):
@admin_role_required
def get(self, request):
_id = request.GET.get("id")
keyword = request.GET.get("keyword")
user = request.user
if _id:
try:
group = Group.objects.get(id=_id)
ensure_created_by(group, user)
return self.success(GroupSerializer(group).data)
except Group.DoesNotExist:
return self.error("Group does not exist")
groups = Group.objects.all().order_by("-create_time")
if not user.is_super_admin():
groups = groups.filter(created_by=user)
if keyword:
groups = groups.filter(name__contains=keyword)
return self.success(self.paginate_data(request, groups, GroupSerializer))
def post(self, request):
pass
def put(self, request):
pass
def delete(self, request):
pass

View File

@ -14,4 +14,5 @@ urlpatterns = [
url(r"^api/", include("submission.urls.oj")),
url(r"^api/admin/", include("submission.urls.admin")),
url(r"^api/admin/", include("utils.urls")),
url(r"^api/admin/", include("group.urls.admin"))
]