Fix image loading.

This commit is contained in:
jmsgrogan 2024-02-17 17:56:17 +00:00
parent f482cb562b
commit ff7c31701e
6 changed files with 59 additions and 5 deletions

1
test/requirements.txt Normal file
View file

@ -0,0 +1 @@
pytest

14
test/test_img_replace.py Normal file
View file

@ -0,0 +1,14 @@
import markdown
from bs4 import BeautifulSoup
md_input = """This is an image of a tree: ![tree image](tree_img)
"""
def test_image_replace():
soup = BeautifulSoup(markdown.markdown(md_input), features="html.parser")
for img in soup.find_all('img'):
img["src"] = "blah"
print(soup.prettify())

View file

@ -5,8 +5,12 @@ from . import views
urlpatterns = [ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
path('login', views.login_view, name="login"),
path('schedule', views.schedule, name="schedule"), path('schedule', views.schedule, name="schedule"),
path("thingstodo", views.things_to_do, name="thingstodo"), path("thingstodo", views.things_to_do, name="thingstodo"),
path("travel", views.travel, name="travel"), path("travel", views.travel, name="travel"),
path("accommodation", views.accommodation, name="accommodation"),
path("landing", views.landing, name="landing"),
path("faq", views.faq, name="faq"),
] ]

View file

@ -1,12 +1,15 @@
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.template import Template from django.template import Template
from django.template import Context from django.template import Context
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
import markdown import markdown
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from .models import Site, Page from .models import Site, Page, Image
_TEMPLATE = """ _TEMPLATE = """
<!DOCTYPE html> <!DOCTYPE html>
@ -42,6 +45,19 @@ def index(request):
soup = BeautifulSoup(template.render(context)) soup = BeautifulSoup(template.render(context))
return HttpResponse(soup.prettify()) return HttpResponse(soup.prettify())
def login_view(request):
token = request.POST["token"]
user = authenticate(request, username="guest", password=token)
if user is not None:
login(request, user)
return redirect("landing")
else:
return redirect("index")
@login_required(login_url="/")
def landing(request):
return get_page("Landing")
def get_page_header(site: Site): def get_page_header(site: Site):
pages = site.page_set.filter(navigable=True) pages = site.page_set.filter(navigable=True)
@ -66,13 +82,30 @@ def get_page(name:str):
content = template.render(context) content = template.render(context)
soup = BeautifulSoup(content, features="html.parser") soup = BeautifulSoup(content, features="html.parser")
for img in soup.find_all('img'):
img_name = img["src"]
img["src"] = Image.objects.get(name=img_name).content.url
return HttpResponse(soup.prettify()) return HttpResponse(soup.prettify())
@login_required(login_url="/")
def schedule(request): def schedule(request):
return get_page("Schedule") return get_page("Schedule")
@login_required(login_url="/")
def things_to_do(request): def things_to_do(request):
return get_page("ThingsToDo") return get_page("ThingsToDo")
@login_required(login_url="/")
def travel(request): def travel(request):
return get_page("Travel") return get_page("Travel")
@login_required(login_url="/")
def faq(request):
return get_page("Faq")
@login_required(login_url="/")
def accommodation(request):
return get_page("Accomodation")

View file

@ -44,7 +44,7 @@ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', #'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',

View file

@ -16,8 +16,10 @@ Including another URLconf
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path("", include('primary.urls')), path("", include('primary.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)