implement sharemetadata, fixes 165
This commit is contained in:
parent
8c5e39448b
commit
7a42473919
11 changed files with 162 additions and 43 deletions
|
|
@ -380,7 +380,7 @@ class Metadata(db.Model):
|
|||
|
||||
@classmethod
|
||||
def get(cls, user_id, item_id):
|
||||
return cls.query.filter_by(user_id=user_id,item_id=item_id).first()
|
||||
return cls.query.filter_by(item_id=item_id, user_id=user_id).first()
|
||||
|
||||
@classmethod
|
||||
def get_or_create(cls, user_id, item_id, data=None, commit=True):
|
||||
|
|
@ -392,19 +392,24 @@ class Metadata(db.Model):
|
|||
m.data = data
|
||||
else:
|
||||
m.data = {}
|
||||
m.save(commit)
|
||||
m.save(commit=commit)
|
||||
elif data:
|
||||
m.edit(data, commit)
|
||||
m.edit(data, commit=commit)
|
||||
return m
|
||||
|
||||
def save(self, commit=True):
|
||||
self.modified = datetime.utcnow()
|
||||
self.data_hash = hashlib.sha1(json.dumps(self.data, ensure_ascii=False, sort_keys=True).encode()).hexdigest()
|
||||
def get_hash(self):
|
||||
return utils.get_meta_hash(self.data.copy())
|
||||
|
||||
def save(self, commit=True, modified=None):
|
||||
if modified is None:
|
||||
self.modified = datetime.utcnow()
|
||||
else:
|
||||
self.modified = modified
|
||||
state.db.session.add(self)
|
||||
if commit:
|
||||
state.db.session.commit()
|
||||
|
||||
def edit(self, data, commit=True):
|
||||
def edit(self, data, commit=True, modified=True):
|
||||
changes = {}
|
||||
if 'isbn' in data and isinstance(data['isbn'], list):
|
||||
isbns = [utils.to_isbn13(isbn) for isbn in data['isbn']]
|
||||
|
|
@ -420,7 +425,8 @@ class Metadata(db.Model):
|
|||
self.data[key] = data[key]
|
||||
changes[key] = data[key]
|
||||
if changes:
|
||||
self.save(commit)
|
||||
self.data_hash = self.get_hash()
|
||||
self.save(commit=commit, modified=modified)
|
||||
return changes
|
||||
|
||||
def delete(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue