sabrebooking / sabrebackend / sabre / sabreapi / admin.py
admin.py
Raw
from django.contrib import admin
from . models import *
from . resources import *
from import_export.admin import ImportExportModelAdmin
from django.http import HttpResponseRedirect
from django.urls import reverse, path
from django.utils.html import format_html
from . views import view_events_control

admin.site.site_header = "SABRE BOOKING SYSTEM"

# Register your models here.
class EquipmentAdmin(ImportExportModelAdmin):
    resource_classes = [EquipmentResource]

admin.site.register(Equipment, EquipmentAdmin)


class AmenityAdmin(ImportExportModelAdmin):
    resource_classes = [AmenityResource]


admin.site.register(Amenity, AmenityAdmin)

class CustomModelAdminMixin:
    """
    Mixin to add custom action buttons to the change view of Django admin.
    """

    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            self.get_custom_url(
                name='custom-action-1',
                url_name='admin:{}_custom_action_1'.format(self.model._meta.app_label),
                view=self.custom_action_1,
                description='Custom Action 1',
            ),
            self.get_custom_url(
                name='custom-action-2',
                url_name='admin:{}_custom_action_2'.format(self.model._meta.app_label),
                view=self.custom_action_2,
                description='Custom Action 2',
            ),
            # Add more custom URLs as needed
        ]
        return custom_urls + urls

    def get_custom_url(self, name, url_name, view, description):
        """
        Helper method to generate a custom URL.
        """
        return path(
            f'{name}/<path:pk>/{url_name}/',
            self.admin_site.admin_view(view),
            name=url_name.format(model=self.model._meta.model_name),
        )

    def custom_action_buttons(self, obj):
        """
        Method to display custom action buttons in the change view.
        """
        buttons = [
            ('Custom Action 1', 'admin:{}_custom_action_1'.format(self.model._meta.app_label)),
            ('Custom Action 2', 'admin:{}_custom_action_2'.format(self.model._meta.app_label)),
            # Add more buttons as needed
        ]
        return format_html(
            '&nbsp;'.join(
                [
                    '<a class="button" href="{}">{}</a>'.
                    format(
                        reverse(url_name, args=[obj.pk]),
                        description,
                    )
                    for description, url_name in buttons
                ]
            )
        )

    custom_action_buttons.short_description = 'Custom Action Buttons'
    custom_action_buttons.allow_tags = True

    def custom_action_1(self, request, obj_id):
        """
        Placeholder for the first custom action.
        """
        # Implement your custom action logic here
        pass

    def custom_action_2(self, request, obj_id):
        """
        Placeholder for the second custom action.
        """
        # Implement your custom action logic here
        pass


class BookingAdmin(ImportExportModelAdmin, admin.ModelAdmin, CustomModelAdminMixin):
    resource_classes = [BookingResource]
    change_list_template = 'admin/sabreapi/booking/change_list.html'

    def date(self,obj):
        return f"{Booking.humanize_date(obj.event_date)}"
    
    list_display = ["company_name", "date", "room", "display_buttons"]
    list_filter = [ "event_date", "room"]
    search_fields = ["company_name"]
    readonly_fields = ["user","event_duration","number_of_attendees","event_leader_name","room","date_created","catering","event_start","event_end","event_date","event_type","event_name","company_name"]
    actions = ["show_calendar"]

    # def show_calendar(self,obj,int):
    #     return HttpResponseRedirect("admin/sabreapi/availablemeal/")
        # return format_html(f'<a class="button" href=admin/sabreapi/availablemeal/>Show Calendar</a>')


    def display_buttons(self, obj):

        return format_html(f'<a class="button" href="/sabreapi/v1/events/{obj.id}">View ECS</a> &nbsp <a class="button" href="/sabreapi/v1/print/{obj.id}">Print ECS</a>')
    
    display_buttons.short_description = 'Action Buttons'
    display_buttons.allow_tags = True

    # def print_ECS_btn(self,obj):
    #     return format_html(f'<a class="button" href="/sabreapi/v1/events/{obj.id}">View ECS</a>')
    
    # view_ECS_btn.short_description = 'Action Buttons'
    # view_ECS_btn.allow_tags = True

admin.site.register(Booking, BookingAdmin)

class RoomAdmin(ImportExportModelAdmin):
    resource_classes = [RoomResource]


admin.site.register(Room, RoomAdmin)


class ImageAdmin(ImportExportModelAdmin):
    resource_classes = [ImageResource]


admin.site.register(Image, ImageAdmin)


class WaitlistAdmin(ImportExportModelAdmin):
    resource_classes = [AmenityResource]


admin.site.register(Waitlist, WaitlistAdmin)


class CustomUserAdmin(ImportExportModelAdmin):
    resource_classes = [CustomUserResource]


admin.site.register(CustomUser, CustomUserAdmin)


class RoomFunctionAdmin(ImportExportModelAdmin):
    resource_classes = [RoomFunctionResource]


admin.site.register(RoomFunction, RoomFunctionAdmin)


class CateringAdmin(ImportExportModelAdmin):
    resource_classes = [CateringResource]


admin.site.register(Catering, CateringAdmin)


class AvailableMealAdmin(ImportExportModelAdmin):
    resource_classes = [AvailableMealResource]
    list_display = ["title", "catering_package", "meal_time"]
    list_filter = ["catering_package", "meal_time"]

admin.site.register(AvailableMeal, AvailableMealAdmin)


class FeedbackAdmin(ImportExportModelAdmin):
    resource_classes = [FeedbackResource]


admin.site.register(Feedback, FeedbackAdmin)