Django Admin Настройте функцию сохранения для администратора, чтобы он отличался от обычного штатного пользователя. - PullRequest
0 голосов
/ 26 сентября 2018

Я хочу запустить настраиваемую функцию сохранения для администратора, которая должна отличаться от обычной.Причина в том, что я скрыл поле от своих моделей (parking_off).Я делаю приложение для парковки, и менеджеры хотят, чтобы поле было скрыто для сотрудников, поэтому они каким-то образом подправили весь мой код.

Как я могу заставить эту функцию сохранения работать только для администраторов?Поле parking_off доступно только для администраторов.

  def save(self):
        list = []
        d = self.parking_on
        while d <= self.parking_off:
            list.append(
                Parking(user=self.user,
                        email=self.email,
                        parking_on=d,
                        parking_off=d,
                        location=self.location
                        )
            )
            d = d + timedelta(days=1)
        Parking.objects.bulk_create(list)

Ниже приведены мои модели:

from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from datetime import datetime, timedelta, time
from django.core.exceptions import NON_FIELD_ERRORS

today = datetime.now().date()
tomorrow = today + timedelta(1)
now = datetime.now()
l = now.hour
m = int(now.strftime("%H"))


class ParkingManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(ParkingManager, self).filter(draft=False).filter(parking_on__lte=datetime.now())


class Parking(models.Model):
    PARKING_PLOT = (
        ('P1', 'Parking #1'),('P2', 'Parking #2'), ('P3', 'Parking #3'), 
        ('P4', 'Parking #4'),('P5', 'Parking #5'), ('P6', 'Parking #5'), 
        ('P7', 'Parking #7'),('P8', 'Parking #8'), ('P9', 'Parking #9'), 
        ('P10', 'Parking #10'),('P11', 'Parking #11'), ('P12', 'Parking #12'), 
        ('P13', 'Parking #13'),('P14', 'Parking #14'), ('P15', 'Parking #15')

    )
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             blank=True, null=True, default=1, on_delete=True)
    email = models.EmailField(blank=True, null=True)
    parking_on = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,
                                  help_text='Alege data cand doresti sa vii in office',)
    parking_off = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,
                                   help_text='Alege Data Plecarii')
    numar_masina = models.CharField(max_length=8, default="IF77WXV", blank=True, null=True,
                                    help_text='Introdu Numarul Masinii')
    location = models.CharField(max_length=3, blank=True, default="P1", null=True, choices=PARKING_PLOT,
                                help_text='Alege Locul de Parcare Dorit')
    updated = models.DateTimeField(
        auto_now=True, auto_now_add=False, blank=True, null=True)
    timestamp = models.DateTimeField(
        auto_now=False, auto_now_add=True, blank=True, null=True)
    # coming_hour = models.TimeField(default=time(
    #     9, 00), auto_now=False, auto_now_add=False, help_text='Alege Ora Venirii')
    # departure_hour = models.TimeField(default=time(
    #     18, 00), auto_now=False, auto_now_add=False, help_text='Alege Ora Plecarii')
    objects = ParkingManager()

    def __str__(self):
        return self.location + " | " + str(self.parking_on) + " | " + str(self.parking_off)

    class Meta:
        verbose_name_plural = "parking"
        ordering = ["-parking_on"]
        unique_together = ("parking_on", "location")

    def clean(self):
        if self.parking_on == today:  # merge--vedem dak parcam azi
            raise ValidationError(
                {'parking_on': _('Please book for a date starting tomorrow')})
        if self.parking_off < self.parking_on:  # merge-vedem daca bookam in trecut
            raise ValidationError(
                {'parking_off': _('You cant book for a past date!')})

    def validate_unique(self, exclude=None):
        try:
            super(Parking, self).validate_unique()
        except ValidationError as e:
            raise ValidationError(
                {'location': 
                _("Dear " + str(self.user) +
                                  " seems that the parking places " + str(self.location) +
                                  " you want to book, is already taken on " + str(self.parking_on) + " !" +
                                  " Please select another date or other parking location! "
                                  )})

    def save(self):
        list = []
        d = self.parking_on
        while d <= self.parking_off:
            list.append(
                Parking(user=self.user,
                        email=self.email,
                        parking_on=d,
                        parking_off=d,
                        location=self.location
                        )
            )
            d = d + timedelta(days=1)
        Parking.objects.bulk_create(list)

