gallery.accords-library.com/server/szurubooru/func/snapshots.py

144 lines
4.3 KiB
Python
Raw Normal View History

import datetime
from szurubooru import db
def get_tag_snapshot(tag):
2016-04-22 18:58:04 +00:00
return {
'names': [tag_name.name for tag_name in tag.names],
2016-04-20 09:15:36 +00:00
'category': tag.category.name,
'suggestions': sorted(rel.first_name for rel in tag.suggestions),
'implications': sorted(rel.first_name for rel in tag.implications),
}
2016-04-22 18:58:04 +00:00
2016-04-22 18:58:04 +00:00
def get_post_snapshot(post):
return {
'source': post.source,
'safety': post.safety,
'checksum': post.checksum,
'tags': sorted([tag.first_name for tag in post.tags]),
'relations': sorted([
rel.post_id for rel in post.relations]),
2016-04-22 18:58:04 +00:00
'notes': sorted([{
'polygon': note.polygon,
'text': note.text,
2016-05-28 09:22:25 +00:00
} for note in post.notes], key=lambda x: x['polygon']),
2016-04-22 18:58:04 +00:00
'flags': post.flags,
'featured': post.is_featured,
}
def get_tag_category_snapshot(category):
return {
'name': category.name,
'color': category.color,
'default': True if category.default else False,
}
2016-04-21 17:25:38 +00:00
def get_previous_snapshot(snapshot):
2016-08-14 08:45:00 +00:00
assert snapshot
2016-04-21 17:25:38 +00:00
return db.session \
.query(db.Snapshot) \
.filter(db.Snapshot.resource_type == snapshot.resource_type) \
.filter(db.Snapshot.resource_id == snapshot.resource_id) \
.filter(db.Snapshot.creation_time < snapshot.creation_time) \
.order_by(db.Snapshot.creation_time.desc()) \
.limit(1) \
.first()
2016-04-20 09:15:36 +00:00
def get_snapshots(entity):
2016-08-14 08:45:00 +00:00
assert entity
resource_type, resource_id, _ = db.util.get_resource_info(entity)
2016-04-20 09:15:36 +00:00
return db.session \
.query(db.Snapshot) \
.filter(db.Snapshot.resource_type == resource_type) \
.filter(db.Snapshot.resource_id == resource_id) \
2016-04-20 09:15:36 +00:00
.order_by(db.Snapshot.creation_time.desc()) \
.all()
def serialize_snapshot(snapshot, earlier_snapshot=()):
2016-08-14 08:45:00 +00:00
assert snapshot
if earlier_snapshot is ():
earlier_snapshot = get_previous_snapshot(snapshot)
return {
'operation': snapshot.operation,
'type': snapshot.resource_type,
'id': snapshot.resource_repr,
2016-04-21 17:25:38 +00:00
'user': snapshot.user.name if snapshot.user else None,
'data': snapshot.data,
'earlier-data': earlier_snapshot.data if earlier_snapshot else None,
'time': snapshot.creation_time,
}
def get_serialized_history(entity):
if not entity:
return []
2016-04-20 09:15:36 +00:00
ret = []
earlier_snapshot = None
for snapshot in reversed(get_snapshots(entity)):
ret.insert(0, serialize_snapshot(snapshot, earlier_snapshot))
earlier_snapshot = snapshot
2016-04-20 09:15:36 +00:00
return ret
2016-04-30 21:53:50 +00:00
def _save(operation, entity, auth_user):
2016-08-14 08:45:00 +00:00
assert operation
assert entity
serializers = {
'tag': get_tag_snapshot,
'tag_category': get_tag_category_snapshot,
'post': get_post_snapshot,
}
resource_type, resource_id, resource_repr = (
db.util.get_resource_info(entity))
2016-07-03 12:46:15 +00:00
now = datetime.datetime.utcnow()
snapshot = db.Snapshot()
snapshot.creation_time = now
snapshot.operation = operation
snapshot.resource_type = resource_type
snapshot.resource_id = resource_id
snapshot.resource_repr = resource_repr
2016-04-24 14:34:06 +00:00
snapshot.data = serializers[resource_type](entity)
snapshot.user = auth_user
2016-04-20 09:15:36 +00:00
earlier_snapshots = get_snapshots(entity)
delta = datetime.timedelta(minutes=10)
snapshots_left = len(earlier_snapshots)
while earlier_snapshots:
last_snapshot = earlier_snapshots.pop(0)
is_fresh = now - last_snapshot.creation_time <= delta
if snapshot.data != last_snapshot.data:
if not is_fresh or last_snapshot.user != auth_user:
break
db.session.delete(last_snapshot)
if snapshot.operation != db.Snapshot.OPERATION_DELETED:
snapshot.operation = last_snapshot.operation
snapshots_left -= 1
if not snapshots_left and operation == db.Snapshot.OPERATION_DELETED:
pass
else:
db.session.add(snapshot)
2016-04-30 21:53:50 +00:00
def save_entity_creation(entity, auth_user):
2016-08-14 08:45:00 +00:00
assert entity
2016-04-30 21:53:50 +00:00
_save(db.Snapshot.OPERATION_CREATED, entity, auth_user)
2016-04-30 21:53:50 +00:00
def save_entity_modification(entity, auth_user):
2016-08-14 08:45:00 +00:00
assert entity
2016-04-30 21:53:50 +00:00
_save(db.Snapshot.OPERATION_MODIFIED, entity, auth_user)
2016-04-30 21:53:50 +00:00
def save_entity_deletion(entity, auth_user):
2016-08-14 08:45:00 +00:00
assert entity
2016-04-30 21:53:50 +00:00
_save(db.Snapshot.OPERATION_DELETED, entity, auth_user)