<reponame>fabricioifc/ifcarros-api<filename>api/admin.py<gh_stars>0
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.urls import reverse

from .models import *

class AutorizationInline(admin.StackedInline):
    readonly_fields = ['user']
    model = Autorization
    can_delete = False

@admin.register(Autorization)
class AutorizationAdmin(admin.ModelAdmin):
    readonly_fields = ('user', )
    list_display = ['__str__', 'get_user_sol', 'get_dthr_sol', 'dthrautorizacao', 'get_user_aut', 'status']
    ordering = ['-dthrautorizacao']

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(AutorizationAdmin, self).save_model(request, obj, form, change)

    def status(self):
        return u'%s' % self.solicitation.status   
    def get_user_aut(self, obj):
        return obj.user.name
    def get_user_sol(self, obj):
        return obj.solicitation.user.name
    def get_dthr_sol(self, obj):
        return obj.solicitation.dthrrequisicao
    get_user_sol.short_description = 'Solicitante'
    # get_user_aut.short_description = 'Autorizador'
    get_dthr_sol.short_description = 'Data Solicitação'

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(AutorizationAdmin, self).save_model(request, obj, form, change)


@admin.register(Passenger)
class PassengerAdmin(admin.ModelAdmin):
    pass

@admin.register(Solicitation)
class SolicitationAdmin(admin.ModelAdmin):
    readonly_fields = ['user']
    list_display = ['__str__', 'get_user_sol', 'dthrrequisicao', 'get_user_aut', 'get_dhtr_aut', 'get_status']
    ordering = ['-dthrrequisicao']
    list_filter = ('dthrrequisicao', 'autorization__status', 'autorization__user__name', )
    # actions = ['efetuar_autorizacao']
    
    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(SolicitationAdmin, self).save_model(request, obj, form, change)

    def get_user_aut(self, obj):
        return obj.autorization.user.name if obj.autorization.user != None else None
    def get_dhtr_aut(self, obj):
        return obj.autorization.dthrautorizacao
    def get_status_aut(self, obj):
        return obj.autorization.STATUS[obj.autorization.status]
    def get_user_sol(self, obj):
        return obj.user.name
    # def my_link(self, obj):
    #     # info = (obj._meta.app_label, obj._meta.model_name)
    #     info = (obj._meta.app_label, 'autorization')
    #     admin_url = reverse('admin:%s_%s_changelist' % info)
    #     return admin_url
    def get_status(self, obj):
        return obj.autorization.status

    get_user_sol.short_description = 'Solicitado por'
    get_user_aut.short_description = 'Autorizado por'
    get_dhtr_aut.short_description = 'Autorizador em'
    get_status_aut.short_description = 'Status'
    inlines = (AutorizationInline, )