Admin:

from django.contrib import admin
from .models import Parking
from django.contrib import messages


def remove_from_fieldsets(fieldsets, fields):
        for fieldset in fieldsets:
            for field in fields:
                if field in fieldset[1]['fields']:
                    new_fields = []
                    for new_field in fieldset[1]['fields']:
                        if not new_field in fields:
                            new_fields.append(new_field)

                    fieldset[1]['fields'] = tuple(new_fields)
                    break
class ParkingModelAdmin(admin.ModelAdmin):
    list_display = ["user", "location",
                    "parking_on"]
    list_display_links = [ "location"]
    list_editable = [ "parking_on"]
    list_filter = ["parking_on", "location", "email"]
    search_fields = ["location", "parking_on"]
    date_hierarchy = 'parking_on'

    class Meta: 

        model = Parking


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

    def save_model(self, request, obj, form, change):
        # add an additional message
        user = request.user
        messages.info(request, "Dear " + str(user)+ " "+ " please note that your parking plot has been reserved")
        super(ParkingModelAdmin, self).save_model(request, obj, form, change)


    def get_fieldsets(self, request, obj=None):
        fieldsets = super(ParkingModelAdmin, self).get_fieldsets(request, obj)

        if not request.user.is_superuser:
            remove_from_fieldsets(fieldsets, ('parking_off',))

        return fieldsets

admin.site.register(Parking, ParkingModelAdmin)

Поскольку я удалил поле parking_offдля не администраторов я получаю ошибки, подобные приведенному ниже, что является нормальным, потому что поле не существует

TypeError at /admin/parcare/parking/add/ 
'<' not supported between instances of 'NoneType' and 'datetime.date'

Я пробовал также с попыткой, но безуспешно:

def save(self):
        try:
            list = []
            d = self.parking_on
            while d <= self.parking_off:
                list.append(
                    Parking(user=self.user,
                            email=self.email,
                            parking_on=d,
                            parking_off=d,
                            location=self.location
                            )
                )
                d = d + timedelta(days=1)
            Parking.objects.bulk_create(list)
        except:
            parking_off=self.parking_on
            list = []
            d = self.parking_on
            while d <= parking_off:
                list.append(
                    Parking(user=self.user,
                            email=self.email,
                            parking_on=d,
                            parking_off=d,
                            location=self.location
                            )
                )
                d = d + timedelta(days=1)
            Parking.objects.bulk_create(list)

Также с:

def save(self, request, obj, form, change):
    if request.user.is_superuser:
            list = []
            d = self.parking_on
            while d <= self.parking_off:
                list.append(
                    Parking(user=self.user,
                            email=self.email,
                            parking_on=d,
                            parking_off=d,
                            location=self.location
                            )
                )
                d = d + timedelta(days=1)
            Parking.objects.bulk_create(list)
    else:
        super().save(request, obj, form, change)

Пробовал также с:

 def save_model_clean(self, request, obj, form, change):
     if not request.user.is_superuser:
         if self.parking_off != self.parking_on:
             raise ValidationError(
                 {'parking_off': _('You cant book for a future date!')})
         obj.save()

1 Ответ

0 голосов
/ 27 сентября 2018

Вы правильно сделали, просто переопределите ваш save_model, чтобы включить обе вещи, если вы сохраняете данные из админ-панели как:

def save_model(self, request, obj, form, change):
    user = request.user
    if user.is_superuser:
        list = []
        d = obj.parking_on
        while d <= obj.parking_off:
            list.append(
                Parking(user=obj.user,
                        email=obj.email,
                        parking_on=d,
                        parking_off=d,
                        location=obj.location
                        )
            )
            d = d + timedelta(days=1)
        Parking.objects.bulk_create(list)
        messages.info(request, "your_custom_message")
    else:
        messages.info(request, "Dear " + str(user)+ " "+ " please note that your parking plot has been reserved")
        super().save_model(request, obj, form, change)

Это должно удовлетворить ваши потребности.

...