Skip to content

Commit

Permalink
Notion admin improvements (#2394)
Browse files Browse the repository at this point in the history
* Searching notion materials buy short ids

* Links to the notion and LMS
  • Loading branch information
f213 committed Aug 30, 2024
1 parent 9ba4527 commit fa700c3
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 238 deletions.
45 changes: 42 additions & 3 deletions src/apps/notion/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from django import forms
from django.db.models import QuerySet, Value
from django.db.models.functions import Replace
from django.http.request import HttpRequest
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from httpx import HTTPError

from apps.notion import helpers
Expand Down Expand Up @@ -35,20 +40,54 @@ def clean_title(self) -> str:
class NotionMaterialAdmin(ModelAdmin):
list_display = (
"title",
"course",
"page_id",
"our_page",
"notion_page",
)
fields = [
"title",
"course",
"page_id",
"is_home_page",
]
list_display_links = list_display
search_fields = [
"slug",
"page_id",
]
lookup_fields = [
"page_id",
"slug_without_dashes",
]

list_filter = ("course",)
form = NotionMaterialForm
save_as = True

def get_search_results(self, request: HttpRequest, queryset: QuerySet, search_term: str) -> tuple[QuerySet, bool]:
"""Allow searching both by long and short ids"""
if len(search_term) == 36:
search_term = helpers.uuid_to_id(search_term)

queryset.annotate(slug_without_dashes=Replace("slug", Value("-"), Value("")))
return super().get_search_results(request, queryset, search_term)

@admin.display(description=_("LMS"))
@mark_safe
def our_page(self, obj: Material) -> str:
slug = helpers.uuid_to_id(str(obj.slug))
lms_url = obj.get_absolute_url()

return f"""<a target="_blank" href="{ lms_url }">
<img class="notion-lms-logo" src="/static/logo/tds.png" />
{slug}</a>"""

@admin.display(description=_("Notion"))
@mark_safe
def notion_page(self, obj: Material) -> str:
notion_url = obj.get_notion_url()
return f"""<a target="_blank" href="{ notion_url }">
<img class="notion-logo" src="/static/logo/notion.svg" />
{obj.page_id}</a>"""


@admin.register(MaterialFile)
class MaterialFileAdmin(ModelAdmin): ...
19 changes: 19 additions & 0 deletions src/apps/notion/migrations/0009_notion_slug_field_translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.15 on 2024-08-30 17:26

import uuid

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("notion", "0008_notion_asset_metadata"),
]

operations = [
migrations.AlterField(
model_name="material",
name="slug",
field=models.UUIDField(db_index=True, default=uuid.uuid4, unique=True, verbose_name="Our page id"),
),
]
5 changes: 4 additions & 1 deletion src/apps/notion/models/material.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_by_page_id_or_slug(self, page_id_or_slug: str) -> Optional["Material"]:
class Material(TimestampedModel):
objects = MaterialManager()

slug = models.UUIDField(default=uuid.uuid4, db_index=True, unique=True)
slug = models.UUIDField(_("Our page id"), default=uuid.uuid4, db_index=True, unique=True)

title = models.CharField(_("Page title"), max_length=128, blank=True, help_text=_("Will be fetched automatically if empty"))
course = models.ForeignKey("products.Course", on_delete=models.CASCADE)
Expand All @@ -64,3 +64,6 @@ def __str__(self) -> str:
def get_absolute_url(self) -> str:
slug = uuid_to_id(str(self.slug))
return urljoin(settings.FRONTEND_URL, f"materials/{slug}/")

def get_notion_url(self) -> str:
return f"https://notion.so/1-{self.page_id}"
10 changes: 10 additions & 0 deletions src/core/static/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@
.app-number-input {
-moz-appearance: textfield;
}

.notion-logo {
max-width: 16px;
position: relative;
top: -2px;
}

.notion-lms-logo {
max-width: 16px;
}
4 changes: 4 additions & 0 deletions src/core/static/logo/notion.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/core/static/logo/tds.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fa700c3

Please sign in to comment.