def del_format(): # Authentication fail = Auth.assert_is_role (Role.ROLE_ADMIN) if fail: return fail # Target format format_id = CTK.request.url.split('/')[-1] # Check whether it can be deleted q = ("SELECT COUNT(*) as total " "FROM view_asset_formats " "WHERE formats_id = %(format_id)s;" %(locals())) re = Query(q) usage = re['total'][0] if usage != 0: subs = [{'n':'','s':''},{'n':'n','s':''}][(usage > 1)] subs['num'] = usage msg = ("Imposible realizar la operaciĆ³n ya que existe%(n)s " "%(num)d activo%(s)s en ese formato" % subs) return default (msg) # Delete q = "DELETE FROM transcode_targets WHERE source_id = %(format_id)s OR target_id = %(format_id)s;" %(locals()) q += "DELETE FROM formats WHERE id = %(format_id)s;" %(locals()) q = "START TRANSACTION; %s COMMIT;" % q ok = transaction_check_success (q) if not ok: msg = 'No se pudo eliminar el formato.' return default (msg) return default()
def del_format(): # Authentication fail = Auth.assert_is_role(Role.ROLE_ADMIN) if fail: return fail # Target format format_id = CTK.request.url.split('/')[-1] # Check whether it can be deleted q = ("SELECT COUNT(*) as total " "FROM view_asset_formats " "WHERE formats_id = %(format_id)s;" % (locals())) re = Query(q) usage = re['total'][0] if usage != 0: subs = [{'n': '', 's': ''}, {'n': 'n', 's': ''}][(usage > 1)] subs['num'] = usage msg = ("Imposible realizar la operaciĆ³n ya que existe%(n)s " "%(num)d activo%(s)s en ese formato" % subs) return default(msg) # Delete q = "DELETE FROM transcode_targets WHERE source_id = %(format_id)s OR target_id = %(format_id)s;" % ( locals()) q += "DELETE FROM formats WHERE id = %(format_id)s;" % (locals()) q = "START TRANSACTION; %s COMMIT;" % q ok = transaction_check_success(q) if not ok: msg = 'No se pudo eliminar el formato.' return default(msg) return default()
def edit_format_apply(): # Authentication fail = Auth.assert_is_role(Role.ROLE_ADMIN) if fail: return fail format_id = CTK.post.pop('formatid') if not format_id: return CTK.HTTP_Error(406) # Update the database q = '' for key in ['lossy_flag', 'format']: if key in CTK.post: q += "UPDATE formats SET %s='%s' WHERE id = %s;" % \ (key, CTK.post[key], format_id) if 'target' in CTK.post: q += "INSERT INTO transcode_targets (source_id, target_id) VALUES ('%s','%s');" % \ (format_id,CTK.post['target']) if q: q = "START TRANSACTION; %s COMMIT;" % q if not transaction_check_success(q): return {'ret': "error"} return {'ret': "ok", 'redirect': '%s/edit/%s' % (LOCATION, format_id)}
def edit_format_apply(): # Authentication fail = Auth.assert_is_role (Role.ROLE_ADMIN) if fail: return fail format_id = CTK.post.pop('formatid') if not format_id: return CTK.HTTP_Error(406) # Update the database q = '' for key in ['lossy_flag', 'format']: if key in CTK.post: q += "UPDATE formats SET %s='%s' WHERE id = %s;" % \ (key, CTK.post[key], format_id) if 'target' in CTK.post: q += "INSERT INTO transcode_targets (source_id, target_id) VALUES ('%s','%s');" % \ (format_id,CTK.post['target']) if q: q = "START TRANSACTION; %s COMMIT;" % q if not transaction_check_success (q): return {'ret': "error"} return {'ret': "ok", 'redirect': '%s/edit/%s'%(LOCATION,format_id)}
def update_files (files): q = '' for f in files: sql_columns, sql_values = [], [] for key,value in f.items(): if value != None: sql_columns.append(key) sql_values.append("'%s'" % value) q += "REPLACE INTO files (%s) VALUES (%s);" \ % (','.join(sql_columns), ','.join(sql_values)) q = 'START TRANSACTION; %s COMMIT' % q ok = transaction_check_success (q) if not ok: raise Error.SQLException
def update_files(files): q = '' for f in files: sql_columns, sql_values = [], [] for key, value in f.items(): if value != None: sql_columns.append(key) sql_values.append("'%s'" % value) q += "REPLACE INTO files (%s) VALUES (%s);" \ % (','.join(sql_columns), ','.join(sql_values)) q = 'START TRANSACTION; %s COMMIT' % q ok = transaction_check_success(q) if not ok: raise Error.SQLException
def del_format_target(): """Deletes transcode targets""" # Authentication fail = Auth.assert_is_role(Role.ROLE_ADMIN) if fail: return fail # Target format req = CTK.request.url.split('/') source_id = req[-2] target_id = req[-1] # Delete q = "DELETE FROM transcode_targets " \ "WHERE source_id = %(source_id)s AND target_id = %(target_id)s;" %(locals()) ok = transaction_check_success(q) return CTK.HTTP_Redir("%s/edit/%s" % (LOCATION, source_id))
def del_format_target(): """Deletes transcode targets""" # Authentication fail = Auth.assert_is_role (Role.ROLE_ADMIN) if fail: return fail # Target format req = CTK.request.url.split('/') source_id = req[-2] target_id = req[-1] # Delete q = "DELETE FROM transcode_targets " \ "WHERE source_id = %(source_id)s AND target_id = %(target_id)s;" %(locals()) ok = transaction_check_success (q) return CTK.HTTP_Redir("%s/edit/%s" % (LOCATION, source_id))
def __delete_asset (self): if not self.__delete_attachment(): return False self._asset._file = {} asset = self._asset q = ("DELETE FROM parts WHERE derivative_id = '%(id)d';" + "DELETE FROM replacements WHERE replacer_id = '%(id)d';" + "DELETE FROM asset_versions WHERE derivative_id = '%(id)d';" + "DELETE FROM asset_formats WHERE target_id = '%(id)d' OR source_id = %(id)d;" + "DELETE FROM children WHERE child_id = '%(id)d';" + "DELETE FROM assets WHERE id = '%(id)s'") % ({'id': asset['id']}) q = "START TRANSACTION; %s; COMMIT;" % q if not transaction_check_success (q): return False return True
def delete_file (the_file): q = 'DELETE FROM files WHERE id = %s;' % the_file['id'] q = 'START TRANSACTION; %s COMMIT' % q ok = transaction_check_success (q) if not ok: raise Error.SQLException filename = the_file.get('filename') media = '%s/%s' % (ASSET_PATH, filename) thumb = '%s/%s.%s' % (THUMB_PATH, filename, THUMB_EXT) try: os.unlink(media) os.unlink(thumb) except: return False return True
def delete_file(the_file): q = 'DELETE FROM files WHERE id = %s;' % the_file['id'] q = 'START TRANSACTION; %s COMMIT' % q ok = transaction_check_success(q) if not ok: raise Error.SQLException filename = the_file.get('filename') media = '%s/%s' % (ASSET_PATH, filename) thumb = '%s/%s.%s' % (THUMB_PATH, filename, THUMB_EXT) try: os.unlink(media) os.unlink(thumb) except: return False return True
def __delete_asset(self): if not self.__delete_attachment(): return False self._asset._file = {} asset = self._asset q = ( "DELETE FROM parts WHERE derivative_id = '%(id)d';" + "DELETE FROM replacements WHERE replacer_id = '%(id)d';" + "DELETE FROM asset_versions WHERE derivative_id = '%(id)d';" + "DELETE FROM asset_formats WHERE target_id = '%(id)d' OR source_id = %(id)d;" + "DELETE FROM children WHERE child_id = '%(id)d';" + "DELETE FROM assets WHERE id = '%(id)s'") % ({ 'id': asset['id'] }) q = "START TRANSACTION; %s; COMMIT;" % q if not transaction_check_success(q): return False return True
def update(self): """Re-ingest modified asset""" self._check_existence() asset = self._asset sql_values = self._process_update_values(DB_KEYS) # assets table q = "UPDATE assets SET %s WHERE id = %s;" % (','.join(sql_values), asset['id']) # relations: they should remain the same(children, versions, asset_formats) old_asset = Asset(asset['id']) if asset._parts != old_asset._parts and asset._parts.has_key( 'has_parts_of'): q += "DELETE FROM parts WHERE derivative_id='%d';" % \ (asset['id']) if asset._parts.get('is_part_of', None): for x in asset._parts['is_part_of']: q += "INSERT INTO parts VALUES (%d, %d);" % \ (x, asset['id']) q += "DELETE FROM files WHERE id='%d';" % asset['id'] if asset._file: keys, values = [], [] for key, value in asset._file.items(): if value: keys.append(key) values.append("'%s'" % str(value)) q += ("REPLACE INTO files (%s) VALUES (%s);" % (','.join(keys), ','.join(values))) q = "START TRANSACTION; %s COMMIT;" % q if transaction_check_success(q): return True return False
def update (self): """Re-ingest modified asset""" self._check_existence() asset = self._asset sql_values = self._process_update_values (DB_KEYS) # assets table q = "UPDATE assets SET %s WHERE id = %s;" %(','.join(sql_values), asset['id']) # relations: they should remain the same(children, versions, asset_formats) old_asset = Asset(asset['id']) if asset._parts != old_asset._parts and asset._parts.has_key('has_parts_of'): q += "DELETE FROM parts WHERE derivative_id='%d';" % \ (asset['id']) if asset._parts.get('is_part_of', None): for x in asset._parts['is_part_of']: q += "INSERT INTO parts VALUES (%d, %d);" % \ (x, asset['id']) q += "DELETE FROM files WHERE id='%d';" % asset['id'] if asset._file: keys, values = [], [] for key,value in asset._file.items(): if value: keys.append(key) values.append("'%s'" % str(value)) q += ("REPLACE INTO files (%s) VALUES (%s);" % (','.join(keys), ','.join(values))) q = "START TRANSACTION; %s COMMIT;" % q if transaction_check_success (q): return True return False
def add (self): """Feed asset to platform""" sql_columns = [] sql_values = [] asset = self._asset db = asset._db if not db.has_key('published_flag'): db['published_flag'] = 0 keys = DB_KEYS[:] keys.remove('id') for key in keys: if db.get(key): sql_columns.append(key) sql_values.append("'%s'" % db[key]) q = "INSERT INTO assets (%s) VALUES (%s);" \ % (','.join(sql_columns), ','.join(sql_values)) query = Query(q) try: asset['id'] = query.result[0]['INSERT_ID'] except KeyError: return False q = ''; if asset._parent_id: q += "INSERT INTO children VALUES (%d, %d);" % \ (asset._parent_id, asset['id']) q += "INSERT INTO asset_versions VALUES (%d, %d);" % \ (asset._parent_id, asset['id']) replaces = asset._replacements.get('replaces') if replaces: for x in replaces: q += "INSERT INTO replacements VALUES (%d, %d);" % \ (asset['id'], x) # Populate attachment info if asset._file: asset._file['id'] = asset['id'] keys, values = [], [] for key,value in asset._file.items(): if value: keys.append(key) values.append("'%s'" % str(value)) q += ("INSERT INTO files (%s) VALUES (%s);" % (','.join(keys), ','.join(values))) try: for x in asset._parts['has_parts_of']: q += "INSERT INTO parts VALUES (%d, %d);" % \ (int(x), asset['id']) except (KeyError, TypeError): pass # Populate 'asset_formats' table # insert = [] for format in asset._formats: if len(format) < 3 and format.get('source'): # Insert incomplete references and remove them from # memory in case the object is reused. It should be # complete once it is reloaded insert.append(format) for format in insert: q += ("INSERT INTO asset_formats (source_id, target_id) " "VALUES (%d,%d);" % (format['source'], asset['id'])) asset._formats.remove(format) # Perform if q: q = "START TRANSACTION; %s COMMIT;" % q if not transaction_check_success (q): # Delete previous commit q = "DELETE FROM assets WHERE assets.id == '%s'" % asset['id'] query = Query(q) return False acl = ACL(self.params) acl.set_default_asset_acl(self._asset) return True
def add(self): """Feed asset to platform""" sql_columns = [] sql_values = [] asset = self._asset db = asset._db if not db.has_key('published_flag'): db['published_flag'] = 0 keys = DB_KEYS[:] keys.remove('id') for key in keys: if db.get(key): sql_columns.append(key) sql_values.append("'%s'" % db[key]) q = "INSERT INTO assets (%s) VALUES (%s);" \ % (','.join(sql_columns), ','.join(sql_values)) query = Query(q) try: asset['id'] = query.result[0]['INSERT_ID'] except KeyError: return False q = '' if asset._parent_id: q += "INSERT INTO children VALUES (%d, %d);" % \ (asset._parent_id, asset['id']) q += "INSERT INTO asset_versions VALUES (%d, %d);" % \ (asset._parent_id, asset['id']) replaces = asset._replacements.get('replaces') if replaces: for x in replaces: q += "INSERT INTO replacements VALUES (%d, %d);" % \ (asset['id'], x) # Populate attachment info if asset._file: asset._file['id'] = asset['id'] keys, values = [], [] for key, value in asset._file.items(): if value: keys.append(key) values.append("'%s'" % str(value)) q += ("INSERT INTO files (%s) VALUES (%s);" % (','.join(keys), ','.join(values))) try: for x in asset._parts['has_parts_of']: q += "INSERT INTO parts VALUES (%d, %d);" % \ (int(x), asset['id']) except (KeyError, TypeError): pass # Populate 'asset_formats' table # insert = [] for format in asset._formats: if len(format) < 3 and format.get('source'): # Insert incomplete references and remove them from # memory in case the object is reused. It should be # complete once it is reloaded insert.append(format) for format in insert: q += ("INSERT INTO asset_formats (source_id, target_id) " "VALUES (%d,%d);" % (format['source'], asset['id'])) asset._formats.remove(format) # Perform if q: q = "START TRANSACTION; %s COMMIT;" % q if not transaction_check_success(q): # Delete previous commit q = "DELETE FROM assets WHERE assets.id == '%s'" % asset['id'] query = Query(q) return False acl = ACL(self.params) acl.set_default_asset_acl(self._asset) return True