From: Jörn Menne Date: Thu, 13 Feb 2025 08:35:24 +0000 (+0100) Subject: Cleanup X-Git-Url: https://git.menne-pb.de/?a=commitdiff_plain;p=pinpoint.git Cleanup --- diff --git a/georeport/admin.py b/georeport/admin.py index 59de7b6..aae6975 100644 --- a/georeport/admin.py +++ b/georeport/admin.py @@ -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 index 0000000..7aa324e --- /dev/null +++ b/georeport/db_utils.py @@ -0,0 +1,53 @@ +# Copyright: (c) 2025, Jörn Menne +# 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() diff --git a/georeport/models.py b/georeport/models.py index 0e38a02..44ea3c1 100644 --- a/georeport/models.py +++ b/georeport/models.py @@ -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. diff --git a/georeport/templatetags/georeport_extras.py b/georeport/templatetags/georeport_extras.py index 19b8b0d..1697566 100644 --- a/georeport/templatetags/georeport_extras.py +++ b/georeport/templatetags/georeport_extras.py @@ -4,6 +4,9 @@ register = template.Library() def key(value, arg): + """ + Custom templatetag to access dictionaries in templates + """ return value[arg] diff --git a/georeport/urls.py b/georeport/urls.py index 0f4de3b..427ac57 100644 --- a/georeport/urls.py +++ b/georeport/urls.py @@ -1,5 +1,9 @@ +# Copyright: (c) 2025, Jörn Menne +# 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 ✅ diff --git a/georeport/views.py b/georeport/views.py index 3ccf2f3..6192e6c 100644 --- a/georeport/views.py +++ b/georeport/views.py @@ -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, )