Repository navigation
fix: improve collections models and api [FC-0059] #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ef09229
fa03042
9417609
a30e04a
87defd8
a4540ec
1c3fdec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,15 +22,15 @@ | |||||||||||
|
|
||||||||||||
| def create_collection( | ||||||||||||
| learning_package_id: int, | ||||||||||||
| name: str, | ||||||||||||
| title: str, | ||||||||||||
| description: str = "", | ||||||||||||
| ) -> Collection: | ||||||||||||
| """ | ||||||||||||
| Create a new Collection | ||||||||||||
| """ | ||||||||||||
| collection = Collection.objects.create( | ||||||||||||
| learning_package_id=learning_package_id, | ||||||||||||
| name=name, | ||||||||||||
| title=title, | ||||||||||||
| description=description, | ||||||||||||
| ) | ||||||||||||
|
Comment on lines
36
to
37
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Likewise we need to add the |
||||||||||||
| return collection | ||||||||||||
|
|
@@ -45,26 +45,26 @@ def get_collection(collection_id: int) -> Collection: | |||||||||||
|
|
||||||||||||
| def update_collection( | ||||||||||||
| collection_id: int, | ||||||||||||
| name: str | None = None, | ||||||||||||
| title: str | None = None, | ||||||||||||
| description: str | None = None, | ||||||||||||
| ) -> Collection: | ||||||||||||
| """ | ||||||||||||
| Update a Collection | ||||||||||||
| """ | ||||||||||||
| lp = Collection.objects.get(id=collection_id) | ||||||||||||
| collection = Collection.objects.get(id=collection_id) | ||||||||||||
|
|
||||||||||||
| # If no changes were requested, there's nothing to update, so just return | ||||||||||||
| # the Collection as-is | ||||||||||||
| if all(field is None for field in [name, description]): | ||||||||||||
| return lp | ||||||||||||
| if all(field is None for field in [title, description]): | ||||||||||||
| return collection | ||||||||||||
|
|
||||||||||||
| if name is not None: | ||||||||||||
| lp.name = name | ||||||||||||
| if title is not None: | ||||||||||||
| collection.title = title | ||||||||||||
| if description is not None: | ||||||||||||
| lp.description = description | ||||||||||||
| collection.description = description | ||||||||||||
|
|
||||||||||||
| lp.save() | ||||||||||||
| return lp | ||||||||||||
| collection.save() | ||||||||||||
| return collection | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def get_learning_package_collections(learning_package_id: int) -> QuerySet[Collection]: | ||||||||||||
|
|
@@ -73,8 +73,6 @@ def get_learning_package_collections(learning_package_id: int) -> QuerySet[Colle | |||||||||||
|
|
||||||||||||
| Only enabled collections are returned | ||||||||||||
| """ | ||||||||||||
| return ( | ||||||||||||
| Collection.objects | ||||||||||||
| .filter(learning_package_id=learning_package_id, enabled=True) | ||||||||||||
| .select_related("learning_package") | ||||||||||||
| ) | ||||||||||||
| return Collection.objects \ | ||||||||||||
| .filter(learning_package_id=learning_package_id, enabled=True) \ | ||||||||||||
| .select_related("learning_package") | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Generated by Django 4.2.14 on 2024-08-14 14:20 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.conf import settings | ||
| from django.db import migrations, models | ||
|
|
||
| import openedx_learning.lib.fields | ||
| import openedx_learning.lib.validators | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
| ('oel_collections', '0001_initial'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RemoveField( | ||
| model_name='collection', | ||
| name='name', | ||
| ), | ||
| migrations.AddField( | ||
| model_name='collection', | ||
| name='created_by', | ||
| field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='collection', | ||
| name='title', | ||
| field=openedx_learning.lib.fields.MultiCollationCharField(db_collations={'mysql': 'utf8mb4_unicode_ci', 'sqlite': 'NOCASE'}, default='Collection', help_text='The title of the collection.', max_length=500), | ||
| preserve_default=False, | ||
| ), | ||
| migrations.AlterField( | ||
| model_name='collection', | ||
| name='created', | ||
| field=models.DateTimeField(auto_now_add=True, validators=[openedx_learning.lib.validators.validate_utc_datetime]), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name='collection', | ||
| name='description', | ||
| field=openedx_learning.lib.fields.MultiCollationTextField(blank=True, db_collations={'mysql': 'utf8mb4_unicode_ci', 'sqlite': 'NOCASE'}, default='', help_text='Provides extra information for the user about this collection.', max_length=10000), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name='collection', | ||
| name='modified', | ||
| field=models.DateTimeField(auto_now=True, validators=[openedx_learning.lib.validators.validate_utc_datetime]), | ||
| ), | ||
| migrations.AddIndex( | ||
| model_name='collection', | ||
| index=models.Index(fields=['learning_package', 'title'], name='oel_collect_learnin_dfaf89_idx'), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,18 @@ | |
| """ | ||
| from __future__ import annotations | ||
|
|
||
| from django.conf import settings | ||
| from django.db import models | ||
| from django.utils.translation import gettext_lazy as _ | ||
|
|
||
| from ....lib.fields import case_insensitive_char_field | ||
| from ....lib.fields import MultiCollationTextField, case_insensitive_char_field | ||
| from ....lib.validators import validate_utc_datetime | ||
| from ..publishing.models import LearningPackage | ||
|
|
||
| __all__ = [ | ||
| "Collection", | ||
| ] | ||
|
|
||
|
|
||
| class Collection(models.Model): | ||
| """ | ||
|
|
@@ -20,22 +26,29 @@ class Collection(models.Model): | |
| # Each collection belongs to a learning package | ||
| learning_package = models.ForeignKey(LearningPackage, on_delete=models.CASCADE) | ||
|
|
||
| name = case_insensitive_char_field( | ||
| title = case_insensitive_char_field( | ||
| null=False, | ||
| max_length=255, | ||
| db_index=True, | ||
| blank=False, | ||
| max_length=500, | ||
| help_text=_( | ||
| "The name of the collection." | ||
| "The title of the collection." | ||
| ), | ||
| ) | ||
| description = case_insensitive_char_field( | ||
| null=False, | ||
|
|
||
| description = MultiCollationTextField( | ||
| blank=True, | ||
| null=False, | ||
| default="", | ||
| max_length=10_000, | ||
| help_text=_( | ||
| "Provides extra information for the user about this collection." | ||
| ), | ||
| db_collations={ | ||
| "sqlite": "NOCASE", | ||
| "mysql": "utf8mb4_unicode_ci", | ||
| } | ||
| ) | ||
|
|
||
| # We don't have api functions to handle the enabled field. This is a placeholder for future use and | ||
| # a way to "soft delete" collections. | ||
| enabled = models.BooleanField( | ||
|
|
@@ -44,11 +57,33 @@ class Collection(models.Model): | |
| "Whether the collection is enabled or not." | ||
| ), | ||
| ) | ||
| created = models.DateTimeField(auto_now_add=True) | ||
| modified = models.DateTimeField(auto_now=True) | ||
|
|
||
| created_by = models.ForeignKey( | ||
| settings.AUTH_USER_MODEL, | ||
| on_delete=models.SET_NULL, | ||
| null=True, | ||
| blank=True, | ||
| ) | ||
|
|
||
| created = models.DateTimeField( | ||
| auto_now_add=True, | ||
| validators=[ | ||
| validate_utc_datetime, | ||
| ], | ||
| ) | ||
|
|
||
| modified = models.DateTimeField( | ||
| auto_now=True, | ||
| validators=[ | ||
| validate_utc_datetime, | ||
| ], | ||
| ) | ||
|
|
||
| class Meta: | ||
| verbose_name_plural = "Collections" | ||
| indexes = [ | ||
| models.Index(fields=["learning_package", "title"]), | ||
| ] | ||
|
Comment on lines
+80
to
+86
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted to confirm, since the ForeignKey field defined is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are right @yusuf-musleh ! |
||
|
|
||
| def __repr__(self) -> str: | ||
| """ | ||
|
|
@@ -60,4 +95,4 @@ def __str__(self) -> str: | |
| """ | ||
| User-facing string representation of a Collection. | ||
| """ | ||
| return f"<{self.__class__.__name__}> ({self.id}:{self.name})" | ||
| return f"<{self.__class__.__name__}> ({self.id}:{self.title})" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized the
created_byvalue was missing from the params and in the.createbelow, would you mind adding them in this PR since I needed it when I was working on the REST endpoints.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch @yusuf-musleh!
Fixed here: a30e04a