Skip to content

Commit

Permalink
Updating my django todo api
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasvallejosdev committed Jan 29, 2024
1 parent 9e576f8 commit 7c140bb
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 136 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions app/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.0 on 2023-07-14 21:57
# Generated by Django 4.0 on 2024-01-25 22:44

from django.db import migrations, models
import uuid
Expand All @@ -18,7 +18,7 @@ class Migration(migrations.Migration):
fields=[
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('user_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('userId', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('email', models.EmailField(max_length=255, unique=True)),
('username', models.CharField(max_length=45, unique=True)),
('password', models.CharField(max_length=255)),
Expand Down
18 changes: 0 additions & 18 deletions app/core/migrations/0002_rename_user_id_user_userid.py

This file was deleted.

7 changes: 5 additions & 2 deletions app/todo_api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Generated by Django 4.0 on 2023-07-14 21:57
# Generated by Django 4.0 on 2024-01-25 22:44

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):
Expand All @@ -17,6 +18,7 @@ class Migration(migrations.Migration):
name='TaskList',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('list_uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
('name', models.CharField(max_length=255)),
('created_at', models.DateTimeField(auto_now_add=True, null=True)),
('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='core.user')),
Expand All @@ -26,12 +28,13 @@ class Migration(migrations.Migration):
name='Task',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('task_uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
('title', models.CharField(max_length=255)),
('completed', models.BooleanField(default=False)),
('due_date', models.DateTimeField(blank=True, null=True)),
('created_at', models.DateTimeField(auto_now_add=True, null=True)),
('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='core.user')),
('task_list', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='todo_api.tasklist')),
('task_list', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='todo_api.tasklist', to_field='list_uuid')),
],
),
]
19 changes: 0 additions & 19 deletions app/todo_api/migrations/0002_tasklist_task_list_uuid.py

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions app/todo_api/migrations/0004_task_task_uuid.py

This file was deleted.

19 changes: 0 additions & 19 deletions app/todo_api/migrations/0005_alter_task_task_list.py

This file was deleted.

Empty file removed app/todo_api/permissions.py
Empty file.
2 changes: 0 additions & 2 deletions app/todo_api/tests/test_task_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def test_retrieve_tasks(self):
create_task(user=self.user)

res = self.client.get(TASKS_URL)
print(res.data)
tasks = Task.objects.all().order_by("created_at")
serializer = TaskSerializer(tasks, many=True)

Expand All @@ -93,7 +92,6 @@ def test_retrieve_tasks_limited_to_user(self):
create_task(user=self.user)

res = self.client.get(TASKS_URL)
print(res.data)

tasks = Task.objects.filter(created_by=self.user).order_by("created_at")
serializer = TaskSerializer(tasks, many=True)
Expand Down
47 changes: 44 additions & 3 deletions app/todo_api/tests/test_task_list_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from venv import create
from django.test import TestCase
from django.urls import reverse
from django.contrib.auth import get_user_model
Expand All @@ -11,7 +12,7 @@
TASKS_LISTS_URL = reverse("todo_api:lists-list")


