aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/resources
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pyblackbird_cc/resources/forms.py38
-rw-r--r--pyblackbird_cc/resources/models.py26
-rw-r--r--pyblackbird_cc/resources/urls.py5
-rw-r--r--pyblackbird_cc/resources/views.py23
4 files changed, 67 insertions, 25 deletions
diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py
index f905811..da27fd4 100644
--- a/pyblackbird_cc/resources/forms.py
+++ b/pyblackbird_cc/resources/forms.py
@@ -5,8 +5,12 @@ from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django import forms
-from .models import ResourceCategory
-from .models import ResourceType
+from pyblackbird_cc.resources.models import Resource
+from pyblackbird_cc.resources.models import ResourceCategory
+from pyblackbird_cc.resources.models import ResourceType
+
+from .models import AGE_RANGE_CHOICES
+from .models import CURRICULUM_CHOICES
logger = logging.getLogger(__name__)
@@ -50,24 +54,12 @@ class ResourceCreateForm(forms.Form):
)
resource_type = forms.ModelChoiceField(queryset=ResourceType.objects.all())
age_range = forms.ChoiceField(
- choices=[
- ("3-5", "3-5"),
- ("5-7", "5-7"),
- ("7-11", "7-11"),
- ("11-14", "11-14"),
- ("14-16", "14-16"),
- ("16+", "16+"),
- ("Age not applicable", "Age not applicable"),
- ],
+ choices=AGE_RANGE_CHOICES,
help_text="Try to be accurate in your choice of age range so that your resource "
"shows up in the correct searches. (Although we don't have searches yet!)",
)
curriculum = forms.ChoiceField(
- choices=[
- ("No curriculum", "No curriculum"),
- ("English", "English"),
- ("Scottish", "Scottish"),
- ],
+ choices=CURRICULUM_CHOICES,
)
main_resource_category = forms.ModelChoiceField(
queryset=ResourceCategory.objects.all(),
@@ -137,3 +129,17 @@ class ResourceCreateForm(forms.Form):
if len(pdf_files) > ALLOWED_PDFS:
raise forms.ValidationError("Please select up to 20 PDF files.")
return pdf_files
+
+
+class ResourceUpdateMetadataForm(forms.ModelForm):
+ class Meta:
+ model = Resource
+ fields = [
+ "name",
+ "description",
+ "resource_type",
+ "age_range",
+ "curriculum",
+ "main_resource_category",
+ "additional_resource_category",
+ ]
diff --git a/pyblackbird_cc/resources/models.py b/pyblackbird_cc/resources/models.py
index 68f0637..b11bcb9 100644
--- a/pyblackbird_cc/resources/models.py
+++ b/pyblackbird_cc/resources/models.py
@@ -1,5 +1,21 @@
from django.db import models
+CURRICULUM_CHOICES = [
+ ("No curriculum", "No curriculum"),
+ ("English", "English"),
+ ("Scottish", "Scottish"),
+]
+
+AGE_RANGE_CHOICES = [
+ ("3-5", "3-5"),
+ ("5-7", "5-7"),
+ ("7-11", "7-11"),
+ ("11-14", "11-14"),
+ ("14-16", "14-16"),
+ ("16+", "16+"),
+ ("Age not applicable", "Age not applicable"),
+]
+
# Create your models here.
class Resource(models.Model):
@@ -28,15 +44,7 @@ class Resource(models.Model):
null=False,
default="5-7",
blank=False,
- choices=[
- ("3-5", "3-5"),
- ("5-7", "5-7"),
- ("7-11", "7-11"),
- ("11-14", "11-14"),
- ("14-16", "14-16"),
- ("16+", "16+"),
- ("Age not applicable", "Age not applicable"),
- ],
+ choices=AGE_RANGE_CHOICES,
)
curriculum = models.CharField(
max_length=20,
diff --git a/pyblackbird_cc/resources/urls.py b/pyblackbird_cc/resources/urls.py
index 30c5f99..86b3f4d 100644
--- a/pyblackbird_cc/resources/urls.py
+++ b/pyblackbird_cc/resources/urls.py
@@ -7,6 +7,11 @@ urlpatterns = [
path("", views.index, name="index"),
path("create/", views.create_resource, name="create_resource"),
path("resource/<int:resource_id>", views.resource_detail, name="resource_detail"),
+ path(
+ "resource/update-metadata/<int:pk>",
+ views.update_resource_metadata,
+ name="resource_update_metadata",
+ ),
]
htmx_patterns = [
diff --git a/pyblackbird_cc/resources/views.py b/pyblackbird_cc/resources/views.py
index c1d7672..e96e701 100644
--- a/pyblackbird_cc/resources/views.py
+++ b/pyblackbird_cc/resources/views.py
@@ -15,6 +15,7 @@ from django.shortcuts import render
from . import services
from .forms import ResourceCreateForm
+from .forms import ResourceUpdateMetadataForm
from .models import PDFPageSnapshot
from .models import PDFResource
from .models import Resource
@@ -357,3 +358,25 @@ def hx_download_button(request):
"resources/hx_download_button.html",
{"pdf_url": _extract_metadata_from_resource(res).pdf_url},
)
+
+
+@login_required
+def update_resource_metadata(request, pk): # Change resource_id to pk
+ resource = get_object_or_404(Resource, pk=pk)
+
+ if request.method == "POST":
+ form = ResourceUpdateMetadataForm(request.POST, instance=resource)
+ if form.is_valid():
+ form.save()
+ return redirect(
+ "resources:resource_detail",
+ resource_id=resource.pk,
+ ) # Use pk instead of resource_id
+ else:
+ form = ResourceUpdateMetadataForm(instance=resource)
+
+ return render(
+ request,
+ "resources/resource_metadata_update.html",
+ {"form": form, "resource": resource},
+ )