diff options
author | Matthew Lemon <y@yulqen.org> | 2024-07-29 17:06:29 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-07-29 17:06:29 +0100 |
commit | b3d46f2ddbd7312e731e8a05e09a8b547439b2ff (patch) | |
tree | 70fc0e4487dee1ee2f2febf083f35a7a0a38721e | |
parent | 914fb4dd4ab8b7e37c67de01d6a42802999553c1 (diff) |
wip: Adds a new model for additional categories
- Adds new ResourceSubcategory
- Uses this for choices= in the Resource form (create)
- Adds this new model to the admin
Doesn't validate in the edit form yet,
7 files changed, 119 insertions, 9 deletions
diff --git a/pyblackbird_cc/resources/admin.py b/pyblackbird_cc/resources/admin.py index 302f5ca..b885caf 100644 --- a/pyblackbird_cc/resources/admin.py +++ b/pyblackbird_cc/resources/admin.py @@ -1,7 +1,7 @@ # Register your models here. from django.contrib import admin -from pyblackbird_cc.resources.models import Resource +from pyblackbird_cc.resources.models import Resource, ResourceSubcategory from pyblackbird_cc.resources.models import ResourceCategory from pyblackbird_cc.resources.models import ResourceType @@ -15,6 +15,9 @@ class ResourceTypeAdmin(admin.ModelAdmin): class ResourceCategoryAdmin(admin.ModelAdmin): pass +@admin.register(ResourceSubcategory) +class ResourceSubcategoryAdmin(admin.ModelAdmin): + pass @admin.register(Resource) class ResourceAdmin(admin.ModelAdmin): diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py index dbc6df5..efe1ccb 100644 --- a/pyblackbird_cc/resources/forms.py +++ b/pyblackbird_cc/resources/forms.py @@ -6,7 +6,7 @@ from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit from django import forms -from pyblackbird_cc.resources.models import Resource +from pyblackbird_cc.resources.models import Resource, ResourceSubcategory from pyblackbird_cc.resources.models import ResourceCategory from pyblackbird_cc.resources.models import ResourceType from .models import AGE_RANGE_CHOICES @@ -19,7 +19,7 @@ ALLOWED_PDFS = 20 def _create_choices_tuple() -> List[Tuple[str, str]]: - qs_lst = list(ResourceCategory.objects.values_list('name', flat=True)) + qs_lst = list(ResourceSubcategory.objects.values_list('name', flat=True)) res = [] for x in qs_lst: res.append((x, x)) @@ -31,6 +31,7 @@ class ResourceCreateForm(forms.Form): super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.add_input(Submit("submit", "Submit")) + self.fields['additional_resource_category'].choices = _create_choices_tuple() error_css_class = "error" required_css_class = "required" @@ -84,9 +85,9 @@ class ResourceCreateForm(forms.Form): "to one or two to make your resource easier to find.", ) additional_resource_category = forms.MultipleChoiceField( - choices=_create_choices_tuple(), required=False, ) + pdf_files = forms.FileField( widget=forms.TextInput( attrs={ @@ -159,6 +160,7 @@ class ResourceUpdateMetadataForm(forms.ModelForm): super().__init__(*args, **kwargs) self.helper = FormHelper(self) self.helper.add_input(Submit("submit", "Submit")) + self.fields['additional_resource_category'].choices = _create_choices_tuple() error_css_class = "error" required_css_class = "required" diff --git a/pyblackbird_cc/resources/migrations/0014_remove_resource_additional_resource_category_and_more.py b/pyblackbird_cc/resources/migrations/0014_remove_resource_additional_resource_category_and_more.py new file mode 100644 index 0000000..7573bf2 --- /dev/null +++ b/pyblackbird_cc/resources/migrations/0014_remove_resource_additional_resource_category_and_more.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0.4 on 2024-07-29 14:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("resources", "0013_resourcecategory_badge_foreground_colour"), + ] + + operations = [ + migrations.RemoveField( + model_name="resource", + name="additional_resource_category", + ), + migrations.AddField( + model_name="resource", + name="additional_resource_category", + field=models.ManyToManyField( + blank=True, + null=True, + related_name="additional_resource_category", + to="resources.resourcecategory", + ), + ), + ] diff --git a/pyblackbird_cc/resources/migrations/0015_resourcesubcategory.py b/pyblackbird_cc/resources/migrations/0015_resourcesubcategory.py new file mode 100644 index 0000000..3eaada4 --- /dev/null +++ b/pyblackbird_cc/resources/migrations/0015_resourcesubcategory.py @@ -0,0 +1,30 @@ +# Generated by Django 5.0.4 on 2024-07-29 14:55 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("resources", "0014_remove_resource_additional_resource_category_and_more"), + ] + + operations = [ + migrations.CreateModel( + name="ResourceSubcategory", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("name", models.CharField(max_length=255)), + ("created_at", models.DateTimeField(auto_now_add=True)), + ("updated_at", models.DateTimeField(auto_now=True)), + ], + ), + ] diff --git a/pyblackbird_cc/resources/migrations/0016_alter_resourcesubcategory_options_and_more.py b/pyblackbird_cc/resources/migrations/0016_alter_resourcesubcategory_options_and_more.py new file mode 100644 index 0000000..77a3f27 --- /dev/null +++ b/pyblackbird_cc/resources/migrations/0016_alter_resourcesubcategory_options_and_more.py @@ -0,0 +1,31 @@ +# Generated by Django 5.0.4 on 2024-07-29 15:36 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("resources", "0015_resourcesubcategory"), + ] + + operations = [ + migrations.AlterModelOptions( + name="resourcesubcategory", + options={"verbose_name_plural": "Resource Subcategories"}, + ), + migrations.AlterModelOptions( + name="resourcetype", + options={"verbose_name_plural": "Resource Types"}, + ), + migrations.AlterField( + model_name="resource", + name="additional_resource_category", + field=models.ManyToManyField( + blank=True, + null=True, + related_name="additional_resource_category", + to="resources.resourcesubcategory", + ), + ), + ] diff --git a/pyblackbird_cc/resources/models.py b/pyblackbird_cc/resources/models.py index d302ae6..a03992b 100644 --- a/pyblackbird_cc/resources/models.py +++ b/pyblackbird_cc/resources/models.py @@ -46,7 +46,7 @@ class Resource(models.Model): related_name="main_resource_category", ) additional_resource_category = models.ManyToManyField( - "ResourceCategory", + "ResourceSubcategory", null=True, blank=True, related_name="additional_resource_category", @@ -105,6 +105,21 @@ class ResourceType(models.Model): created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) + class Meta: + verbose_name_plural = "Resource Types" + + def __str__(self): + return self.name + + +class ResourceSubcategory(models.Model): + name = models.CharField(max_length=255, null=False) + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + class Meta: + verbose_name_plural = "Resource Subcategories" + def __str__(self): return self.name diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py index db236f0..4381267 100644 --- a/pyblackbird_cc/resources/views.py +++ b/pyblackbird_cc/resources/views.py @@ -19,7 +19,7 @@ from django.shortcuts import render from . import services from .forms import ResourceCreateForm from .forms import ResourceUpdateMetadataForm -from .models import PDFPageSnapshot +from .models import PDFPageSnapshot, ResourceSubcategory from .models import PDFResource from .models import Resource @@ -256,9 +256,11 @@ def create_resource(request): additional_resource_category = form.cleaned_data["additional_resource_category"] feature_slot = form.cleaned_data["feature_slot"] + additional_resource_category_objs = [ResourceSubcategory.objects.create(name=x) for x in additional_resource_category] + try: with transaction.atomic(): - resource = Resource.objects.create( + resource = Resource( name=name, description=description, card_description=card_description, @@ -266,9 +268,10 @@ def create_resource(request): age_range=age_range, curriculum=curriculum, main_resource_category=main_resource_category, - additional_resource_category=additional_resource_category, feature_slot=feature_slot, ) + resource.save() + resource.additional_resource_category.set(additional_resource_category_objs) metadata_generator = create_metadata(pdf_files) snapshotted_pages = [] @@ -290,7 +293,6 @@ def create_resource(request): snapshotted_pages.append(snapshot_images) resource.thumbnail_filenames = [f.name for f in thumbnail_files] - resource.save() # Reset the file pointers for pdf_files for pdf_file in pdf_files: |