def list_detail_url_list(list_uuid):
def list_detail_url(list_uuid):
return reverse(
"todo_api:lists-detail",
args=[
Expand Down Expand Up @@ -88,16 +89,56 @@ def test_retrieve_list_pk_from_name(self):
list_2 = create_task_list(user=self.user, name="List 2")
create_task_list(user=self.user, name="List 3")

res = self.client.get(list_detail_url_list(list_2.list_uuid))
res = self.client.get(list_detail_url(list_2.list_uuid))

self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertEqual(list_2.id, res.data["id"])


def test_retrieve_inbox_from_name(self):
"""Test retrieve and create if inbox not exists using slug name"""
url = list_detail_url_list("inbox")
url = list_detail_url("inbox")
res = self.client.get(url)
exists = TaskList.objects.filter(name="inbox").exists()

self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertTrue(exists)

def test_retrieve_unique_inbox_for_each_account(self):
"""Test retrieve and create unique inbox for account"""
user_two = get_user_model().objects.create(
email="[email protected]", password="userexample123"
)
list_inbox = create_task_list(user=user_two, name="inbox")
url = list_detail_url("inbox")
res = self.client.get(url)
exists = TaskList.objects.filter(name="inbox", created_by=self.user).exists()
list = TaskList.objects.filter(name="inbox", created_by=self.user)

self.assertEqual(res.status_code, status.HTTP_200_OK)
self.assertTrue(exists)
self.assertNotEqual(list_inbox.list_uuid, list[0].list_uuid)

def test_fully_update_task_list(self):
"""Test partial update list with patch"""
list = create_task_list(user=self.user)
payload = {
"name": "New name"
}
url = list_detail_url(list.list_uuid)
res = self.client.patch(url, payload)
self.assertEqual(res.status_code, status.HTTP_200_OK)
list.refresh_from_db()
self.assertEqual(list.name, payload["name"])

def test_update_unauthorized_failure(self):
new_user = get_user_model().objects.create_user(
email="[email protected]", password="usernew123"
)
list = create_task_list(user=new_user)
url = list_detail_url(list.list_uuid)
res = self.client.patch(url, {
"name": "new name"
})
self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND)
self.assertNotEqual(list.name, "new name")
3 changes: 2 additions & 1 deletion app/todo_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def retrieve(self, request, list_uuid=None, *args, **kwargs):
try:
if list_uuid == "inbox":
# case inbox os is not created
inbox = TaskList.objects.filter(name="inbox").exists()
inbox = TaskList.objects.filter(name="inbox", created_by=self.request.user).exists() # noqa: E501
if not inbox:
TaskList.objects.create(name="inbox", created_by=self.request.user)
queryset = queryset.get(name__iexact="inbox")
Expand All @@ -155,3 +155,4 @@ def retrieve(self, request, list_uuid=None, *args, **kwargs):
)
serializer = self.get_serializer(queryset, many=False)
return Response(serializer.data)

60 changes: 30 additions & 30 deletions app/todo_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,46 +90,46 @@
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

# MYSQL_DATABASE = os.getenv('MYSQL_DATABASE')
# MYSQL_ROOT_PASSWORD = os.getenv('MYSQL_ROOT_PASSWORD')
# MYSQL_DATABASE_HOST = os.getenv('MYSQL_DATABASE_HOST')
# MYSQL_DATABASE_PORT = os.getenv('MYSQL_DATABASE_PORT')

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.mysql',
# 'NAME': MYSQL_DATABASE,
# 'USER': 'root',
# 'PASSWORD': MYSQL_ROOT_PASSWORD,
# 'HOST': MYSQL_DATABASE_HOST,
# 'PORT': MYSQL_DATABASE_PORT,
# 'TEST': {
# 'NAME': 'test_tododjango',
# },
# }
# }

# if 'test' in sys.argv or 'test_coverage' in sys.argv:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
MYSQL_DATABASE = os.getenv('MYSQL_DATABASE')
MYSQL_ROOT_PASSWORD = os.getenv('MYSQL_ROOT_PASSWORD')
MYSQL_DATABASE_HOST = os.getenv('MYSQL_DATABASE_HOST')
MYSQL_DATABASE_PORT = os.getenv('MYSQL_DATABASE_PORT')

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.mysql',
'NAME': MYSQL_DATABASE,
'USER': 'root',
'PASSWORD': MYSQL_ROOT_PASSWORD,
'HOST': MYSQL_DATABASE_HOST,
'PORT': MYSQL_DATABASE_PORT,
'TEST': {
'NAME': 'test_tododjango',
},
}
}

if 'test' in sys.argv or 'test_coverage' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }

# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', # noqa: E501
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
Expand Down Expand Up @@ -177,7 +177,7 @@
TEST_RUNNER = "redgreenunittest.django.runner.RedGreenDiscoverRunner"

# CORS configuration
CORS_ORIGIN_ALLOW_ALL = True # only for dev environment!, this should be changed before you push to production
CORS_ORIGIN_ALLOW_ALL = True # only for dev environment!, this should be changed before you push to production # noqa: E501

# Default user model
AUTH_USER_MODEL = 'core.User'
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ pytz==2022.7.1

psycopg2-binary==2.9.5
urllib3~=1.26.13
# mysqlclient==2.1.1
# django-mysql==3.8.1
mysqlclient==2.1.1
django-mysql==3.8.1

dj-rest-auth==4.0.1
django-allauth==0.50.0
djangorestframework-simplejwt==5.2.2

PyJWT==2.7.0
flake8==4.0.1
flake8==4.0.1

0 comments on commit 7c140bb

Please sign in to comment.