Compare commits

...

6 Commits

13 changed files with 305 additions and 63 deletions

View File

@ -9,6 +9,7 @@ https://docs.djangoproject.com/en/5.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""
import os
from pathlib import Path
@ -23,70 +24,71 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
SECRET_KEY = os.getenv("DJANGO_SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv('DEBUG')
DEBUG = os.getenv("DEBUG")
ALLOWED_HOSTS = eval(os.getenv('ALLOWED_HOSTS'))
ALLOWED_HOSTS = eval(os.getenv("ALLOWED_HOSTS"))
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'core.apps.CoreConfig',
'articles.apps.ArticlesConfig',
'markdownx',
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.sites",
"django.contrib.flatpages",
"core.apps.CoreConfig",
"articles.apps.ArticlesConfig",
"links.apps.LinksConfig",
"markdownx",
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = 'config.urls'
ROOT_URLCONF = "config.urls"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
WSGI_APPLICATION = "config.wsgi.application"
# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': os.getenv('DB_PORT'),
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.getenv("DB_NAME"),
"USER": os.getenv("DB_USER"),
"PASSWORD": os.getenv("DB_PASSWORD"),
"HOST": os.getenv("DB_HOST"),
"PORT": os.getenv("DB_PORT"),
}
}
@ -95,25 +97,25 @@ DATABASES = {
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/
LANGUAGE_CODE = 'en-gb'
LANGUAGE_CODE = "en-gb"
TIME_ZONE = 'UTC'
TIME_ZONE = "UTC"
USE_I18N = True
@ -122,10 +124,10 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static/'
MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media/'
STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "static/"
MEDIA_URL = "media/"
MEDIA_ROOT = BASE_DIR / "media/"
# Site id to allow flatpages to function correctly
SITE_ID = 1
@ -145,18 +147,18 @@ SITE_ID = 1
# Tagulous config
SERIALIZATION_MODULES = {
'xml': 'tagulous.serializers.xml_serializer',
'json': 'tagulous.serializers.json',
'python': 'tagulous.serializers.python',
'yaml': 'tagulous.serializers.pyyaml',
"xml": "tagulous.serializers.xml_serializer",
"json": "tagulous.serializers.json",
"python": "tagulous.serializers.python",
"yaml": "tagulous.serializers.pyyaml",
}
# Markdownx config
MARKDOWNX_MARKDOWN_EXTENSIONS = [
'markdown.extensions.extra',
"markdown.extensions.extra",
]
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

View File

@ -14,12 +14,14 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('core.urls', namespace='core')),
path('articles/', include('articles.urls', namespace='articles')),
path('markdownx/', include('markdownx.urls')),
path("admin/", admin.site.urls),
path("", include("core.urls", namespace="core")),
path("articles/", include("articles.urls", namespace="articles")),
path("websites/", include("links.urls", namespace="links")),
path("markdownx/", include("markdownx.urls")),
]

View File

@ -22,11 +22,11 @@
{# href="{% url 'showcase:project_list' %}">Photography</a>#}
{# <a class="{% if 'bear' in request.path %}text-cyan-400 uppercase{% endif %} hover:text-red-400"#}
{# href="{% url 'bear:latest' %}">A Bear Aware</a>#}
{# <a class="{% if 'websites' in request.path %}text-cyan-400 uppercase{% endif %} hover:text-red-400"#}
{# href="{% url 'links:list' %}">Websites</a>#}
<a class="{% if 'websites' in request.path %}text-cyan-400 uppercase{% endif %} hover:text-red-400"
href="{% url 'links:list' %}">Websites</a>
{# <a class="{% if 'categories' in request.path %}text-cyan-400 uppercase{% endif %} hover:text-red-400" href="{% url 'articles:categories' %}">Categories</a>#}
{# <a class="{% if 'showcase' in request.path %}text-cyan-400 uppercase{% endif %} hover:text-red-400" href="#">Showcase</a>#}
{# <a class="{% if 'locations' in request.path %}text-cyan-400 uppercase{% endif %} hover:text-red-400" href="#">Locations</a>#}
</nav>
</div>
</header>
</header>

0
links/__init__.py Normal file
View File

16
links/admin.py Normal file
View File

@ -0,0 +1,16 @@
from django.contrib import admin
from .models import Category, Link
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ["title", "slug", "parent"]
prepopulated_fields = {"slug": ("title",)}
@admin.register(Link)
class LinkAdmin(admin.ModelAdmin):
list_display = ["title", "website", "category", "is_published", "followed"]
list_filter = ["category", "is_published"]
ordering = ["title"]

6
links/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class LinksConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "links"

View File

@ -0,0 +1,75 @@
# Generated by Django 5.1.6 on 2025-10-31 23:22
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Category",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=40, unique=True)),
("slug", models.SlugField(max_length=40, unique=True)),
("introduction", models.TextField(blank=True, null=True)),
(
"parent",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="children",
to="links.category",
),
),
],
options={
"ordering": ["title"],
},
),
migrations.CreateModel(
name="Link",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=200, unique=True)),
("website", models.URLField(unique=True)),
("introduction", models.TextField(blank=True, null=True)),
("followed", models.PositiveIntegerField(default=0)),
("created", models.DateTimeField(auto_now_add=True)),
("is_published", models.BooleanField(default=False)),
(
"category",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
related_name="links",
to="links.category",
),
),
],
options={
"ordering": ["category", "title"],
},
),
]

