Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/jyothiprakashpanaik/issue 43/feat editor table to store which user has access to edit which lists #145

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion codedigger/lists/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from .models import List, ListInfo, Solved, ListExtraInfo, LadderStarted, Enrolled
from .models import List, ListInfo, Solved, ListExtraInfo, LadderStarted, Enrolled, Editor


class ListInfoAdmin(admin.ModelAdmin):
Expand All @@ -26,9 +26,14 @@ class EnrolledAdmin(admin.ModelAdmin):
search_fields = ('enroll__user', 'enroll__list')


class EditorAdmin(admin.ModelAdmin):
search_fields = ('editor__user', 'editor__list')


admin.site.register(List, ListAdmin)
admin.site.register(ListInfo, ListInfoAdmin)
admin.site.register(Solved, SolvedAdmin)
admin.site.register(ListExtraInfo, ListExtraInfoAdmin)
admin.site.register(LadderStarted, LadderStartedAdmin)
admin.site.register(Enrolled, EnrolledAdmin)
admin.site.register(Editor, EditorAdmin)
10 changes: 10 additions & 0 deletions codedigger/lists/fixtures/editor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"model": "lists.editor",
"pk": 1,
"fields": {
"editor_user": 1,
"editor_list": 3
}
}
]
15 changes: 14 additions & 1 deletion codedigger/lists/fixtures/lists.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,25 @@
"pk": 3,
"fields": {
"name": "testinglist_userlist",
"owner": 1,
"owner": 3,
"isAdmin": false,
"isTopicWise": true,
"type_list": "3",
"public": false,
"slug": "testinglist_userlist"
}
},
{
"model": "lists.list",
"pk": 4,
"fields": {
"name": "testinglist_userlist1",
"owner": 1,
"isAdmin": false,
"isTopicWise": true,
"type_list": "3",
"public": false,
"slug": "testinglist_userlist1"
}
}
]
15 changes: 15 additions & 0 deletions codedigger/lists/fixtures/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,20 @@
"created_at": "2021-10-1T10:00:00.511Z",
"updated_at": "2021-10-1T10:00:00.511Z"
}
},
{
"model": "user.user",
"pk": 3,
"fields": {
"username": "testinguser2",
"email": "[email protected]",
"password": "QWERTY@123",
"is_verified": true,
"is_active": true,
"is_staff": false,
"auth_provider": "email",
"created_at": "2021-10-1T10:00:00.511Z",
"updated_at": "2021-10-1T10:00:00.511Z"
}
}
]
34 changes: 34 additions & 0 deletions codedigger/lists/migrations/0008_editormodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 3.2.8 on 2022-01-14 12:17

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('lists', '0007_auto_20211227_1032'),
]

operations = [
migrations.CreateModel(
name='EditorModel',
fields=[
('id',
models.AutoField(auto_created=True,
primary_key=True,
serialize=False,
verbose_name='ID')),
('editor_list',
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
related_name='editor_list',
to='lists.list')),
('editor_user',
models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
related_name='editor_user',
to=settings.AUTH_USER_MODEL)),
],
),
]
19 changes: 19 additions & 0 deletions codedigger/lists/migrations/0009_rename_editormodel_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.2.8 on 2022-01-14 12:18

from django.conf import settings
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('lists', '0008_editormodel'),
]

operations = [
migrations.RenameModel(
old_name='EditorModel',
new_name='Editor',
),
]
13 changes: 13 additions & 0 deletions codedigger/lists/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,16 @@ class Enrolled(models.Model):

def __str__(self):
return self.enroll_list.slug


class Editor(models.Model):
editor_user = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name="editor_user")
editor_list = models.ForeignKey(List,
on_delete=models.CASCADE,
related_name="editor_list")

def __str__(self):
return str(self.editor_user) + " can edit list " + str(
self.editor_list)
14 changes: 12 additions & 2 deletions codedigger/lists/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers, status
from .models import ListInfo, Solved, List, ListInfo, LadderStarted
from .models import ListInfo, Solved, List, ListInfo, LadderStarted, Editor
from problem.models import Problem
from user.models import User, Profile
from drf_writable_nested.serializers import WritableNestedModelSerializer
Expand Down Expand Up @@ -107,7 +107,8 @@ class GetUserlistSerializer(serializers.ModelSerializer):

