SMART-19 Merge pull request 'develop' (#8) from develop into master

Reviewed-on: #8
This commit is contained in:
Patryk Dolata 2020-12-17 17:03:00 +01:00
commit 7e9553eeca
19 changed files with 195 additions and 3 deletions

View File

@ -22,4 +22,4 @@ To run tests:
`pip3 install -r requirements.txt` `pip3 install -r requirements.txt`
`python3 app.py` `python3 app.py`

Binary file not shown.

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class ProjectConfig(AppConfig):
name = 'smartpicasso.app.project'

View File

@ -0,0 +1,31 @@
# Generated by Django 3.1.3 on 2020-12-14 19:08
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Project',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('name', models.CharField(max_length=100)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'project',
},
),
]

View File

@ -0,0 +1,29 @@
"""
@author p.dolata
"""
import uuid
from django.db import models
from smartpicasso.app.user.models import User
class Project(models.Model):
"""
Model representing user's project
"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Meta:
"""
Meta to set table name in database
"""
db_table = 'project'

View File

@ -0,0 +1,17 @@
"""
@author p.dolata
"""
from rest_framework import serializers
from smartpicasso.app.project.models import Project
class ProjectSerializer(serializers.ModelSerializer):
"""
Class to manage serializing Project
"""
class Meta:
model = Project
fields = ('id', 'name', 'created_at', 'updated_at')

View File

@ -0,0 +1,70 @@
"""
@author: p.dolata
"""
from datetime import datetime
from django.test import TestCase
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase, APIClient
from smartpicasso.app.project.models import Project
from smartpicasso.app.user.models import User
class ProjectsApiTest(APITestCase):
client = APIClient()
projects_url = reverse('projects-list')
new_project = {'name': 'test_project'}
def test_create_project_without_auth(self):
response = self.client.post(self.projects_url, self.new_project, format='json')
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_create_project_with_invalid_token(self):
self.client.force_authenticate(user=None)
response = self.client.post(self.projects_url, self.new_project, format='json')
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_create_project(self):
user = User.objects.create_user(email='test@test.com', password='test')
self.client.force_authenticate(user=user)
response = self.client.post(self.projects_url, self.new_project, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
project = Project.objects.get(id=response.data['id'])
self.assertEqual(project.user.id, user.id)
self.assertEqual(project.name, self.new_project['name'])
self.assertAlmostEqual(datetime.timestamp(project.created_at), datetime.timestamp(datetime.now()),
delta=600)
self.assertAlmostEqual(datetime.timestamp(project.updated_at), datetime.timestamp(datetime.now()),
delta=600)
def test_get_user_projects_without_auth(self):
response = self.client.get(self.projects_url, format='json')
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_get_user_projects_with_invalid_token(self):
self.client.force_authenticate(user=None)
response = self.client.get(self.projects_url, format='json')
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_get_user_projects(self):
user = User.objects.create_user(email='test@test.com', password='test')
self.client.force_authenticate(user=user)
Project.objects.create(user=user, name='project_1')
Project.objects.create(user=user, name='project_2')
response = self.client.get(self.projects_url, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
projects = response.data
self.assertEqual(len(projects), 2)
self.assertEqual(projects[0]['name'], 'project_1')
self.assertEqual(projects[1]['name'], 'project_2')
class ProjectTest(TestCase):
def test_project_str(self):
user = User.objects.create_user(email='test@test.com', password='test')
project = Project.objects.create(user=user, name='test_project')
self.assertEqual(str(project), 'test_project')

View File

@ -0,0 +1,11 @@
"""
@author: p.dolata
"""
from rest_framework.routers import SimpleRouter
from smartpicasso.app.project.views import ProjectsView
router = SimpleRouter(trailing_slash=False)
router.register('projects', ProjectsView, basename="projects")
urlpatterns = router.urls

View File

@ -0,0 +1,24 @@
"""
@author p.dolata
"""
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from smartpicasso.app.project.serializers import ProjectSerializer
from smartpicasso.app.project.models import Project
class ProjectsView(ModelViewSet):
"""
View for project endpoints
"""
permission_classes = (IsAuthenticated,)
serializer_class = ProjectSerializer
def perform_create(self, serializer):
serializer.save(user=self.request.user)
def get_queryset(self):
user = self.request.user
return Project.objects.filter(user=user)

View File

@ -22,6 +22,6 @@ class UserProfile(models.Model):
class Meta: class Meta:
""" """
Meta to se table name in database Meta to set table name in database
""" """
db_table = 'user_profile' db_table = 'user_profile'

View File

@ -38,7 +38,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'smartpicasso.app.user', 'smartpicasso.app.user',
'smartpicasso.app.user_profile' 'smartpicasso.app.user_profile',
'smartpicasso.app.project'
] ]
MIDDLEWARE = [ MIDDLEWARE = [

View File

@ -19,5 +19,6 @@ from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('api/', include('smartpicasso.app.user.urls')), path('api/', include('smartpicasso.app.user.urls')),
path('api/', include('smartpicasso.app.user_profile.urls')), path('api/', include('smartpicasso.app.user_profile.urls')),
path('api/', include('smartpicasso.app.project.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]