32
app/settings/migrations/0002_usersettings.py
Normal file
32
app/settings/migrations/0002_usersettings.py
Normal file
@ -0,0 +1,32 @@
|
||||
# Generated by Django 5.0.6 on 2024-05-24 23:50
|
||||
|
||||
import access.fields
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('access', '0002_alter_team_organization'),
|
||||
('settings', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='UserSettings',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False, unique=True)),
|
||||
('created', access.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False)),
|
||||
('modified', access.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False)),
|
||||
('default_organization', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.DO_NOTHING, to='access.organization')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
@ -0,0 +1,37 @@
|
||||
# Generated by Django 5.0.6 on 2024-05-24 23:19
|
||||
|
||||
import access.fields
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from settings.models.user_settings import UserSettings
|
||||
|
||||
def add_user_settings(apps, schema_editor):
|
||||
|
||||
for user in User.objects.all():
|
||||
|
||||
if not UserSettings.objects.filter(pk=user.id).exists():
|
||||
|
||||
user_setting = UserSettings.objects.create(
|
||||
user=user
|
||||
)
|
||||
|
||||
user_setting.save()
|
||||
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('access', '0002_alter_team_organization'),
|
||||
('settings', '0002_usersettings'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(add_user_settings),
|
||||
]
|
52
app/settings/models/user_settings.py
Normal file
52
app/settings/models/user_settings.py
Normal file
@ -0,0 +1,52 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import models
|
||||
|
||||
from access.fields import *
|
||||
from access.models import TenancyObject, Organization
|
||||
|
||||
from core.mixin.history_save import SaveHistory
|
||||
|
||||
|
||||
class UserSettingsCommonFields(models.Model):
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
id = models.AutoField(
|
||||
primary_key=True,
|
||||
unique=True,
|
||||
blank=False
|
||||
)
|
||||
|
||||
slug = None
|
||||
|
||||
created = AutoCreatedField()
|
||||
|
||||
modified = AutoLastModifiedField()
|
||||
|
||||
|
||||
|
||||
class UserSettings(UserSettingsCommonFields):
|
||||
|
||||
user = models.ForeignKey(
|
||||
User,
|
||||
on_delete=models.CASCADE,
|
||||
blank= False,
|
||||
)
|
||||
|
||||
|
||||
default_organization = models.ForeignKey(
|
||||
Organization,
|
||||
on_delete=models.DO_NOTHING,
|
||||
blank= True,
|
||||
default = None,
|
||||
null = True,
|
||||
)
|
||||
|
||||
def is_owner(self, user: int) -> bool:
|
||||
|
||||
if user == self.user:
|
||||
|
||||
return True
|
||||
|
||||
return False
|
50
app/settings/tests/user_settings/test_user_settings.py
Normal file
50
app/settings/tests/user_settings/test_user_settings.py
Normal file
@ -0,0 +1,50 @@
|
||||
|
||||
from django.test import TestCase, Client
|
||||
|
||||
import pytest
|
||||
import unittest
|
||||
import requests
|
||||
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_on_create_of_user_settings_add():
|
||||
""" On Creation of a user their settings are created """
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_on_delete_of_user_settings_removed():
|
||||
""" On Delete of a user their settings are removed """
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_on_delete_organization_default_organization():
|
||||
""" On Delete of an organization, users default organization set to null """
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_on_delete_organization_user_settings_not_deleted():
|
||||
""" On Delete of an organization, users settings are not deleted """
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_only_owner_can_view():
|
||||
""" Only owner can access their settings url """
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_only_owner_can_edit():
|
||||
""" Only owner and super admin can change user settings """
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="to be written")
|
||||
def test_user_settings_super_admin_can_edit():
|
||||
""" Super admin can change user settings """
|
||||
pass
|
||||
|
49
app/settings/views/user_settings.py
Normal file
49
app/settings/views/user_settings.py
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
from django.core.exceptions import PermissionDenied
|
||||
from django.urls import reverse
|
||||
from django.views import generic
|
||||
|
||||
from access.mixin import OrganizationPermission
|
||||
|
||||
from settings.models.user_settings import UserSettings
|
||||
|
||||
|
||||
|
||||
class View(generic.UpdateView):
|
||||
|
||||
context_object_name = "settings"
|
||||
|
||||
fields = [
|
||||
'default_organization',
|
||||
]
|
||||
|
||||
model = UserSettings
|
||||
|
||||
template_name = 'form.html.j2'
|
||||
|
||||
|
||||
def form_valid(self, form):
|
||||
|
||||
if self.object.is_owner(self.request.user):
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
raise PermissionDenied()
|
||||
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
|
||||
return reverse('_settings_user', args=(self.kwargs['pk'],))
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
|
||||
if self.object.is_owner(self.request.user):
|
||||
|
||||
context = super().get_context_data(**kwargs)
|
||||
|
||||
context['content_title'] = 'User Settings'
|
||||
|
||||
return context
|
||||
|
||||
raise PermissionDenied()
|
Reference in New Issue
Block a user