Skip to content

Commit

Permalink
Added support for ResourceLink model and view.
Browse files Browse the repository at this point in the history
Closes #441
  • Loading branch information
tim-schilling committed Jan 7, 2025
1 parent 195edcd commit 9f3cacf
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 3 deletions.
24 changes: 23 additions & 1 deletion home/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.contrib import admin
from django.urls import reverse
from django.utils.html import escape
from django.utils.safestring import mark_safe

from .models import Event
from .models import Event, ResourceLink
from .models import Question
from .models import Session
from .models import SessionMembership
Expand All @@ -13,6 +16,25 @@ class EventAdmin(admin.ModelAdmin):
filter_horizontal = ("speakers", "rsvped_members", "organizers")


@admin.register(ResourceLink)
class ResourceLinkAdmin(admin.ModelAdmin):
list_display = (
"path",
"link",
"url",
"permanent",
"updated",
"created",
)
ordering = ("path",)
search_fields = ("path", "url")

@admin.display(description="Link", ordering="path")
def link(self, obj):
href = reverse("resource_link", kwargs={"path": obj.path})
return mark_safe(f'<a href="{href}">Copy to share</a>')


class SessionMembershipInline(admin.TabularInline):
model = SessionMembership
extra = 0
Expand Down
10 changes: 9 additions & 1 deletion home/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import factory

from accounts.factories import UserFactory
from home.models import Event
from home.models import Event, ResourceLink
from home.models import Question
from home.models import Session
from home.models import Survey
Expand All @@ -22,6 +22,14 @@ class Meta:
status = Event.SCHEDULED


class ResourceLinkFactory(factory.django.DjangoModelFactory):
class Meta:
model = ResourceLink

path = factory.Sequence(lambda n: "path/resource-%d" % n)
url = factory.Sequence(lambda n: "https://testserver/%d/resource" % n)


class SessionFactory(factory.django.DjangoModelFactory):
class Meta:
model = Session
Expand Down
43 changes: 43 additions & 0 deletions home/migrations/0030_resourcelink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.1.13 on 2025-01-07 13:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("home", "0029_alter_generalpage_content_listblock"),
]

operations = [
migrations.CreateModel(
name="ResourceLink",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("created", models.DateTimeField(auto_now_add=True)),
("updated", models.DateTimeField(auto_now=True)),
("permanent", models.BooleanField(default=False)),
(
"path",
models.TextField(
help_text="The relative path for requests to the djangonaut.space web app.",
unique=True,
),
),
(
"url",
models.URLField(
help_text="The final URL it directs to.", max_length=2000
),
),
],
),
]
1 change: 1 addition & 0 deletions home/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .blog import *
from .event import *
from .resource import *
from .session import *
from .survey import *
15 changes: 15 additions & 0 deletions home/models/resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.db import models


class ResourceLink(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
permanent = models.BooleanField(default=False)
path = models.TextField(
help_text="The relative path for requests to the djangonaut.space web app.",
unique=True,
)
url = models.URLField(help_text="The final URL it directs to.", max_length=2000)

def __str__(self):
return self.path
29 changes: 29 additions & 0 deletions home/tests/test_resource_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.test import TestCase
from django.urls import reverse

from home.factories import ResourceLinkFactory


class ResourceLinkViewTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.resource_link = ResourceLinkFactory.create()
cls.url = reverse("resource_link", kwargs={"path": cls.resource_link.path})

def test_get(self):
response = self.client.get(self.url)
self.assertRedirects(
response, self.resource_link.url, fetch_redirect_response=False
)

def test_get_not_found(self):
response = self.client.get(
reverse("resource_link", kwargs={"path": "not-found"})
)
self.assertEqual(response.status_code, 404)

def test_post(self):
response = self.client.post(self.url)
self.assertRedirects(
response, self.resource_link.url, fetch_redirect_response=False
)
2 changes: 2 additions & 0 deletions home/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path

from .views import CreateUserSurveyResponseFormView
from .views import resource_link
from .views import event_calendar
from .views import EventDetailView
from .views import EventListView
Expand Down Expand Up @@ -28,4 +29,5 @@
UserSurveyResponseView.as_view(),
name="user_survey_response",
),
path("resource/<path:path>", resource_link, name="resource_link"),
]
7 changes: 6 additions & 1 deletion home/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import UserPassesTestMixin
from django.db.models import Prefetch
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse_lazy
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormMixin
Expand All @@ -18,6 +18,7 @@
from .models import Survey
from .models import UserQuestionResponse
from .models import UserSurveyResponse
from .models import ResourceLink


def event_calendar(request):
Expand Down Expand Up @@ -178,3 +179,7 @@ def get_context_data(self, **kwargs):
kwargs["read_only"] = True
context_data = super().get_context_data(**kwargs)
return context_data


def resource_link(request, path):
return redirect(get_object_or_404(ResourceLink, path=path).url)

0 comments on commit 9f3cacf

Please sign in to comment.