]> git.menne-pb.de Git - pinpoint.git/commitdiff
Cleanup master
authorJörn Menne <jmenne@fedora.de>
Thu, 13 Feb 2025 08:35:24 +0000 (09:35 +0100)
committerJörn Menne <jmenne@fedora.de>
Thu, 13 Feb 2025 08:35:24 +0000 (09:35 +0100)
georeport/admin.py
georeport/db_utils.py [new file with mode: 0644]
georeport/models.py
georeport/templatetags/georeport_extras.py
georeport/urls.py
georeport/views.py

index 59de7b65c0e07b32f972aed764300ad5b2e9bdf4..aae69759cc5bfbf87a721ebdb17c8d4b6b733d57 100644 (file)
@@ -15,11 +15,8 @@ from django.utils.translation import ngettext
 
 from georeport.models import Category, Image, Report
 
-from django import forms
 from .minio import get_url
 
-import pdb
-from django.utils.safestring import mark_safe
 
 # TODO: reorder
 
@@ -36,7 +33,7 @@ class CategoryInline(admin.TabularInline):
 
 
 class CategoryUserInline(admin.TabularInline):
-    model = Category.users.through
+    model = Category.users.through  # type:ignore through is not known
     extra = 0
     can_delete = False
 
@@ -46,7 +43,7 @@ class CategoryUserInline(admin.TabularInline):
 
 
 class CategoryGroupInline(admin.TabularInline):
-    model = Category.groups.through
+    model = Category.groups.through  # type:ignore through is not known
     extra = 0
     can_delete = False
 
diff --git a/georeport/db_utils.py b/georeport/db_utils.py
new file mode 100644 (file)
index 0000000..7aa324e
--- /dev/null
@@ -0,0 +1,53 @@
+# Copyright: (c) 2025, Jörn Menne <jmenne@posteo.de>
+# GNU General Public License v3.0 (see LICSENE or https://www.gnu.org/license/gpl-3.0.md)
+import json
+from .models import Category
+import os
+
+# TODO: User and groups are currently not exported
+# This has to be added to work correctly.
+
+
+def write_categories_to_file(path="cat.json"):
+    """
+    Creates a json file, which contains all
+    necessary information to repopulate the database from scratch
+    with the categories.
+
+    Arguments:
+        path: The path to the json file
+    """
+    categories = Category.objects.all()  # type:ignore
+
+    data = []
+    for cat in categories:
+        catData = {}
+        catData["name"] = cat.name
+        catData["parent"] = cat.parent.name if cat.parent else ""
+
+        data.append(catData)
+
+    jsondata = json.dumps(data, indent=4)
+    with open(path, "w") as file:
+        file.write(jsondata)
+
+
+def populateCategories(path="cat.json"):
+    """
+    populates a database with categories provided by a json-file.
+    It works best, if the json was created by the function write_categories_to_file.
+
+    Arguments:
+        path: The path to the json-file
+    """
+
+    if not os.path.exists(path):
+        print("File not found")
+        return
+    with open(path, "r") as file:
+        data = json.load(file)
+        for x in data:
+            cat = Category(name=x["name"])
+            if x["parent"] != "":
+                cat.parent = Category.objects.filter(name=x["parent"]).first()  # type: ignore Attribute object is not known
+            cat.save()
index 0e38a025dc277729848435fc0746c94a09623a51..44ea3c1fee0e4c091ec864c75c3812db9b0090d9 100644 (file)
@@ -9,7 +9,6 @@ from django.db import models
 from django.contrib.auth.models import Group, User
 from typing import override
 
-from django.forms import DecimalField
 
 # Create your models here.
 
index 19b8b0d2865fbeb34fbf0db3085cd766f4f95db5..16975660546568d929e48492f0cdfb797ede46e3 100644 (file)
@@ -4,6 +4,9 @@ register = template.Library()
 
 
 def key(value, arg):
+    """
+    Custom templatetag to access dictionaries in templates
+    """
     return value[arg]
 
 
index 0f4de3bf66d468904672b7901b0d5397e196f8eb..427ac57a668c0ed1bac0d00fe9ce0e7271c95c3b 100644 (file)
@@ -1,5 +1,9 @@
+# Copyright: (c) 2025, Jörn Menne <jmenne@posteo.de>
+# GNU General Public License v3.0 (see LICSENE or https://www.gnu.org/license/gpl-3.0.md)
+
 from . import views
 from django.urls import path
+
 # TODO: Adjust to open311
 #  /services: -> List with Categories  <- GET ✅
 #  /sercvice/{id} -> single Category <- GET ✅
index 3ccf2f3df74de19a1c4c39ad21a23b10264048c8..6192e6c109438070854734e3805442b8b82a0b3a 100644 (file)
@@ -7,8 +7,6 @@ Each view is associated with a url in urls.py.
 A view takes a request and creates a respond for the request.
 """
 
-import os
-import shutil
 from base64 import urlsafe_b64decode
 
 from Crypto.Cipher import ChaCha20
@@ -18,12 +16,9 @@ from django.core.mail import send_mail
 from django.http import HttpResponse, JsonResponse
 from django.shortcuts import get_object_or_404, redirect, render
 from django.views.decorators.http import require_GET, require_http_methods, require_safe
-from minio import Minio
-
-from pinpoint_report.settings import DEFAULT_FROM_EMAIL
 
 from .admin import send_update
-from .forms import ImageForm, ReportForm
+from .forms import ReportForm
 from .models import Category, Report
 
 from .minio import handle_file_uploads, get_url
@@ -201,7 +196,7 @@ def send_creation_confirmation(report_dict):
         subject=subject,
         message=message,
         recipient_list=recipient_list,
-        from_email=DEFAULT_FROM_EMAIL,
+        from_email=settings.DEFAULT_FROM_EMAIL,
         fail_silently=True,
     )
 
@@ -233,6 +228,6 @@ def send_creation_mail(report_dict):
         subject=subject,
         message=message,
         recipient_list=recipient_list,
-        from_email=DEFAULT_FROM_EMAIL,
+        from_email=settings.DEFAULT_FROM_EMAIL,
         fail_silently=True,
     )