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

Change users models #66

Merged
merged 12 commits into from
Nov 26, 2021
8 changes: 4 additions & 4 deletions tasks/migrations/0002_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class Migration(migrations.Migration):
),
migrations.AddField(
model_name='comment',
name='task_id',
name='task',
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to='tasks.task'),
on_delete=django.db.models.deletion.CASCADE, to='tasks.task', related_name='task'),
),
migrations.AddField(
model_name='comment',
name='user_id',
field=models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='comments',
name='appUser',
field=models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='appUser',
to='users.user'),
),
]
12 changes: 8 additions & 4 deletions tasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class Priority(ChoiceEnum):
class Task(models.Model):
title = models.CharField(max_length=200)
created_date = models.DateTimeField(auto_now_add=True)
assignee = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='assigneeTasks')
created_by = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='createdTasks')
assignee = models.ForeignKey(
User, on_delete=models.RESTRICT, related_name='assigneeTasks')
created_by = models.ForeignKey(
User, on_delete=models.RESTRICT, related_name='createdTasks')
AvivLiberman marked this conversation as resolved.
Show resolved Hide resolved
priority = EnumChoiceField(Priority, default=Priority.LOW, max_length=1)
status = EnumChoiceField(Status, default=Status.BACKLOG, max_length=1)
description = models.TextField(null=True, blank=True)
Expand All @@ -30,8 +32,10 @@ def __str__(self) -> str:


class Comment(models.Model):
user_id = models.ForeignKey(User, on_delete=models.RESTRICT, related_name='comments')
task_id = models.ForeignKey(Task, on_delete=models.CASCADE)
appUser = models.ForeignKey(
User, on_delete=models.RESTRICT, related_name='appUser')
task = models.ForeignKey(
Task, on_delete=models.CASCADE, related_name='task')
title = models.CharField(max_length=200)
created_date = models.DateTimeField(auto_now_add=True)
description = models.TextField(null=True, blank=True)
Expand Down
2 changes: 0 additions & 2 deletions tests.py

This file was deleted.

7 changes: 2 additions & 5 deletions users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ class Migration(migrations.Migration):
('user',
models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False,
to='auth.user')),
('email', models.EmailField(max_length=255)),
('first_name', models.CharField(max_length=255)),
('last_name', models.CharField(max_length=255)),
('role', enumchoicefield.fields.EnumChoiceField(default=users.models.Role.EMPLOYEE,
enum_class=users.models.Role, max_length=1)),
('team_id', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='users',
to='users.team')),
('team', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='team',
to='users.team')),
],
),
]
23 changes: 17 additions & 6 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from enumchoicefield import ChoiceEnum, EnumChoiceField
from django.conf import settings
from django.contrib.auth.models import User as DjangoUser


# Enum
Expand All @@ -21,12 +22,22 @@ def __str__(self) -> str:
class User(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True)
email = models.EmailField(max_length=255)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
role = EnumChoiceField(Role, default=Role.EMPLOYEE, max_length=1)
team_id = models.ForeignKey(
Team, on_delete=models.RESTRICT, related_name='users')
team = models.ForeignKey(
Team, on_delete=models.RESTRICT, related_name='team')

@staticmethod
def create_user(username, email, password, first_name, last_name, role, team):
django_user = DjangoUser.objects.create_user(username=username,
email=email,
password=password,
first_name=first_name,
last_name=last_name)
user = User(user=django_user,
role=role,
team=team)
user.save()
return user

def __str__(self) -> str:
return self.first_name
return self.user.username
70 changes: 68 additions & 2 deletions users/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,69 @@
# from django.test import TestCase
import pytest
from users.models import Role, User, Team

# Create your tests here.

@pytest.mark.django_db
class TestTeams:

@pytest.fixture
def valid_teams(self):
team_1 = Team.objects.create(
name="Team1", description="Best team ever")
team_2 = Team.objects.create(
name="Team2", description="Best team ever")
team_3 = Team.objects.create(
name="Team3", description="Best team ever")
return team_1, team_2, team_3

def test_create_teams(self, valid_teams):
for team in valid_teams:
assert isinstance(team, Team)
assert Team.objects.filter(id=team.id).exists()

def test_create_team_without_title(self):
with pytest.raises(Exception):
Team.objects.create('', "Best team ever")


@pytest.mark.django_db
class TestUsers:

@pytest.fixture
def team_1(self):
return Team.objects.create(name="Team1", description="Best team ever")

@pytest.fixture
def employee_1(self, team_1):
user1 = User.create_user(
username="user1",
email="[email protected]",
password="password",
first_name="first_name",
last_name="last_name",
role=Role.EMPLOYEE,
team=team_1)
return user1

def test_create_user(self, employee_1):
assert isinstance(employee_1, User)
assert User.objects.filter(user=employee_1.user).exists()

def test_create_user_without_email(self):
with pytest.raises(Exception):
User.create_user(
username="user1",
email="[email protected]",
password="password",
last_name="last_name",
role=Role.EMPLOYEE,
team=TestTeams.valid_team)

def test_create_user_with_long_name(self):
with pytest.raises(Exception):
User.create_user(
username="user1",
email="[email protected]",
password="password",
last_name="last_name",
role=Role.EMPLOYEE,
team=TestTeams.valid_team)