@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
    readonly_fields = ['user']
    
    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(CarAdmin, self).save_model(request, obj, form, change)
    
    # fieldsets = (
    #     (None, {'fields': ('marca', 'modelo', 'km', 'ano', 'descricao', 'imagem')}),
    # )

    # def get_form(self, request, obj=None, **kwargs):
    #     form = super(CarAdmin, self).get_form(request, obj, **kwargs)
    #     form.base_fields['user'].initial = request.user
    #     return form

    # def formfield_for_foreignkey(self, db_field, request, **kwargs):
    #     if db_field.name == 'user':
    #         kwargs['queryset'] = User.objects.filter(id=request.user.id)
    #     return super().formfield_for_foreignkey(db_field, request, **kwargs)

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    exclude = ('first_name', 'last_name', )
    fieldsets = (
        (None, {'fields': ('email', 'username', 'password')}),
        (_('Personal info'), {'fields': ('name', 'siape', 'funcao', 'cpf')}),
        (_('Permissions'), {'fields': (
            'is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
        # (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'username', '<PASSWORD>', '<PASSWORD>'),
        }),
    )
    list_display = ('email', 'name', 'is_active', 'is_staff', 'is_superuser')
    # list_display = ('email', 'name', 'is_active', 'is_superuser', 'is_servidor', 'is_diretor', 'is_gestor')
    search_fields = ('email', 'name')
    ordering = ('email',)
    inlines = (UserProfileInline, )
    list_filter = ('is_active', 'is_staff', 'is_superuser')

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.urls import reverse

from .models import *

class AutorizationInline(admin.StackedInline):
    readonly_fields = ['user']
    model = Autorization
    can_delete = False

@admin.register(Autorization)
class AutorizationAdmin(admin.ModelAdmin):
    readonly_fields = ('user', )
    list_display = ['__str__', 'get_user_sol', 'get_dthr_sol', 'dthrautorizacao', 'get_user_aut', 'status']
    ordering = ['-dthrautorizacao']

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(AutorizationAdmin, self).save_model(request, obj, form, change)

    def status(self):
        return u'%s' % self.solicitation.status   
    def get_user_aut(self, obj):
        return obj.user.name
    def get_user_sol(self, obj):
        return obj.solicitation.user.name
    def get_dthr_sol(self, obj):
        return obj.solicitation.dthrrequisicao
    get_user_sol.short_description = 'Solicitante'
    # get_user_aut.short_description = 'Autorizador'
    get_dthr_sol.short_description = 'Data Solicitação'

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(AutorizationAdmin, self).save_model(request, obj, form, change)


@admin.register(Passenger)
class PassengerAdmin(admin.ModelAdmin):
    pass

@admin.register(Solicitation)
class SolicitationAdmin(admin.ModelAdmin):
    readonly_fields = ['user']
    list_display = ['__str__', 'get_user_sol', 'dthrrequisicao', 'get_user_aut', 'get_dhtr_aut', 'get_status']
    ordering = ['-dthrrequisicao']
    list_filter = ('dthrrequisicao', 'autorization__status', 'autorization__user__name', )
    # actions = ['efetuar_autorizacao']
    
    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(SolicitationAdmin, self).save_model(request, obj, form, change)

    def get_user_aut(self, obj):
        return obj.autorization.user.name if obj.autorization.user != None else None
    def get_dhtr_aut(self, obj):
        return obj.autorization.dthrautorizacao
    def get_status_aut(self, obj):
        return obj.autorization.STATUS[obj.autorization.status]
    def get_user_sol(self, obj):
        return obj.user.name
    # def my_link(self, obj):
    #     # info = (obj._meta.app_label, obj._meta.model_name)
    #     info = (obj._meta.app_label, 'autorization')
    #     admin_url = reverse('admin:%s_%s_changelist' % info)
    #     return admin_url
    def get_status(self, obj):
        return obj.autorization.status

    get_user_sol.short_description = 'Solicitado por'
    get_user_aut.short_description = 'Autorizado por'
    get_dhtr_aut.short_description = 'Autorizador em'
    get_status_aut.short_description = 'Status'
    inlines = (AutorizationInline, )

@admin.register(Car)
class CarAdmin(admin.ModelAdmin):
    readonly_fields = ['user']
    
    def save_model(self, request, obj, form, change):
        obj.user = request.user
        super(CarAdmin, self).save_model(request, obj, form, change)
    
    # fieldsets = (
    #     (None, {'fields': ('marca', 'modelo', 'km', 'ano', 'descricao', 'imagem')}),
    # )

    # def get_form(self, request, obj=None, **kwargs):
    #     form = super(CarAdmin, self).get_form(request, obj, **kwargs)
    #     form.base_fields['user'].initial = request.user
    #     return form

    # def formfield_for_foreignkey(self, db_field, request, **kwargs):
    #     if db_field.name == 'user':
    #         kwargs['queryset'] = User.objects.filter(id=request.user.id)
    #     return super().formfield_for_foreignkey(db_field, request, **kwargs)

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False


@admin.register(User)
class UserAdmin(BaseUserAdmin):
    exclude = ('first_name', 'last_name', )
    fieldsets = (
        (None, {'fields': ('email', 'username', 'password')}),
        (_('Personal info'), {'fields': ('name', 'siape', 'funcao', 'cpf')}),
        (_('Permissions'), {'fields': (
            'is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
        # (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'username', '<PASSWORD>', '<PASSWORD>'),
        }),
    )
    list_display = ('email', 'name', 'is_active', 'is_staff', 'is_superuser')
    # list_display = ('email', 'name', 'is_active', 'is_superuser', 'is_servidor', 'is_diretor', 'is_gestor')
    search_fields = ('email', 'name')
    ordering = ('email',)
    inlines = (UserProfileInline, )
    list_filter = ('is_active', 'is_staff', 'is_superuser')
# get_user_aut.short_description = 'Autorizador' # actions = ['efetuar_autorizacao'] # def my_link(self, obj): #     # info = (obj._meta.app_label, obj._meta.model_name) #     info = (obj._meta.app_label, 'autorization') #     admin_url = reverse('admin:%s_%s_changelist' % info) #     return admin_url # fieldsets = ( #     (None, {'fields': ('marca', 'modelo', 'km', 'ano', 'descricao', 'imagem')}), # ) # def get_form(self, request, obj=None, **kwargs): #     form = super(CarAdmin, self).get_form(request, obj, **kwargs) #     form.base_fields['user'].initial = request.user #     return form # def formfield_for_foreignkey(self, db_field, request, **kwargs): #     if db_field.name == 'user': #         kwargs['queryset'] = User.objects.filter(id=request.user.id) #     return super().formfield_for_foreignkey(db_field, request, **kwargs) # (_('Important dates'), {'fields': ('last_login', 'date_joined')}), # list_display = ('email', 'name', 'is_active', 'is_superuser', 'is_servidor', 'is_diretor', 'is_gestor')