View File

46
links/models.py Normal file
View File

@ -0,0 +1,46 @@
from django.db import models
from django.urls import reverse_lazy
from django.utils.text import slugify
class Category(models.Model):
title = models.CharField(max_length=40, unique=True)
slug = models.SlugField(max_length=40, unique=True)
introduction = models.TextField(blank=True, null=True)
parent = models.ForeignKey(
"self", blank=True, null=True, on_delete=models.CASCADE, related_name="children"
)
def save(
self, force_insert=False, force_update=False, using=None, update_fields=None
):
if not self.slug:
self.slug = slugify(self.title)
return super(Category, self).save()
def get_absolute_url(self):
return reverse_lazy("links:list_category", args=["self.slug"])
def __str__(self):
return self.title
class Meta:
ordering = ["title"]
class Link(models.Model):
title = models.CharField(max_length=200, unique=True)
website = models.URLField(unique=True)
introduction = models.TextField(blank=True, null=True)
category = models.ForeignKey(
Category, on_delete=models.PROTECT, related_name="links"
)
followed = models.PositiveIntegerField(default=0)
created = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=False)
def __str__(self):
return self.title
class Meta:
ordering = ["category", "title"]

View File

@ -0,0 +1,48 @@
{% extends 'base.html' %}
{% block content %}
<main>
{% if object_list %}
{# <div class="text-center">#}
{# <p class="text-lg font-semibold">Filter by category group:</p>#}
{# <a class="font-mono" href="{% url 'links:list' %}">All</a>#}
{# {% for cat in category_groups %}#}
{# &bull;#}
{# <a class="font-mono" href="{% url 'links:list_category' category=cat %}">{{ cat.title }}</a>#}
{# {% endfor %}#}
{# </div>#}
{# <hr class="mx-8">#}
<div class="grid gap-12 grid-cols-1 md:grid-cols-2 lg:grid-cols-3 m-8">
{% for cat in object_list %}
<div class="pb-8 break-inside-avoid-column">
<h5 class="text-3xl font-semibold">{{ cat.title }}</h5>
<hr class="my-2">
{% for subcat in cat.children.all %}
<div>
<h6 class="text-2xl italic">{{ subcat.title }}</h6>
<hr class="my-2">
{% for link in subcat.links.all %}
<div class="mb-4">
<a class="text-xl font-semibold hover:text-red-400" href="{% url 'links:open_link' title=link.title %}" target="_blank">{{ link.title }}</a>
{% if link.introduction %}
<p class="text-justify">{{ link.introduction|linebreaksbr }}</p>
{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
</div>
{% endfor %}
</div>
{% else %}
<div class="text-center m-8">
<h3 class="text-4xl mb-4">Oh No!</h3>
<h5 class="text-lg">I haven't added any links yet.</h5>
<p class="text-neutral-400">(sorry not sorry)</p>
</div>
{% endif %}
{% if page_obj.has_previous or page_obj.has_next %}
{% include 'pagination.html' %}
{% endif %}
</main>
{% endblock %}

3
links/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

11
links/urls.py Normal file
View File

@ -0,0 +1,11 @@
from django.urls import path
from . import views
app_name = "links"
urlpatterns = [
path("", views.LinkList.as_view(), name="list"),
path("categories/<slug:category>/", views.LinkList.as_view(), name="list_category"),
path("launch/<str:title>", views.LinkRedirectView.as_view(), name="open_link"),
]

33
links/views.py Normal file
View File

@ -0,0 +1,33 @@
from django.db.models import Prefetch
from django.shortcuts import get_object_or_404
from django.views.generic import ListView, RedirectView
from .models import Category, Link
class CategoryList(ListView):
model = Category
class LinkList(ListView):
template_name = "links/link_list.html"
paginate_by = 6
def get_queryset(self):
category_group = self.kwargs.get("category", None)
if category_group:
queryset = Category.objects.filter(slug=category_group)
else:
queryset = Category.objects.filter(parent=None)
return queryset.prefetch_related(
Prefetch("links", queryset=Link.objects.filter(is_published=True))
)
class LinkRedirectView(RedirectView):
def get_redirect_url(self, *args, **kwargs):
url = get_object_or_404(Link, title=kwargs["title"])
url.followed += 1
url.save()
return url.website