78 lines
2 KiB
Python
78 lines
2 KiB
Python
# -*- coding: utf-8 -*-
|
|
from datetime import datetime
|
|
import json
|
|
import logging
|
|
import os
|
|
import shutil
|
|
|
|
from sqlalchemy.orm import load_only
|
|
import ox
|
|
import sqlalchemy as sa
|
|
|
|
from changelog import add_record
|
|
from db import MutableDict
|
|
import db
|
|
import json_pickler
|
|
import settings
|
|
import state
|
|
import utils
|
|
import media
|
|
from websocket import trigger_event
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Annotation(db.Model):
|
|
__tablename__ = 'annotation'
|
|
|
|
_id = sa.Column(sa.Integer(), primary_key=True)
|
|
id = sa.Column(sa.String(43))
|
|
created = sa.Column(sa.DateTime())
|
|
modified = sa.Column(sa.DateTime())
|
|
|
|
user = sa.orm.relationship('User', backref=sa.orm.backref('annotations', lazy='dynamic'))
|
|
item = sa.orm.relationship('Item', backref=sa.orm.backref('items', lazy='dynamic'))
|
|
data = sa.Column(MutableDict.as_mutable(sa.PickleType(pickler=json_pickler)))
|
|
|
|
def __init__(self, item_id, user_id, data):
|
|
self.created = datetime.utcnow()
|
|
self.modified = datetime.utcnow()
|
|
self.item_id = item_id
|
|
self.user_id = user_id
|
|
self.data = data
|
|
|
|
@classmethod
|
|
def create(cls, **kwargs):
|
|
a = cls(**kwargs)
|
|
state.db.session.add(a)
|
|
state.db.session.commit()
|
|
|
|
@classmethod
|
|
def get(cls, user, item_id, annotation_id):
|
|
for a in cls.query.filter_by(item_id=item_id, user=user, _id=annotation_id):
|
|
if a.data.get('id') == annotation_id:
|
|
return a
|
|
|
|
@classmethod
|
|
def get_by_item(cls, user, item_id):
|
|
annotations = []
|
|
for a in cls.query.filter_by(item_id=item_id):
|
|
annotations.append(a.json())
|
|
return annotations
|
|
|
|
def save(self):
|
|
_id = self.data.get('id')
|
|
if _id:
|
|
self._id = _id
|
|
state.db.session.add(self)
|
|
state.db.session.commit()
|
|
|
|
def json(self):
|
|
data = self.data.copy()
|
|
data['created'] = self.created
|
|
data['modified'] = self.modified
|
|
data['user'] = self.user_id
|
|
data['_id'] = ox.toAZ(self.id)
|
|
return data
|
|
|