-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Society to societies data migration and fields
Relatively big change. We remove the society OneToOneField, and replace it with a ManyToManyField supported by a Employment "via" class. This means that in addition to querying costing a bit more, although c'mon, we're not big, we have to create employment relations between workers to facilitate who they work for. On the plus side we can now have workers work for many different societies, which is really convenienent for the few that do, and for us wanting a single unified employment relationship. After all, there's no reason a different individual should in any way be duplicated just because of an oversight on our part in terms of use cases.
- Loading branch information
Showing
11 changed files
with
216 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[ | ||
{ | ||
"model": "society.employment", | ||
"pk": 1, | ||
"fields": { | ||
"worker": 1, | ||
"society": 1, | ||
"active": true | ||
} | ||
}, | ||
{ | ||
"model": "society.employment", | ||
"pk": 2, | ||
"fields": { | ||
"worker": 2, | ||
"society": 1, | ||
"active": true | ||
} | ||
}, | ||
{ | ||
"model": "society.employment", | ||
"pk": 3, | ||
"fields": { | ||
"worker": 3, | ||
"society": 2, | ||
"active": true | ||
} | ||
}, | ||
{ | ||
"model": "society.employment", | ||
"pk": 4, | ||
"fields": { | ||
"worker": 4, | ||
"society": 2, | ||
"active": true | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 15 additions & 4 deletions
19
spbm/apps/society/forms/worker.py → spbm/apps/society/forms/workers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
spbm/apps/society/migrations/0002_alter_and_create_employment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import django | ||
from django.db import migrations, models | ||
|
||
|
||
def create_employment(apps, schema_editor): | ||
""" One-way employment creation from workers """ | ||
Worker = apps.get_model("society", "Worker") | ||
Employment = apps.get_model("society", "Employment") | ||
db_alias = schema_editor.connection.alias | ||
for worker in Worker.objects.using(db_alias).all(): | ||
Employment.objects.create(worker=worker, society=worker.society, active=worker.active) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('society', '0001_squashed_app_merges'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='event', | ||
name='date', | ||
field=models.DateField(help_text='The date of the event in the format of <em>YYYY-MM-DD</em>.', verbose_name='event date'), | ||
), | ||
migrations.AlterField( | ||
model_name='event', | ||
name='name', | ||
field=models.CharField(help_text='Name or title describing the event.', max_length=100, verbose_name='name'), | ||
), | ||
migrations.AlterField( | ||
model_name='event', | ||
name='registered', | ||
field=models.DateField(auto_now_add=True, help_text='Date of event registration.', verbose_name='registered'), | ||
), | ||
migrations.AlterField( | ||
model_name='shift', | ||
name='hours', | ||
field=models.DecimalField(decimal_places=2, max_digits=10, validators=[django.core.validators.MinValueValidator(0.25)], verbose_name='hours'), | ||
), | ||
migrations.AlterField( | ||
model_name='shift', | ||
name='wage', | ||
field=models.DecimalField(decimal_places=2, max_digits=10, validators=[django.core.validators.MinValueValidator(10)], verbose_name='wage'), | ||
), | ||
migrations.CreateModel( | ||
name='Employment', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('active', models.BooleanField(default=True)), | ||
('society', models.ForeignKey(on_delete=models.PROTECT, to='society.Society')), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='worker', | ||
name='societies', | ||
field=models.ManyToManyField(related_name='workers', through='society.Employment', to='society.Society'), | ||
), | ||
migrations.AddField( | ||
model_name='employment', | ||
name='worker', | ||
field=models.ForeignKey(on_delete=models.CASCADE, to='society.Worker'), | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='employment', | ||
unique_together=set([('worker', 'society')]), | ||
), | ||
migrations.RunPython( | ||
create_employment, | ||
), | ||
migrations.RemoveField( | ||
model_name='worker', | ||
name='society', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.contrib.auth.models import User, Permission | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from spbm.apps.accounts.models import SpfUser | ||
from spbm.apps.society.models import Society, Worker | ||
from . import test_fixtures | ||
|
||
|
||
class WorkerTests(TestCase): | ||
fixtures = test_fixtures | ||
|
||
def setUp(self): | ||
# Logging into the site as a test in itself can be somewhere else. This doesn't need it. | ||
self.user = User(username='cyb') | ||
self.user.save() | ||
self.spf_user = SpfUser(user=self.user, society=Society.objects.get(pk=2)) | ||
self.spf_user.save() | ||
self.client.force_login(self.user) | ||
|
||
@classmethod | ||
def setUpTestData(cls): | ||
cls.test = True | ||
cls.society_name = 'CYB' | ||
|
||
def test_create_worker(self): | ||
# TODO: finish test | ||
worker = { | ||
'name': 'Ola Normann', | ||
'norlonn_number': '100', | ||
'person_id': '01020394311', | ||
|
||
} | ||
self.assertEqual(self.client.post(reverse('worker-create', args=[self.society_name]), data=worker).status_code, | ||
403) | ||
self.user.user_permissions.add(Permission.objects.get(codename='add_worker')) | ||
last_worker = Worker.objects.last() | ||
post = self.client.post(reverse('worker-create', args=[self.society_name]), data=worker) | ||
self.assertNotEqual(last_worker, Worker.objects.last()) | ||
self.assertEqual(post.status_code, 200) | ||
|
||
def test_create_worker_duplicate_norlonn(self): | ||
pass | ||
|
||
def test_create_worker_duplicate_national_id(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
def user_allowed_society(usr, soc): | ||
if usr.is_superuser: | ||
def user_allowed_society(user, target_society): | ||
# Superusers go straight ahead | ||
if user.is_superuser: | ||
return True | ||
|
||
if usr.spfuser.society == soc: | ||
# Checking that we're the same society | ||
if user.spfuser.society == target_society: | ||
return True | ||
|
||
# Checking whether the user is one of the societies | ||
if hasattr(target_society, 'all') and user.spfuser.society in target_society.all(): | ||
return True | ||
|
||
return False | ||
|
||
|
||
def user_society(request): | ||
return request.user.spfuser.society | ||
return request.user.spfuser.society |