Skip to content

Commit

Permalink
admin: i18n
Browse files Browse the repository at this point in the history
One reference to "PDF" is removed to make message useful if other
filetypes are supported in future.
  • Loading branch information
friedelwolff committed Aug 16, 2024
1 parent 3c4d3a0 commit 7f7ec5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 6 additions & 5 deletions app/general/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import magic
from django.contrib import admin
from django.forms import HiddenInput, ModelForm
from django.utils.translation import gettext as _
from simple_history.admin import SimpleHistoryAdmin

from general.service.extract_text import GetTextError, GetTextFromPDF
Expand Down Expand Up @@ -32,29 +33,29 @@ def clean(self):
if uploaded_file:
file_type = magic.from_buffer(uploaded_file.read(), mime=True)
if file_type != "application/pdf":
self.add_error("uploaded_file", "Only PDF files are allowed.")
self.add_error("uploaded_file", _("Only PDF files are allowed."))

try:
# Extract text from PDF file
cleaned_data["document_data"] = GetTextFromPDF(uploaded_file).to_text()

except GetTextError:
return self.add_error(
"uploaded_file", "The uploaded PDF file is corrupted or not fully downloaded."
"uploaded_file", _("The uploaded file is corrupted or not fully downloaded.")
)

cleaned_data["mime_type"] = file_type

uploaded_file.seek(0) # Reset file pointer after read

if not url and not uploaded_file:
self.add_error("url", "Either URL or uploaded file must be provided.")
self.add_error("uploaded_file", "Either URL or uploaded file must be provided.")
self.add_error("url", _("Either URL or uploaded file must be provided."))
self.add_error("uploaded_file", _("Either URL or uploaded file must be provided."))

if uploaded_file:
limit = 10 * 1024 * 1024
if uploaded_file.size and uploaded_file.size > limit:
self.add_error("uploaded_file", "File size must not exceed 10MB.")
self.add_error("uploaded_file", _("File size must not exceed 10MB."))

return cleaned_data

Expand Down
3 changes: 2 additions & 1 deletion app/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
{% endcomment %}

{% load static %}
{% load i18n %}

{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}

Expand All @@ -24,7 +25,7 @@
{% block branding %}
<h1 class="header-title">
<a href="{% url 'home' %}">
<img src="{% static 'img/sadilar.png' %}" class="main-logo" alt=_("SADiLaR")>
<img src="{% static 'img/sadilar.png' %}" class="main-logo" alt="{% trans 'SADiLaR' %}">
</a>
</h1>
{% if user.is_anonymous %}
Expand Down

0 comments on commit 7f7ec5e

Please sign in to comment.