Skip to content

Commit

Permalink
replace django organizations with custom implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Jan 10, 2024
1 parent 0ad29f4 commit ff87ff8
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 106 deletions.
2 changes: 1 addition & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
"treebeard",
"corsheaders",
"rules.apps.AutodiscoverRulesConfig",
"organizations",
"taggit",
"django_filters",
]
Expand Down Expand Up @@ -353,3 +352,4 @@


PROJECTS_PROJECT_MODEL = "nina.Project"
PROJECTS_PROJECTMEMBERSHIP_MODEL = "nina.ProjectMembership"
8 changes: 4 additions & 4 deletions metadata_catalogue/nina/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

@register(Project)
class ProjectAdmin(ModelAdmin):
pass
search_fields = ["name", "slug", "description"]


@register(Department)
class DepartmentAdmin(ModelAdmin):
pass
search_fields = ["name", "slug", "description"]


@register(Category)
class CategoryAdmin(ModelAdmin):
pass
search_fields = ["name"]


@register(Topic)
class TopicAdmin(ModelAdmin):
pass
search_fields = ["name"]
16 changes: 5 additions & 11 deletions metadata_catalogue/nina/libs/harvesters.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,13 @@ def _fetch_paginated_project(url: str, limit=50):

def _process_project(project: dict):
p, created = Project.objects.get_or_create(
extid=project.get("id"),
id=project.get("id"),
defaults={
"extid": project.get("id"),
"name": project.get("title"),
"description": project.get("notes"),
"slug": f'prj-{project.get("id")}',
},
)
if created:
slug = f'prj-{project.get("id")}'
p.slug = slug

p.status = project.get("project_state")
p.budget = project.get("budget")
Expand All @@ -54,17 +51,14 @@ def _process_project(project: dict):
p.customer, _ = Organization.objects.get_or_create(name=project.get("customer"))

for group in project.get("groups"):
slug = f'dpt-{project.get("id")}'
d, created = Department.objects.get_or_create(
extid=group.get("id"),
id=group.get("id"),
defaults={
"name": group.get("title"),
"description": group.get("description"),
"slug": f'dpt-{project.get("id")}',
},
)
if created:
d.slug = slug
d.save()

p.departments.add(d)

Expand All @@ -76,7 +70,7 @@ def _process_project(project: dict):
u.set_unusable_password()
u.save()

p.get_or_add_user(u)
p.members.add(u)


def prosjektoversikt(url: str, limit=50):
Expand Down
94 changes: 65 additions & 29 deletions metadata_catalogue/nina/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Generated by Django 4.2.8 on 2024-01-09 12:01
# Generated by Django 4.2.8 on 2024-01-10 07:47

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django_extensions.db.fields
import taggit.managers


class Migration(migrations.Migration):
initial = True

dependencies = [
("organizations", "0006_alter_organization_slug"),
("datasets", "0006_content_valid"),
("taggit", "0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx"),
("datasets", "0006_content_valid"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
Expand All @@ -25,42 +27,48 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name="Department",
fields=[
("id", models.CharField(max_length=50, primary_key=True, serialize=False)),
("name", models.CharField(max_length=250)),
(
"organization_ptr",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="organizations.organization",
),
"slug",
django_extensions.db.fields.AutoSlugField(blank=True, editable=False, populate_from=["name"]),
),
("extid", models.CharField(max_length=100)),
("description", models.TextField(blank=True, null=True)),
],
bases=("organizations.organization",),
),
migrations.CreateModel(
name="DepartmentMembership",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
],
),
migrations.CreateModel(
name="Project",
fields=[
("name", models.CharField(max_length=250)),
(
"organization_ptr",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="organizations.organization",
),
"slug",
django_extensions.db.fields.AutoSlugField(blank=True, editable=False, populate_from=["name"]),
),
("extid", models.CharField(max_length=100)),
("id", models.CharField(max_length=50, primary_key=True, serialize=False)),
("description", models.TextField(blank=True, null=True)),
("start_date", models.DateField(blank=True, null=True)),
("end_date", models.DateField(blank=True, null=True)),
("budget", models.BigIntegerField(blank=True, null=True)),
("status", models.CharField(blank=True, max_length=50, null=True)),
],
bases=("organizations.organization",),
options={
"abstract": False,
},
),
migrations.CreateModel(
name="ProjectMembership",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
],
options={
"abstract": False,
},
),
migrations.CreateModel(
name="Topic",
Expand All @@ -73,6 +81,20 @@ class Migration(migrations.Migration):
model_name="topic",
constraint=models.UniqueConstraint(models.F("name"), name="unique topic name"),
),
migrations.AddField(
model_name="projectmembership",
name="project",
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.PROJECTS_PROJECT_MODEL),
),
migrations.AddField(
model_name="projectmembership",
name="user",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="projects_membership",
to=settings.AUTH_USER_MODEL,
),
),
migrations.AddField(
model_name="project",
name="category",
Expand All @@ -92,6 +114,11 @@ class Migration(migrations.Migration):
name="departments",
field=models.ManyToManyField(blank=True, related_name="projects", to="nina.department"),
),
migrations.AddField(
model_name="project",
name="members",
field=models.ManyToManyField(blank=True, through="nina.ProjectMembership", to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name="project",
name="tags",
Expand All @@ -107,16 +134,25 @@ class Migration(migrations.Migration):
name="topics",
field=models.ManyToManyField(blank=True, to="nina.topic"),
),
migrations.AddConstraint(
migrations.AddField(
model_name="departmentmembership",
name="department",
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="nina.department"),
),
migrations.AddField(
model_name="departmentmembership",
name="user",
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name="department",
constraint=models.UniqueConstraint(models.F("extid"), name="unique departemt extid"),
name="members",
field=models.ManyToManyField(
blank=True, related_name="members", through="nina.DepartmentMembership", to=settings.AUTH_USER_MODEL
),
),
migrations.AddConstraint(
model_name="category",
constraint=models.UniqueConstraint(models.F("name"), name="unique category name"),
),
migrations.AddConstraint(
model_name="project",
constraint=models.UniqueConstraint(models.F("extid"), name="unique project code"),
),
]
44 changes: 21 additions & 23 deletions metadata_catalogue/nina/models.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
from django.db import models
from organizations.models import Organization
from django_extensions.db.fields import AutoSlugField
from taggit.managers import TaggableManager

