Skip to content

Commit

Permalink
add files app, add models, add view to serve files
Browse files Browse the repository at this point in the history
  • Loading branch information
jeriox committed Sep 21, 2024
1 parent fa2e7d9 commit 7780b3b
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ build
.env
.idea
.env.example

ENV
.venv
venv
5 changes: 5 additions & 0 deletions deployment/compose/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ server {
expires 1d;
add_header Cache-Control "public";
}

location /usercontent/ {
internal;
alias /var/ephios/data/private/media/;
}
}
Empty file.
5 changes: 5 additions & 0 deletions ephios/plugins/files/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin

from ephios.plugins.files.models import Document

admin.site.register(Document)
15 changes: 15 additions & 0 deletions ephios/plugins/files/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.utils.translation import gettext_lazy as _

from ephios.core.plugins import PluginConfig


class PluginApp(PluginConfig):
name = "ephios.plugins.files"

class EphiosPluginMeta:
name = _("Files")
author = "Ephios Team"
description = _("This plugins allows you to upload files and link to them in events.")

def ready(self):
from . import signals # pylint: disable=unused-import
26 changes: 26 additions & 0 deletions ephios/plugins/files/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.8 on 2024-09-21 12:01

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Document",
fields=[
(
"id",
models.BigAutoField(
auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
),
),
("title", models.CharField(max_length=255)),
("file", models.FileField(upload_to="documents/")),
],
),
]
Empty file.
9 changes: 9 additions & 0 deletions ephios/plugins/files/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models


class Document(models.Model):
title = models.CharField(max_length=255)
file = models.FileField(upload_to="documents/")

def __str__(self):
return self.title
Empty file added ephios/plugins/files/signals.py
Empty file.
8 changes: 8 additions & 0 deletions ephios/plugins/files/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from ephios.plugins.files.views import DocumentView

app_name = "files"
urlpatterns = [
path("documents/<int:pk>/", DocumentView.as_view(), name="document"),
]
20 changes: 20 additions & 0 deletions ephios/plugins/files/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.views import View
from guardian.mixins import LoginRequiredMixin

from ephios.plugins.files.models import Document


class DocumentView(View, LoginRequiredMixin):
def get(self, request, *args, **kwargs):
document = get_object_or_404(Document, id=kwargs["pk"])
response = HttpResponse()
response["Content-Disposition"] = (
"attachment; filename=" + os.path.split(document.file.name)[1]
)
response["X-Accel-Redirect"] = settings.MEDIA_URL + document.file.name
return response
2 changes: 2 additions & 0 deletions ephios/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"ephios.plugins.eventautoqualification.apps.PluginApp",
"ephios.plugins.simpleresource.apps.PluginApp",
"ephios.plugins.federation.apps.PluginApp",
"ephios.plugins.files.apps.PluginApp",
]
PLUGINS = copy.copy(CORE_PLUGINS)
for ep in metadata.entry_points(group="ephios.plugins"):
Expand Down Expand Up @@ -224,6 +225,7 @@
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = env.str("STATIC_URL", default="/static/")
MEDIA_URL = env.str("MEDIA_URL", default="/usercontent/")

STATICFILES_DIRS = (os.path.join(BASE_DIR, "ephios/static"),)
STATICFILES_FINDERS = (
Expand Down

0 comments on commit 7780b3b

Please sign in to comment.