def include_apply(): collection_id = CTK.post.pop('collection_id') asset_id = CTK.post.pop('asset_id') if not collection_id or not asset_id: return CTK.HTTP_Error(400) acl = ACL() addable = acl.filter_collections("ad", [collection_id]) if not int(collection_id) in addable: return CTK.HTTP_Error(401, 'Sin permisos para añadir a esta colección') asset_id = int(asset_id) asset = Asset.Asset(asset_id) collection = Collection.Collection(collection_id) if asset['collections_id'] and not asset_id in collection: return CTK.HTTP_Error(400) if asset['collections_id']: asset['collections_id'] = None else: asset['collections_id'] = int(collection_id) op = OpAsset(asset) ret = op.update() if ret == True: return { 'ret': "ok", 'redirect': '%s/edit/%s' % (LOCATION, collection_id) } else: return {'ret': "error"}
def publish_asset_apply (): # Authentication fail = Auth.assert_is_role (Role.ROLE_PUBLISHER) if fail: return fail acl = ACL() ret = [] for key in CTK.post: if not key.startswith('published_'): continue flag = int(CTK.post[key]) asset_id = key[len('published_'):] asset = Asset(asset_id) if asset['published_flag'] == flag: continue asset['published_flag'] = flag asset['publisher_id'] = Auth.get_user_id() asset['date_available'] = "CURRENT_TIMESTAMP()" op = OpAsset(asset) ret.append(op.update()) if ret[-1] == False: break if asset['published_flag']: acl.set_published_asset_acl(asset, True) else: acl.set_published_asset_acl(asset, False) if False in ret: return {'ret': "error"} return {'ret': "ok"}
def del_asset(): """Delete asset""" # Target asset asset_id = CTK.request.url.split('/')[-1] try: asset_id = __check_asset_id(asset_id) asset = Asset (asset_id) if not asset['id']: return CTK.HTTP_Redir('/') except: return CTK.HTTP_Redir('/') # Authentication fail_admin = Auth.assert_is_role (Role.ROLE_ADMIN) user_id = Auth.get_user_id() is_creator = (asset['creator_id'] == user_id) if fail_admin and not is_creator: return fail_admin op = OpAsset (asset) result = op.delete() if result['ret'] == False: return default() if result['type'] == 'total': return CTK.HTTP_Redir(LOCATION) return default (ERROR_IN_USE)
def publish_asset_apply(): # Authentication fail = Auth.assert_is_role(Role.ROLE_PUBLISHER) if fail: return fail acl = ACL() ret = [] for key in CTK.post: if not key.startswith('published_'): continue flag = int(CTK.post[key]) asset_id = key[len('published_'):] asset = Asset(asset_id) if asset['published_flag'] == flag: continue asset['published_flag'] = flag asset['publisher_id'] = Auth.get_user_id() asset['date_available'] = "CURRENT_TIMESTAMP()" op = OpAsset(asset) ret.append(op.update()) if ret[-1] == False: break if asset['published_flag']: acl.set_published_asset_acl(asset, True) else: acl.set_published_asset_acl(asset, False) if False in ret: return {'ret': "error"} return {'ret': "ok"}
def del_asset(): """Delete asset""" # Target asset asset_id = CTK.request.url.split('/')[-1] try: asset_id = __check_asset_id(asset_id) asset = Asset(asset_id) if not asset['id']: return CTK.HTTP_Redir('/') except: return CTK.HTTP_Redir('/') # Authentication fail_admin = Auth.assert_is_role(Role.ROLE_ADMIN) user_id = Auth.get_user_id() is_creator = (asset['creator_id'] == user_id) if fail_admin and not is_creator: return fail_admin op = OpAsset(asset) result = op.delete() if result['ret'] == False: return default() if result['type'] == 'total': return CTK.HTTP_Redir(LOCATION) return default(ERROR_IN_USE)
def create_assets (asset, filenames): """Create the assets immediately. Final info is added via callback once the task exits the processing queue.""" ret = [] if not filenames: filenames = [None] for filename in filenames: if filename: queue_flag = id(asset) asset._file = Upload.get_info (filename) op = OpAsset (asset) rc = op.add() ret.append(rc) return ret
def create_assets(asset, filenames): """Create the assets immediately. Final info is added via callback once the task exits the processing queue.""" ret = [] if not filenames: filenames = [None] for filename in filenames: if filename: queue_flag = id(asset) asset._file = Upload.get_info(filename) op = OpAsset(asset) rc = op.add() ret.append(rc) return ret
def transcode_apply(): # Authentication fail = Auth.assert_is_role(Role.ROLE_PUBLISHER) if fail: return fail asset_id = CTK.post.pop('asset_id') asset = Asset(asset_id) op = OpAsset(asset) rets = [] for key in CTK.post: if not key.startswith('target_id_'): continue val = int(CTK.post[key]) if val: target_id = key[len('target_id_'):] ret = op.transcode(target_id) rets.append(ret) if False in rets: return {'ret': "error"} return {'ret': "ok", 'redirect': '%s/publish' % PageAsset.LOCATION}
def transcode_apply (): # Authentication fail = Auth.assert_is_role (Role.ROLE_PUBLISHER) if fail: return fail asset_id = CTK.post.pop('asset_id') asset = Asset(asset_id) op = OpAsset(asset) rets = [] for key in CTK.post: if not key.startswith('target_id_'): continue val = int(CTK.post[key]) if val: target_id = key[len('target_id_'):] ret = op.transcode(target_id) rets.append(ret) if False in rets: return {'ret': "error"} return {'ret': "ok", 'redirect': '%s/publish' % PageAsset.LOCATION}
def edit_asset (): # Authentication fail = Auth.assert_is_role (Role.ROLE_EDITOR) if fail: return fail asset_id = CTK.request.url.split('/')[-1] acl = ACL() editable = acl.filter_assets ("ed" , [asset_id]) if not int(asset_id) in editable: return CTK.HTTP_Error(401) asset = Asset(asset_id) q = "SELECT * "\ "FROM assets WHERE id = '%(asset_id)s';" %(locals()) # Prevent simultaneous editions editor_id = int(Auth.get_user_id()) try: strp = time.strptime(asset['date_modified'], '%Y-%m-%d %H:%M:%S') edit_ts = int(time.mktime(strp)) except (ValueError, TypeError): edit_ts = 0 # Edition during edition-window is only allowed to the current editor. if time.time() - edit_ts < EDIT_WINDOW and asset['editor_id'] != editor_id: page = Page.Default() page += CTK.RawHTML ("<h1>%s: Editar activo</h1>"%(MENU_LINK)) page += CTK.RawHTML ("<h2>El activo está siendo editado actualmente</h2>") return page.Render() # Mark it as 'being edited' asset['date_modified'] = "CURRENT_TIMESTAMP()" asset['editor_id'] = editor_id oa = OpAsset(asset) rc = oa.update() asset = Asset(asset_id) lang = asset['language'] languages= CTK.Combobox({'name':'language', 'selected': lang, 'class':'required'}, LANG) # Table types = ComboboxSQL (props={'selected':asset['asset_types_id'], 'class':'required'}, sql="SELECT id, type FROM asset_types;") licenses = ComboboxSQL (props={'selected':asset['licenses_id'], 'class':'required'}, sql="SELECT id, name FROM licenses;") table = PropsAutoSQL (False, q) table.AddConstant ('asset_id',str(asset_id)) table.Add ('Tipo', types, 'asset_types_id', 'Tipo de activo') table.Add ('Licencia', licenses, 'licenses_id', 'Licencia del activo') table.Add ('Título', CTK.TextField({'class':'required', 'maxlength': LEN_TITL}), 'title', 'Titulo del activo') table.Add ('Descripción', CTK.TextField({'class':'required', 'maxlength': LEN_DESC}), 'description', 'Descripcion del activo') table.Add ('Versión', CTK.TextField({'class':'required'}), 'version', 'Version del activo') table.Add ('Idioma', languages, 'language', 'Idioma del activo') table.Add ('Tema', CTK.TextField({'class':'required', 'maxlength': LEN_SUBJ}), 'subject', 'El tema del contenido del recurso') form = CTK.Submitter('%s/edit/apply'%LOCATION) form += table form += CTK.SubmitterButton('Enviar') page = Page.Default() page += CTK.RawHTML ("<h1>%s: Editar activo</h1>"%(MENU_LINK)) page += form return page.Render()
attachment = File.clone_file (asset._file) asset._file = attachment except IOError,e: # If file copying is not possible, report and abort msg = 'File duplication could not be performed while editing asset ID %s.' %(asset_id) msg += '\n%s\n' %(str(e)) print msg return {'ret':"error"} # Add replacement info if not asset._parent_id: asset._parent_id = asset['id'] asset._replacements['replaces'].append(asset['id']) # Add asset to database op = OpAsset (asset) ret = op.add() if ret == True: # Update base asset asset = Asset(asset_id) asset['date_modified'] = None # unmark edition asset['edited_flag'] = 1 # mark as editted op = OpAsset (asset) ret = op.update() return {'ret': "ok", 'redirect': LOCATION} else: return {'ret': "error"}
def edit_asset(): # Authentication fail = Auth.assert_is_role(Role.ROLE_EDITOR) if fail: return fail asset_id = CTK.request.url.split('/')[-1] acl = ACL() editable = acl.filter_assets("ed", [asset_id]) if not int(asset_id) in editable: return CTK.HTTP_Error(401) asset = Asset(asset_id) q = "SELECT * "\ "FROM assets WHERE id = '%(asset_id)s';" %(locals()) # Prevent simultaneous editions editor_id = int(Auth.get_user_id()) try: strp = time.strptime(asset['date_modified'], '%Y-%m-%d %H:%M:%S') edit_ts = int(time.mktime(strp)) except (ValueError, TypeError): edit_ts = 0 # Edition during edition-window is only allowed to the current editor. if time.time() - edit_ts < EDIT_WINDOW and asset['editor_id'] != editor_id: page = Page.Default() page += CTK.RawHTML("<h1>%s: Editar activo</h1>" % (MENU_LINK)) page += CTK.RawHTML( "<h2>El activo está siendo editado actualmente</h2>") return page.Render() # Mark it as 'being edited' asset['date_modified'] = "CURRENT_TIMESTAMP()" asset['editor_id'] = editor_id oa = OpAsset(asset) rc = oa.update() asset = Asset(asset_id) lang = asset['language'] languages = CTK.Combobox( { 'name': 'language', 'selected': lang, 'class': 'required' }, LANG) # Table types = ComboboxSQL(props={ 'selected': asset['asset_types_id'], 'class': 'required' }, sql="SELECT id, type FROM asset_types;") licenses = ComboboxSQL(props={ 'selected': asset['licenses_id'], 'class': 'required' }, sql="SELECT id, name FROM licenses;") table = PropsAutoSQL(False, q) table.AddConstant('asset_id', str(asset_id)) table.Add('Tipo', types, 'asset_types_id', 'Tipo de activo') table.Add('Licencia', licenses, 'licenses_id', 'Licencia del activo') table.Add('Título', CTK.TextField({ 'class': 'required', 'maxlength': LEN_TITL }), 'title', 'Titulo del activo') table.Add('Descripción', CTK.TextField({ 'class': 'required', 'maxlength': LEN_DESC }), 'description', 'Descripcion del activo') table.Add('Versión', CTK.TextField({'class': 'required'}), 'version', 'Version del activo') table.Add('Idioma', languages, 'language', 'Idioma del activo') table.Add('Tema', CTK.TextField({ 'class': 'required', 'maxlength': LEN_SUBJ }), 'subject', 'El tema del contenido del recurso') form = CTK.Submitter('%s/edit/apply' % LOCATION) form += table form += CTK.SubmitterButton('Enviar') page = Page.Default() page += CTK.RawHTML("<h1>%s: Editar activo</h1>" % (MENU_LINK)) page += form return page.Render()
asset._file = attachment except IOError, e: # If file copying is not possible, report and abort msg = 'File duplication could not be performed while editing asset ID %s.' % ( asset_id) msg += '\n%s\n' % (str(e)) print msg return {'ret': "error"} # Add replacement info if not asset._parent_id: asset._parent_id = asset['id'] asset._replacements['replaces'].append(asset['id']) # Add asset to database op = OpAsset(asset) ret = op.add() if ret == True: # Update base asset asset = Asset(asset_id) asset['date_modified'] = None # unmark edition asset['edited_flag'] = 1 # mark as editted op = OpAsset(asset) ret = op.update() return {'ret': "ok", 'redirect': LOCATION} else: return {'ret': "error"}