remove unused per isbn metadata table and related apis

This commit is contained in:
j 2016-01-21 11:58:07 +05:30
parent 8267fa8b39
commit 6f4672d296
6 changed files with 14 additions and 133 deletions

View file

@ -289,12 +289,6 @@ class Changelog(db.Model):
peer.save() peer.save()
return True return True
def action_editmeta(self, user, timestamp, key, value, data):
return True
def action_resetmeta(self, user, timestamp, key, value):
return True
@classmethod @classmethod
def aggregated_changes(cls, since=None, user_id=None): def aggregated_changes(cls, since=None, user_id=None):
from item.models import Item from item.models import Item
@ -429,10 +423,6 @@ class Changelog(db.Model):
if peer: if peer:
data = peer.json(['username', 'contact']) data = peer.json(['username', 'contact'])
action[peer_id] = [revision, timestamp, data] action[peer_id] = [revision, timestamp, data]
elif op == 'editmeta':
pass
elif op == 'resetmeta':
pass
else: else:
print('unknonw action', data) print('unknonw action', data)
_changes = [] _changes = []

View file

@ -263,40 +263,6 @@ def findMetadata(data):
actions.register(findMetadata) actions.register(findMetadata)
def getMetadata(data):
'''
takes {
key: value
includeEdits: boolean
}
key can be one of the supported identifiers: isbn10, isbn13, oclc, olid,...
'''
if 'includeEdits' in data:
include_edits = data.pop('includeEdits')
else:
include_edits = False
key, value = next(iter(data.items()))
value = cleanup_id(key, value)
response = meta.lookup(key, value)
if include_edits:
response.update(models.Metadata.load(key, value))
for key in [k['id'] for k in settings.config['itemKeys'] if isinstance(k['type'], list)]:
if key in response and not isinstance(response[key], list):
response[key] = [response[key]] if response[key] else []
return response
actions.register(getMetadata)
def resetMetadata(data):
item = models.Item.get(data['id'])
if item and 'primaryid' in item.meta:
meta = models.Metadata.get(*item.meta['primaryid'])
if meta:
meta.reset()
return {}
actions.register(resetMetadata)
def download(data): def download(data):
''' '''
takes { takes {

View file

@ -760,81 +760,6 @@ class Transfer(db.Model):
state.db.session.add(self) state.db.session.add(self)
state.db.session.commit() state.db.session.commit()
class Metadata(db.Model):
__tablename__ = 'metadata'
created = sa.Column(sa.DateTime())
modified = sa.Column(sa.DateTime())
id = sa.Column(sa.Integer(), primary_key=True)
key = sa.Column(sa.String(256))
value = sa.Column(sa.String(256))
data = sa.Column(MutableDict.as_mutable(sa.PickleType(pickler=json_pickler)))
def __repr__(self):
return '='.join([self.key, self.value])
@property
def timestamp(self):
return utils.datetime2ts(self.modified)
@classmethod
def get(cls, key, value):
return cls.query.filter_by(key=key, value=value).first()
@classmethod
def get_or_create(cls, key, value):
m = cls.get(key, value)
if not m:
m = cls(key=key, value=value)
m.created = datetime.utcnow()
m.data = {}
m.save()
return m
def save(self):
self.modified = datetime.utcnow()
state.db.session.add(self)
state.db.session.commit()
def reset(self):
user = state.user()
Changelog.record(user, 'resetmeta', self.key, self.value)
state.db.session.delete(self)
state.db.session.commit()
self.update_items()
def edit(self, data, record=True):
changed = {}
for key in data:
if key == 'id':
continue
if data[key] != self.data.get(key):
self.data[key] = data[key]
changed[key] = data[key]
if changed:
self.save()
if record:
user = state.user()
Changelog.record(user, 'editmeta', self.key, self.value, changed)
return changed
def update_items(self):
for f in Find.query.filter_by(key=self.key, value=self.value):
if f.item:
f.item.update()
@classmethod
def load(self, key, value):
m = self.get(key, value)
if m:
if 'id' in m.data:
del m.data['id']
return m.data
return {}
def remove_unused_names(): def remove_unused_names():
used = list(set( used = list(set(
get_sort_name(a) get_sort_name(a)

View file

@ -101,4 +101,4 @@ USER_AGENT = 'OpenMediaLibrary/%s' % VERSION
DEBUG_HTTP = server.get('debug_http', False) DEBUG_HTTP = server.get('debug_http', False)
DB_VERSION = 6 DB_VERSION = 7

View file

@ -224,20 +224,13 @@ def upgrade_db(old, new=None):
if old <= '20160110-583-59a3709' and new > '20160110-583-59a3709': if old <= '20160110-583-59a3709' and new > '20160110-583-59a3709':
with db.session() as session: with db.session() as session:
import ox import ox
from item.models import Item, Find, Metadata from item.models import Item, Find
from meta.utils import decode_html_data from meta.utils import decode_html_data
def cleanup_description(data): def cleanup_description(data):
if 'description' in data: if 'description' in data:
description = data['description'] description = data['description']
description = description.replace('<br>', '\n').replace('</p><p>', '\n\n') description = description.replace('<br>', '\n').replace('</p><p>', '\n\n')
data['description'] = ox.strip_tags(description).strip() data['description'] = ox.strip_tags(description).strip()
for m in Metadata.query:
data = decode_html_data(m.data.copy())
cleanup_description(data)
if data != m.data:
#print(m, m.data, '\n',data, '\n\n')
m.data = data
m.save()
for i in Item.query: for i in Item.query:
changed = False changed = False
meta = decode_html_data(i.meta.copy()) meta = decode_html_data(i.meta.copy())
@ -387,10 +380,6 @@ def upgrade_db(old, new=None):
if 'id' in i.meta: if 'id' in i.meta:
del i.meta['id'] del i.meta['id']
i.save() i.save()
for m in item.models.Metadata.query:
if 'id' in m.data:
del m.data['id']
m.save()
if old <= '20160106-495-d1b9e96': if old <= '20160106-495-d1b9e96':
run_sql('CREATE INDEX ix_useritem_user ON useritem ("user_id")') run_sql('CREATE INDEX ix_useritem_user ON useritem ("user_id")')
if old <= '20160106-497-c86ba8a': if old <= '20160106-497-c86ba8a':
@ -457,7 +446,6 @@ def update_database():
item.models.File, item.models.File,
item.models.Find, item.models.Find,
item.models.Item, item.models.Item,
item.models.Metadata,
item.models.Scrape, item.models.Scrape,
item.models.Sort, item.models.Sort,
item.models.Transfer, item.models.Transfer,

View file

@ -303,6 +303,8 @@ class Update(Thread):
db_version = migrate_5() db_version = migrate_5()
if db_version < 6: if db_version < 6:
db_version = migrate_6() db_version = migrate_6()
if db_version < 7:
db_version = migrate_7()
settings.server['db_version'] = settings.DB_VERSION settings.server['db_version'] = settings.DB_VERSION
def run(self): def run(self):
@ -407,3 +409,13 @@ def migrate_6():
session.commit() session.commit()
return 6 return 6
def migrate_7():
with db.session() as session:
import changelog
for c in changelog.Changelog.query:
if 'editmeta' in c.data or 'resetmeta' in c.data:
session.delete(c)
session.commit()
db.run_sql('DROP TABLE IF EXISTS metadata')
db.run_sql('VACUUM')
return 7