diff options
author | Matthew Lemon <y@yulqen.org> | 2024-08-01 16:17:23 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-08-01 16:17:23 +0100 |
commit | bad2be31054ff5277b97230b5157edf6a8dd06cc (patch) | |
tree | dc9072a73d586fb00671f742a2e9054fec37f230 | |
parent | 5f37a5839ff00f4f85c5e63e4dcb8246204c9386 (diff) |
Fix bug where new subcategories were being created on selection
5 files changed, 56 insertions, 12 deletions
diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py index e905944..be90a00 100644 --- a/pyblackbird_cc/resources/forms.py +++ b/pyblackbird_cc/resources/forms.py @@ -20,6 +20,7 @@ ALLOWED_PDFS = 20 def _create_choices_tuple() -> List[Tuple[str, str]]: qs_lst = list(ResourceSubcategory.objects.values_list('name', flat=True)) + qs_lst.sort() res = [] for x in qs_lst: res.append((x, x)) @@ -31,7 +32,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() + self.fields['subcategories'].choices = _create_choices_tuple() error_css_class = "error" required_css_class = "required" @@ -84,7 +85,7 @@ class ResourceCreateForm(forms.Form): "searches. It's a good idea to limit the number of subjects you select " "to one or two to make your resource easier to find.", ) - additional_resource_category = forms.MultipleChoiceField( + subcategories = forms.MultipleChoiceField( required=False, ) @@ -174,6 +175,6 @@ class ResourceUpdateMetadataForm(forms.ModelForm): "age_range", "curriculum", "main_resource_category", - "additional_resource_category", + "subcategories", "feature_slot", ] diff --git a/pyblackbird_cc/resources/migrations/0017_rename_additional_resource_category_resource_subcategories.py b/pyblackbird_cc/resources/migrations/0017_rename_additional_resource_category_resource_subcategories.py new file mode 100644 index 0000000..bc4d1e4 --- /dev/null +++ b/pyblackbird_cc/resources/migrations/0017_rename_additional_resource_category_resource_subcategories.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.4 on 2024-08-01 14:29 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("resources", "0016_alter_resourcesubcategory_options_and_more"), + ] + + operations = [ + migrations.RenameField( + model_name="resource", + old_name="additional_resource_category", + new_name="subcategories", + ), + ] diff --git a/pyblackbird_cc/resources/migrations/0018_alter_resource_subcategories.py b/pyblackbird_cc/resources/migrations/0018_alter_resource_subcategories.py new file mode 100644 index 0000000..c981512 --- /dev/null +++ b/pyblackbird_cc/resources/migrations/0018_alter_resource_subcategories.py @@ -0,0 +1,25 @@ +# Generated by Django 5.0.4 on 2024-08-01 15:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ( + "resources", + "0017_rename_additional_resource_category_resource_subcategories", + ), + ] + + operations = [ + migrations.AlterField( + model_name="resource", + name="subcategories", + field=models.ManyToManyField( + blank=True, + related_name="additional_resource_category", + to="resources.resourcesubcategory", + ), + ), + ] diff --git a/pyblackbird_cc/resources/models.py b/pyblackbird_cc/resources/models.py index a03992b..05a848c 100644 --- a/pyblackbird_cc/resources/models.py +++ b/pyblackbird_cc/resources/models.py @@ -45,9 +45,8 @@ class Resource(models.Model): null=False, related_name="main_resource_category", ) - additional_resource_category = models.ManyToManyField( + subcategories = models.ManyToManyField( "ResourceSubcategory", - null=True, blank=True, related_name="additional_resource_category", ) diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py index 4381267..9d85bfa 100644 --- a/pyblackbird_cc/resources/views.py +++ b/pyblackbird_cc/resources/views.py @@ -100,8 +100,8 @@ def _extract_metadata_from_resource(resource_obj) -> ResourceInfo | None: for f in resource_obj.thumbnail_filenames ] try: - if resource_obj.additional_resource_category: - arc_name = resource_obj.additional_resource_category.name + if resource_obj.subcategories: + arc_name = resource_obj.subcategories.name else: arc_name = None return ResourceInfo( @@ -253,10 +253,11 @@ def create_resource(request): age_range = form.cleaned_data["age_range"] curriculum = form.cleaned_data["curriculum"] main_resource_category = form.cleaned_data["main_resource_category"] - additional_resource_category = form.cleaned_data["additional_resource_category"] + subcategories = form.cleaned_data["subcategories"] feature_slot = form.cleaned_data["feature_slot"] - additional_resource_category_objs = [ResourceSubcategory.objects.create(name=x) for x in additional_resource_category] + # We use get here because we know these categories exist + subcategories_objs = [ResourceSubcategory.objects.get(name=x) for x in subcategories] try: with transaction.atomic(): @@ -271,7 +272,7 @@ def create_resource(request): feature_slot=feature_slot, ) resource.save() - resource.additional_resource_category.set(additional_resource_category_objs) + resource.subcategories.set(subcategories_objs) metadata_generator = create_metadata(pdf_files) snapshotted_pages = [] @@ -346,8 +347,8 @@ def resource_detail(request, resource_id): "resource_type": resource_obj.resource_type.name, "main_resource_category": resource_obj.main_resource_category.name, "additional_resource_category": ( - resource_obj.additional_resource_category.name - if resource_obj.additional_resource_category + resource_obj.subcategories.name + if resource_obj.subcategories else None ), "age_range": resource_obj.age_range, |