2011-07-11 02:10:22 +01:00
|
|
|
from django.contrib import admin
|
2023-01-19 18:35:56 +00:00
|
|
|
from django.core import serializers
|
2011-07-11 02:10:22 +01:00
|
|
|
from django.http import HttpResponse
|
2020-05-28 04:54:53 +01:00
|
|
|
|
2023-09-10 13:42:36 +01:00
|
|
|
from troggle.core.models.caves import Cave, CaveAndEntrance, Entrance
|
2024-07-12 16:18:05 +01:00
|
|
|
from troggle.core.models.logbooks import QM, LogbookEntry, PersonLogEntry
|
2023-01-30 19:04:36 +00:00
|
|
|
from troggle.core.models.survex import (
|
|
|
|
DrawingFile,
|
|
|
|
SingleScan,
|
|
|
|
SurvexBlock,
|
|
|
|
SurvexFile,
|
|
|
|
SurvexPersonRole,
|
|
|
|
SurvexStation,
|
|
|
|
)
|
2023-01-29 16:23:58 +00:00
|
|
|
from troggle.core.models.wallets import Wallet
|
2023-01-30 19:04:36 +00:00
|
|
|
from troggle.core.models.troggle import DataIssue, Expedition, Person, PersonExpedition
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
"""This code significantly adds to the capabilities of the Django Management control panel for Troggle data.
|
2021-04-02 15:51:14 +01:00
|
|
|
In particular, it enables JSON export of any data with 'export_as_json'
|
|
|
|
and configures the search fields to be used within the control panel.
|
|
|
|
|
|
|
|
What is the search path for the css and js inclusions in the Media subclasses though ?!
|
2023-01-28 21:00:38 +00:00
|
|
|
|
|
|
|
The page looks for /static/jquery/jquery.min.js
|
2023-01-30 19:04:36 +00:00
|
|
|
"""
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
class TroggleModelAdmin(admin.ModelAdmin):
|
|
|
|
def save_model(self, request, obj, form, change):
|
2022-03-23 22:55:43 +00:00
|
|
|
"""overriding admin save to fill the new_since parsing_field
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2022-03-23 22:55:43 +00:00
|
|
|
new_since_parsing is not currently used in troggle. It is a fossil."""
|
2023-01-30 19:04:36 +00:00
|
|
|
obj.new_since_parsing = True
|
2018-04-15 16:28:13 +01:00
|
|
|
obj.save()
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class Media:
|
2023-01-30 19:04:36 +00:00
|
|
|
js = ("jquery/jquery.min.js", "js/QM_helper.js") # not currently available to troggle, see media/js/README
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class RoleInline(admin.TabularInline):
|
|
|
|
model = SurvexPersonRole
|
|
|
|
extra = 4
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class SurvexBlockAdmin(TroggleModelAdmin):
|
|
|
|
inlines = (RoleInline,)
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2023-03-17 20:01:52 +00:00
|
|
|
# class QMsFoundInline(admin.TabularInline):
|
|
|
|
# model = QM
|
|
|
|
# fk_name = "found_by"
|
|
|
|
# fields = ("number", "grade", "location_description", "comment") # need to add foreignkey to cave part
|
|
|
|
# extra = 1
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2023-01-30 16:18:19 +00:00
|
|
|
class PersonLogEntryInline(admin.TabularInline):
|
|
|
|
model = PersonLogEntry
|
2023-01-30 19:04:36 +00:00
|
|
|
raw_id_fields = ("personexpedition",)
|
2011-07-11 02:10:22 +01:00
|
|
|
extra = 1
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class LogbookEntryAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
prepopulated_fields = {"slug": ("title",)}
|
|
|
|
search_fields = ("title", "expedition__year")
|
|
|
|
date_heirarchy = "date"
|
2023-03-17 20:01:52 +00:00
|
|
|
# inlines = (PersonLogEntryInline, QMsFoundInline)
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class Media:
|
2023-01-30 19:04:36 +00:00
|
|
|
css = {"all": ("css/troggleadmin.css",)} # this does not exist
|
|
|
|
|
|
|
|
actions = ("export_logbook_entries_as_html", "export_logbook_entries_as_txt")
|
|
|
|
|
2019-03-30 17:02:07 +00:00
|
|
|
def export_logbook_entries_as_html(self, modeladmin, request, queryset):
|
2023-01-30 19:04:36 +00:00
|
|
|
response = downloadLogbook(request=request, queryset=queryset, extension="html") # fails, no queryset
|
2011-07-11 02:10:22 +01:00
|
|
|
return response
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2019-03-30 17:02:07 +00:00
|
|
|
def export_logbook_entries_as_txt(self, modeladmin, request, queryset):
|
2023-01-30 19:04:36 +00:00
|
|
|
response = downloadLogbook(request=request, queryset=queryset, extension="txt") # fails, no queryset
|
2011-07-11 02:10:22 +01:00
|
|
|
return response
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
|
|
|
|
class PersonExpeditionInline(admin.TabularInline):
|
|
|
|
model = PersonExpedition
|
|
|
|
extra = 1
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class PersonAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("first_name", "last_name")
|
2011-07-11 02:10:22 +01:00
|
|
|
inlines = (PersonExpeditionInline,)
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class QMAdmin(TroggleModelAdmin):
|
2023-03-17 20:01:52 +00:00
|
|
|
search_fields = ("number", "expoyear")
|
|
|
|
list_display = ("__str__", "grade")
|
2023-01-30 19:04:36 +00:00
|
|
|
list_display_links = ("__str__",)
|
2023-03-17 20:01:52 +00:00
|
|
|
# list_editable = ("comment", "page_ref", "grade")
|
|
|
|
# list_per_page = 20
|
|
|
|
# raw_id_fields = ("found_by", "ticked_off_by")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class PersonExpeditionAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("person__first_name", "expedition__year")
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class CaveAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("official_name", "kataster_number", "unofficial_number")
|
2011-07-11 02:10:22 +01:00
|
|
|
extra = 4
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
class EntranceAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("caveandentrance__cave__kataster_number",)
|
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
class SurvexStationAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("name",)
|
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
|
|
|
|
class SurvexFileAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("path",)
|
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
|
2023-09-06 20:58:14 +01:00
|
|
|
# class SurvexDirectoryAdmin(TroggleModelAdmin):
|
|
|
|
# search_fields = (
|
|
|
|
# "path",
|
|
|
|
# "survexdirectory",
|
|
|
|
# )
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
|
|
|
|
class DrawingFileAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("dwgname",)
|
|
|
|
|
2021-05-05 00:35:10 +01:00
|
|
|
|
|
|
|
class WalletAdmin(TroggleModelAdmin):
|
2023-01-30 19:04:36 +00:00
|
|
|
search_fields = ("fpath",)
|
2021-05-05 00:35:10 +01:00
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
admin.site.register(Cave, CaveAdmin)
|
2023-09-10 13:42:36 +01:00
|
|
|
#admin.site.register(Area)
|
2011-07-11 02:10:22 +01:00
|
|
|
admin.site.register(CaveAndEntrance)
|
|
|
|
admin.site.register(Entrance, EntranceAdmin)
|
2019-06-26 20:57:24 +01:00
|
|
|
admin.site.register(SurvexBlock, SurvexBlockAdmin)
|
2021-05-05 00:35:10 +01:00
|
|
|
admin.site.register(DrawingFile, DrawingFileAdmin)
|
2011-07-11 02:10:22 +01:00
|
|
|
admin.site.register(Expedition)
|
2023-01-30 19:04:36 +00:00
|
|
|
admin.site.register(Person, PersonAdmin)
|
2019-06-26 20:57:24 +01:00
|
|
|
admin.site.register(SurvexPersonRole)
|
2023-09-06 20:58:14 +01:00
|
|
|
#admin.site.register(SurvexDirectory, SurvexDirectoryAdmin)
|
2021-05-05 00:35:10 +01:00
|
|
|
admin.site.register(SurvexFile, SurvexFileAdmin)
|
|
|
|
admin.site.register(SurvexStation, SurvexStationAdmin)
|
2023-01-30 19:04:36 +00:00
|
|
|
admin.site.register(PersonExpedition, PersonExpeditionAdmin)
|
2011-07-11 02:10:22 +01:00
|
|
|
admin.site.register(LogbookEntry, LogbookEntryAdmin)
|
|
|
|
admin.site.register(QM, QMAdmin)
|
2021-05-05 00:35:10 +01:00
|
|
|
admin.site.register(Wallet, WalletAdmin)
|
2020-06-24 00:18:01 +01:00
|
|
|
admin.site.register(SingleScan)
|
2019-04-14 22:45:31 +01:00
|
|
|
admin.site.register(DataIssue)
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2023-01-30 19:04:36 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def export_as_json(modeladmin, request, queryset):
|
2019-03-30 17:02:07 +00:00
|
|
|
response = HttpResponse(content_type="text/json")
|
2023-01-30 19:04:36 +00:00
|
|
|
response["Content-Disposition"] = "attachment; filename=troggle_output.json"
|
2011-07-11 02:10:22 +01:00
|
|
|
serializers.serialize("json", queryset, stream=response)
|
|
|
|
return response
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2011-07-11 02:10:22 +01:00
|
|
|
def export_as_xml(modeladmin, request, queryset):
|
2019-03-30 17:02:07 +00:00
|
|
|
response = HttpResponse(content_type="text/xml")
|
2023-01-30 19:04:36 +00:00
|
|
|
response["Content-Disposition"] = "attachment; filename=troggle_output.xml"
|
2011-07-11 02:10:22 +01:00
|
|
|
serializers.serialize("xml", queryset, stream=response)
|
|
|
|
return response
|
|
|
|
|
2019-02-23 15:30:58 +00:00
|
|
|
|
2020-07-29 22:54:09 +01:00
|
|
|
admin.site.add_action(export_as_xml)
|
|
|
|
admin.site.add_action(export_as_json)
|