from metadata_catalogue.projects.models import BaseProject
from metadata_catalogue.projects.models import BaseProject, BaseProjectMembership

from .conf import settings


class Project(BaseProject):
organization_ptr = models.OneToOneField(
"organizations.Organization",
on_delete=models.CASCADE,
parent_link=True,
primary_key=True,
)
extid = models.CharField(max_length=100)
id = models.CharField(max_length=50, primary_key=True)
description = models.TextField(null=True, blank=True)
start_date = models.DateField(null=True, blank=True)
end_date = models.DateField(null=True, blank=True)
Expand All @@ -25,13 +21,14 @@ class Project(BaseProject):

tags = TaggableManager()

class Meta:
constraints = [models.UniqueConstraint("extid", name="unique project code")]

def __str__(self) -> str:
return self.name


class ProjectMembership(BaseProjectMembership):
pass


class Topic(models.Model):
name = models.CharField(max_length=150)

Expand All @@ -52,18 +49,19 @@ def __str__(self) -> str:
return self.name


class Department(Organization):
organization_ptr = models.OneToOneField(
"organizations.Organization",
on_delete=models.CASCADE,
parent_link=True,
primary_key=True,
)
extid = models.CharField(max_length=100)
class Department(models.Model):
id = models.CharField(max_length=50, primary_key=True)
name = models.CharField(max_length=250)
slug = AutoSlugField(populate_from=["name"])
description = models.TextField(blank=True, null=True)

class Meta:
constraints = [models.UniqueConstraint("extid", name="unique departemt extid")]
members = models.ManyToManyField(
settings.AUTH_USER_MODEL, blank=True, through="nina.DepartmentMembership", related_name="members"
)

def __str__(self) -> str:
return self.name


class DepartmentMembership(models.Model):
department = models.ForeignKey("nina.Department", on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
6 changes: 3 additions & 3 deletions metadata_catalogue/nina/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

urlpatterns = [
path("projects/", ProjectListView.as_view(), name="projects-list"),
path("projects/<slug:slug>/", ProjectDetailView.as_view(), name="projects-detail"),
path("projects/<slug:slug>/edit/", ProjectUpdateView.as_view(), name="projects-edit"),
path("projects/<str:pk>/", ProjectDetailView.as_view(), name="projects-detail"),
path("projects/<str:pk>/edit/", ProjectUpdateView.as_view(), name="projects-edit"),
path("departments/", DepartmentListView.as_view(), name="departments-list"),
path("departments/<slug:slug>/", DepartmentDetailView.as_view(), name="departments-detail"),
path("departments/<str:pk>/", DepartmentDetailView.as_view(), name="departments-detail"),
]
12 changes: 11 additions & 1 deletion metadata_catalogue/projects/admin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from django.contrib.admin import ModelAdmin, site

from .conf import settings
from .models import Project
from .models import Project, ProjectMembership


class ProjectAdmin(ModelAdmin):
pass


class ProjectMembershipAdmin(ModelAdmin):
pass


if not settings.PROJECTS_PROJECT_MODEL or settings.PROJECTS_PROJECT_MODEL == "projects.Project":
site.register(Project, ProjectAdmin)

if (
not settings.PROJECTS_PROJECTMEMBERSHIP_MODEL
or settings.PROJECTS_PROJECTMEMBERSHIP_MODEL == "projects.ProjectMembership"
):
site.register(ProjectMembershipAdmin, ProjectMembershipAdmin)
Loading

0 comments on commit ff87ff8

Please sign in to comment.