class Meta:
model = List
fields = ('id', 'name', 'description', 'slug', 'public')
fields = ('id', 'name', 'description', 'slug')
# fields = ('id', 'name', 'description', 'slug', 'public')


class CreateUserlistSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -208,3 +209,12 @@ class EnrollInListSerializer(serializers.Serializer):

class Meta:
fields = ('slug', )


class EditorListSerializer(serializers.Serializer):
slug = serializers.CharField(required=True)
friend = serializers.CharField(required=True)

class Meta:
model = Editor
fields = ('slug', 'friend')
62 changes: 38 additions & 24 deletions codedigger/lists/tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_check_owner_can_change_visibility_view(self):
res2.status_code, 400)

def test_check_restrict_change_visibility_view(self):
slug = "testinglist_userlist"
slug = "testinglist_userlist1"
test_url = reverse('userlist-edit', kwargs={'slug': slug})
here = User.objects.get(username="testing")
here.set_password(self.user_data['password'])
Expand Down Expand Up @@ -84,11 +84,13 @@ def test_check_only_owner_can_add_problems_view(self):
'platform': 'A'
}
res2 = client2.post(test_url, data2, format="json")
self.assertEqual(res.status_code, 200) and self.assertEqual(
res2.status_code, 400)
self.assertEqual(res.status_code, 200) and \
self.assertEqual(res.data['result'],"Given problem has been added to the list") and \
self.assertEqual(res2.status_code, 400) and \
self.assertRaises(ValidationException, res)

def test_check_owner_can_change_visibility_view(self):
slug = "testinglist_userlist"
slug = "testinglist_userlist1"
test_url = reverse('problem-publiclist', kwargs={'slug': slug})
here = User.objects.get(username="testing")
here.set_password(self.user_data['password'])
Expand Down Expand Up @@ -146,26 +148,38 @@ def test_get_user_stats(self):
res.data['result'][0]['problems_solved'],
res.data['result'][1]['problems_solved'])

def test_add_editors(self):
slug = "testinglist_topicwise"
friend = "testinguser"
username = "testing"
test_url = reverse('add-users')
token = self.login(self.client, self.login_url, self.user_data)
client = self.get_authenticated_client(token)
res = client.post(test_url, {
"slug": slug,
"friend": friend
},
format="json")
self.assertEqual(res.data['result'], "User has been added to the list")

def test_get_user_list(self):
username = "testing"
test_url = reverse('user-list', kwargs={'username': username})
here = User.objects.get(username="testing")
here.set_password(self.user_data['password'])
here.save()
res = self.client.post(self.login_url, self.user_data, format="json")
token = res.data['tokens']['access']
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
res = client.get(test_url, format="json")
def test_get_user_list(self):
username = "testing"
test_url = reverse('user-list', kwargs={'username': username})
here = User.objects.get(username="testing")
here.set_password(self.user_data['password'])
here.save()
res = self.client.post(self.login_url, self.user_data, format="json")
token = res.data['tokens']['access']
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
res = client.get(test_url, format="json")

username2 = "testing1"
test_url = reverse('user-list', kwargs={'username': username2})
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
res2 = client.get(test_url, format="json")
username2 = "testing1"
test_url = reverse('user-list', kwargs={'username': username2})
client = APIClient()
client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)
res2 = client.get(test_url, format="json")

self.assertEqual(res.status_code, 200) and self.assertRaises(
ValidationException, res2)
if (len(res.data['result']) > 0):
self.assertEqual(res.data['result'][0]['public'], True)
self.assertEqual(res.status_code, 200) and self.assertEqual(
len(res.data['result']), 3) and self.assertRaises(
ValidationException, res2)
2 changes: 1 addition & 1 deletion codedigger/lists/tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class TestSetUp(APITestCase):
fixtures = [
"user.json", "problems.json", "lists.json", "list_info.json",
"enrolled.json", "solved.json", "userfriends.json"
"enrolled.json", "solved.json", "userfriends.json", "editor.json"
]

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions codedigger/lists/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SearchUserlistView,
ListGetView,
EnrollListView,
UserListEdit,
testing)

urlpatterns = [
Expand Down Expand Up @@ -69,5 +70,6 @@
path('<str:slug>/stats/standing',
UserStandingStats.as_view(),
name='user-standing'),
path('add', UserListEdit.as_view(), name='add-users'),
path('testing', testing),
]
Loading