from django import forms from django.conf.urls import url from django.contrib import admin, messages from django.core.exceptions import ValidationError from django.shortcuts import redirect from .models import * class LocationAdminForm(forms.ModelForm): def clean_happy_hour_end(self): hh_start = self.cleaned_data['happy_hour_start'] hh_end = self.cleaned_data['happy_hour_end'] if hh_start is None and hh_end is not None: raise ValidationError('Start und Ende Zeit muss angegeben werden') if hh_start is not None and hh_end is None: raise ValidationError('Start und Ende Zeit muss angegeben werden') if hh_start is not None and hh_end is not None and not (hh_end > hh_start): raise ValidationError('Ende-Zeit muss nach Start-Zeit liegen') return self.cleaned_data['happy_hour_end'] @admin.register(Location) class LocationAdmin(admin.ModelAdmin): form = LocationAdminForm list_display = ('code', 'name', 'city', 'happy_hour_start', 'happy_hour_end') fields = ['users', 'code', 'happy_hour_start', 'happy_hour_end', 'name', 'street', 'plz', 'city', 'phone', 'email', 'url', ] @admin.register(Client) class ClientAdmin(admin.ModelAdmin): list_display = ('uuid', 'location', 'report_user') fields = ['location', 'uuid', 'report_user'] @admin.register(LocationData) class LocationDataAdmin(admin.ModelAdmin): def get_urls(self): urls = super().get_urls() my_urls = [ url(r'^process_locationdata/$', self.admin_site.admin_view(self.process_locationdata), name='process_locationdata'), ] return my_urls + urls def process_locationdata(self, request): messages.success(request, 'Items processed.') return redirect('admin:billard_locationdata_changelist') list_display = ('client_id', 'desk_no', 'tst', 'on_off', 'processed', 'error_msg') fields = ['client_id', 'desk_no', 'tst', 'on_off', 'processed', 'error_msg'] class DeskAdminForm(forms.ModelForm): def clean_desk_no(self): id = self.instance.id client = self.cleaned_data['client'] desk_no = self.cleaned_data['desk_no'] t = Desk.objects.filter(desk_no=desk_no, client=client).exclude(id=id) if t.count() > 0: raise ValidationError('Tischnummer bereits vergeben.') return self.cleaned_data['desk_no'] @admin.register(Desk) class DeskAdmin(admin.ModelAdmin): form = DeskAdminForm list_display = ('client', 'desk_no', 'name', 'enabled', 'prize', 'prize_hh') @admin.register(Accounting) class AccountingAdmin(admin.ModelAdmin): list_display = ('desk', 'time_from', 'time_to', 'prize', 'billed', 'account_user', 'account_tst') list_filter = ('desk__client__location', 'account_user', 'account_tst', 'billed') def has_add_permission(self, request): if request.user.username == 'reinsle': return True return False