mirror of
https://github.com/pretix/pretix.git
synced 2026-05-05 15:14:04 +00:00
Added a model for logging actions
This commit is contained in:
@@ -5,6 +5,7 @@ from .items import (
|
||||
Item, ItemCategory, ItemVariation, Property, PropertyValue, Question,
|
||||
Quota, VariationsField, itempicture_upload_to,
|
||||
)
|
||||
from .log import LogEntry
|
||||
from .orders import (
|
||||
CachedTicket, CartPosition, ObjectWithAnswers, Order, OrderPosition,
|
||||
QuestionAnswer, generate_secret,
|
||||
@@ -16,5 +17,5 @@ __all__ = [
|
||||
'ItemCategory', 'Item', 'Property', 'PropertyValue', 'ItemVariation', 'VariationsField', 'Question',
|
||||
'Quota', 'Order', 'CachedTicket', 'QuestionAnswer', 'ObjectWithAnswers', 'OrderPosition',
|
||||
'CartPosition', 'EventSetting', 'OrganizerSetting', 'EventLock', 'cachedfile_name', 'itempicture_upload_to',
|
||||
'generate_secret'
|
||||
'generate_secret', 'LogEntry'
|
||||
]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import uuid
|
||||
|
||||
from django.contrib.contenttypes.fields import GenericRelation
|
||||
from django.db import models
|
||||
from django.db.models.signals import post_delete
|
||||
from django.dispatch import receiver
|
||||
@@ -26,3 +27,21 @@ def cached_file_delete(sender, instance, **kwargs):
|
||||
if instance.file:
|
||||
# Pass false so FileField doesn't save the model.
|
||||
instance.file.delete(False)
|
||||
|
||||
|
||||
class LoggedModel(models.Model):
|
||||
logentries = GenericRelation('LogEntry')
|
||||
|
||||
def log_action(self, user, action, data):
|
||||
from .log import LogEntry
|
||||
from .event import Event
|
||||
|
||||
event = None
|
||||
if isinstance(self, Event):
|
||||
event = self
|
||||
elif hasattr(self, 'event'):
|
||||
event = self.event
|
||||
LogEntry.objects.create(content_object=self, user=user, action=action, data=data, event=event)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@@ -11,13 +11,14 @@ from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from pretix.base.i18n import I18nCharField
|
||||
from pretix.base.models.base import LoggedModel
|
||||
from pretix.base.settings import SettingsProxy
|
||||
|
||||
from .auth import User
|
||||
from .organizer import Organizer
|
||||
|
||||
|
||||
class Event(models.Model):
|
||||
class Event(LoggedModel):
|
||||
"""
|
||||
This model represents an event. An event is anything you can buy
|
||||
tickets for.
|
||||
|
||||
@@ -10,12 +10,13 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from typing import List, Tuple
|
||||
|
||||
from pretix.base.i18n import I18nCharField, I18nTextField
|
||||
from pretix.base.models.base import LoggedModel
|
||||
|
||||
from ..types import VariationDict
|
||||
from .event import Event
|
||||
|
||||
|
||||
class ItemCategory(models.Model):
|
||||
class ItemCategory(LoggedModel):
|
||||
"""
|
||||
Items can be sorted into these categories.
|
||||
|
||||
@@ -72,7 +73,7 @@ def itempicture_upload_to(instance, filename: str) -> str:
|
||||
)
|
||||
|
||||
|
||||
class Item(models.Model):
|
||||
class Item(LoggedModel):
|
||||
"""
|
||||
An item is a thing which can be sold. It belongs to an event and may or may not belong to a category.
|
||||
Items are often also called 'products' but are named 'items' internally due to historic reasons.
|
||||
@@ -539,7 +540,7 @@ class VariationsField(models.ManyToManyField):
|
||||
return super(RelatedField, self).formfield(**defaults)
|
||||
|
||||
|
||||
class Question(models.Model):
|
||||
class Question(LoggedModel):
|
||||
"""
|
||||
A question is an input field that can be used to extend a ticket
|
||||
by custom information, e.g. "Attendee age". A question can allow one o several
|
||||
@@ -613,7 +614,7 @@ class Question(models.Model):
|
||||
self.event.get_cache().clear()
|
||||
|
||||
|
||||
class Quota(models.Model):
|
||||
class Quota(LoggedModel):
|
||||
"""
|
||||
A quota is a "pool of tickets". It is there to limit the number of items
|
||||
of a certain type to be sold. For example, you could have a quota of 500
|
||||
|
||||
30
src/pretix/base/models/log.py
Normal file
30
src/pretix/base/models/log.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from django.contrib.contenttypes.fields import GenericForeignKey
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db import models
|
||||
|
||||
|
||||
class LogEntry(models.Model):
|
||||
"""
|
||||
Represents a change or action that has been performed on another object
|
||||
in the database. This uses django.contrib.contenttypes to allow a
|
||||
relation to an arbitrary database object.
|
||||
|
||||
:param user: The user that performed the action
|
||||
:type user: User
|
||||
:param action_type: The type of action that has been performed. This is
|
||||
used to look up the renderer used to describe the action in a human-
|
||||
readable way. This should be some namespaced value using dotted
|
||||
notationto avaoid duplicates, e.g.
|
||||
``"pretix.plugins.banktransfer.incoming_transfer"``.
|
||||
:type action_type: str
|
||||
:param data: Arbitrary data that can be used by the log action renderer
|
||||
:type data: str
|
||||
"""
|
||||
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
||||
object_id = models.PositiveIntegerField()
|
||||
content_object = GenericForeignKey('content_type', 'object_id')
|
||||
datetime = models.DateTimeField(auto_now_add=True)
|
||||
user = models.ForeignKey('User', null=True, blank=True, on_delete=models.PROTECT)
|
||||
event = models.ForeignKey('Event', null=True, blank=True, on_delete=models.CASCADE)
|
||||
action_type = models.CharField(max_length=255)
|
||||
data = models.TextField(default='{}')
|
||||
@@ -7,7 +7,7 @@ from django.utils.timezone import now
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from typing import List, Union
|
||||
|
||||
from .base import CachedFile
|
||||
from .base import CachedFile, LoggedModel
|
||||
from .event import Event
|
||||
from .items import Item, ItemVariation, Question, Quota
|
||||
|
||||
@@ -16,7 +16,7 @@ def generate_secret():
|
||||
return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(16))
|
||||
|
||||
|
||||
class Order(models.Model):
|
||||
class Order(LoggedModel):
|
||||
"""
|
||||
An order is created when a user clicks 'buy' on his cart. It holds
|
||||
several OrderPositions and is connected to an user. It has an
|
||||
|
||||
@@ -3,12 +3,13 @@ from django.db import models
|
||||
from django.utils.functional import cached_property
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from pretix.base.models.base import LoggedModel
|
||||
from pretix.base.settings import SettingsProxy
|
||||
|
||||
from .auth import User
|
||||
|
||||
|
||||
class Organizer(models.Model):
|
||||
class Organizer(LoggedModel):
|
||||
"""
|
||||
This model represents an entity organizing events, e.g. a company, institution,
|
||||
charity, person, …
|
||||
|
||||
Reference in New Issue
Block a user