Skip to content

Commit

Permalink
spike: [nomerge] spike on restricted run modeling
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Sep 26, 2024
1 parent 4db5daa commit 6d14e72
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
37 changes: 37 additions & 0 deletions enterprise_catalog/apps/catalog/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
ContentMetadata,
EnterpriseCatalog,
EnterpriseCatalogRoleAssignment,
Library,
Book,
BookSpecialCopies,
RestrictedBook,
)


Expand Down Expand Up @@ -167,3 +171,36 @@ class Meta:

fields = ('user', 'role', 'enterprise_id', 'applies_to_all_contexts')
form = EnterpriseCatalogRoleAssignmentAdminForm


@admin.register(Library)
class LibraryAdmin(admin.ModelAdmin):
"""
"""

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
"""
"""

@admin.register(BookSpecialCopies)
class BookAdmin(admin.ModelAdmin):
"""
"""

@admin.register(RestrictedBook)
class RestrictedBookAdmin(admin.ModelAdmin):
"""
"""
list_display = [
'parent',
'library',
'data',
]

fields = [
'parent',
'library',
'data',
'title',
]
55 changes: 55 additions & 0 deletions enterprise_catalog/apps/catalog/migrations/0040_spike.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Generated by Django 4.2.15 on 2024-09-26 14:33

import collections
from django.db import migrations, models
import django.db.models.deletion
import jsonfield.encoder
import jsonfield.fields


class Migration(migrations.Migration):

dependencies = [
('catalog', '0039_alter_catalogquery_unique_together_and_more'),
]

operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255, unique=True)),
('data', jsonfield.fields.JSONField(blank=True, default={}, dump_kwargs={'cls': jsonfield.encoder.JSONEncoder, 'indent': 4, 'separators': (',', ':')}, load_kwargs={'object_pairs_hook': collections.OrderedDict}, null=True)),
],
),
migrations.CreateModel(
name='BookSpecialCopies',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('book', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='special_copies', to='catalog.book')),
],
),
migrations.CreateModel(
name='Library',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255, unique=True)),
],
),
migrations.CreateModel(
name='RestrictedBook',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255, unique=True)),
('data', jsonfield.fields.JSONField(blank=True, default={}, dump_kwargs={'cls': jsonfield.encoder.JSONEncoder, 'indent': 4, 'separators': (',', ':')}, load_kwargs={'object_pairs_hook': collections.OrderedDict}, null=True)),
('libraries', models.ManyToManyField(to='catalog.library')),
('library', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='special_copies', to='catalog.library')),
('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='catalog.bookspecialcopies')),
],
),
migrations.AddField(
model_name='book',
name='libraries',
field=models.ManyToManyField(to='catalog.library'),
),
]
105 changes: 105 additions & 0 deletions enterprise_catalog/apps/catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,3 +1197,108 @@ def current_options(cls):
'no_async': current_config.no_async,
}
return {}


class Library(models.Model):
class Meta:
app_label = 'catalog'

name = models.CharField(
max_length=255,
blank=False,
null=False,
unique=True,
)

def __str__(self):
return self.name


class AbstractBook(models.Model):
class Meta:
abstract = True

title = models.CharField(
max_length=255,
blank=False,
null=False,
unique=True,
)
libraries = models.ManyToManyField(Library)
data = JSONField(
default={},
blank=True,
null=True,
load_kwargs={'object_pairs_hook': collections.OrderedDict},
dump_kwargs={'indent': 4, 'cls': JSONEncoder, 'separators': (',', ':')},
)


class Book(AbstractBook):
class Meta:
app_label = 'catalog'

def __str__(self):
return f"title: {self.title}, data={self.data}"


class BookSpecialCopies(models.Model):
"""
Intermediary that fan's out from a canonical book
to zero or more special RestrictedBooks copies of the book.
"""
class Meta:
app_label = 'catalog'

book = models.OneToOneField(
Book,
related_name='special_copies',
on_delete=models.deletion.CASCADE,
)

def __str__(self):
return f"book={self.book}"


class RestrictedBook(AbstractBook):
"""
A copy of a book that's available only at a specific library.
"""
class Meta:
app_label = 'catalog'

parent = models.ForeignKey(
BookSpecialCopies,
on_delete=models.deletion.CASCADE,
)
library = models.ForeignKey(
Library,
blank=False,
null=True,
related_name='special_copies',
on_delete=models.deletion.CASCADE,
)

def save(self, *args, **kwargs):
if not self.id:
super().save(*args, **kwargs)

self.title = self.parent.book.title
super().save(*args, **kwargs)

def __str__(self):
return f"title: {self.title}, specific_library: {self.library}, data={self.data}"

"""
from enterprise_catalog.apps.catalog.objects import *
boston_pl = Library.objects.get(name='Boston Public Library')
quincy_pl = Library.objects.get(name='Quincy Public Library')
boston_copy = boston_pl.books.get(title='Moby Dick')
boston_copy.library == boston_pl # False
quincy_copy = quincy_pl.books.get(title='Moby Dick')
quincy_copy.library == quincy_pl # True
quincy_copy.restrictedbook.data_override
"""

0 comments on commit 6d14e72

Please sign in to comment.