def getColumns(cls, categoryID): session = Session() categoryIndex = CommandCategories.getIndexFromCategoryID(categoryID) category = session.query(CommandCategories).filter( CommandCategories.i == categoryIndex ).first() out = [] for i in range(1, 5): name = u'column' + unicode(i) out.append({ 'label':getattr(category, name) if category else (u'Column ' + unicode(i)), 'commands':[] }) session.close() if not category: return out for cmd in cls.getCommands(categoryID): out[cmd['column']]['commands'].append(cmd) return out
def getCommandData(cls, commandID): session = Session() commandIndex = UserCommands.getIndexFromCommandID(commandID) result = session.query(UserCommands).filter(UserCommands.i == commandIndex).first() if result is None: session.close() return {'error':'No Such Command'} out = result.toDict() session.close() return out
def variants(self): from alloy.data.model import Session from alloy.data.model.CommandVariants import CommandVariants session = Session() result = session.query(CommandVariants).filter(CommandVariants.usrcmdfk == self.i).all() out = [] for r in result: out.append(r.toButtonDict()) session.close() return out
def deleteVariant(cls, variantID): session = Session() variantIndex = CommandVariants.getIndexFromVariantID(variantID) result = session.query(CommandVariants).filter(CommandVariants.i == variantIndex).first() if result is None: session.close() return {'id':variantID} session.delete(result) session.commit() session.close() return {'id':variantID}
def getCategories(cls): session = Session() result = session.query(CommandCategories).all() if not result: category = CommandCategories() session.add(category) session.commit() out = [category.toDict()] else: out = [] for r in result: out.append(r.toDict()) session.close() return out
def reorderVariants(cls, variantIDs): session = Session() index = 0 out = [] for varID in variantIDs: varIndex = CommandVariants.getIndexFromVariantID(varID) variant = session.query(CommandVariants).filter(CommandVariants.i == varIndex).first() if not variant: continue variant.row = index out.append(variant.variantID) index += 1 session.commit() session.close() return out
def modifyCategory(cls, categoryID, **kwargs): session = Session() result = session.query(CommandCategories).filter( CommandCategories.i == CommandCategories.getIndexFromCategoryID(categoryID) ).first() if result is None: return {'error':'No Such Category'} for n,v in kwargs.iteritems(): if isinstance(v, str): v = unicode(v) setattr(result, n, v) session.commit() out = result.toDict() session.close() return out
def getCommands(cls, categoryID): session = Session() categoryIndex = CommandCategories.getIndexFromCategoryID(categoryID) result = session.query(UserCommands) \ .filter(UserCommands.categoryfk == categoryIndex) \ .order_by(UserCommands.row.asc()) \ .all() if not result: session.close() return [] out = [] for r in result: out.append(r.toButtonDict()) session.close() return out
def reorderColumn(cls, column, commandIDs): session = Session() index = 0 out = [] for cmdID in commandIDs: cmdIndex = UserCommands.getIndexFromCommandID(cmdID) cmd = session.query(UserCommands).filter(UserCommands.i == cmdIndex).first() if not cmd: continue cmd.column = column cmd.row = index out.append(cmd.commandID) index += 1 session.commit() session.close() return out
def modifyVariant(cls, variantID, **kwargs): session = Session() variantIndex = CommandVariants.getIndexFromVariantID(variantID) result = session.query(CommandVariants).filter(CommandVariants.i == variantIndex).first() if result is None: session.close() return {'error':'No Such Variant'} for n,v in kwargs.iteritems(): if isinstance(v, str): v = unicode(v) setattr(result, n, v) session.commit() out = result.toButtonDict() session.close() return out
def createCategory(cls, label): """ Creates a new category with the specified label and returns the dictionary representation of the newly created category, including its id. @@@param label:string The display label for the new category. @@@return dict The dictionary representation of the newly created category. """ session = Session() category = CommandCategories(label=unicode(label)) session.add(category) session.commit() out = category.toDict() session.close() return out
def deleteCommand(cls, commandID): session = Session() commandIndex = UserCommands.getIndexFromCommandID(commandID) result = session.query(UserCommands).filter(UserCommands.i == commandIndex).first() if result is None: session.close() return {'id':commandID} session.delete(result) result = session.query(CommandVariants).filter(CommandVariants.usrcmdfk == commandIndex).all() for r in result: session.delete(result) session.commit() session.close() return {'id':commandID}
def modifyCommand(cls, commandID, **kwargs): session = Session() commandIndex = UserCommands.getIndexFromCommandID(commandID) result = session.query(UserCommands).filter(UserCommands.i == commandIndex).first() if result is None: session.close() return {'error':'No Such Command'} for n,v in kwargs.iteritems(): if isinstance(v, str): v = unicode(v) setattr(result, n, v) session.commit() out = result.toButtonDict() session.close() return out
def deleteCategory(cls, categoryID): session = Session() categoryIndex = CommandCategories.getIndexFromCategoryID(categoryID) result = session.query(CommandCategories).filter(CommandCategories.i == categoryIndex).first() if result: session.delete(result) result = session.query(UserCommands).filter(UserCommands.categoryfk == categoryIndex).all() for r in result: session.delete(r) variants = session.query(CommandVariants).filter(CommandVariants.usrcmdfk == r.i).all() for v in variants: session.delete(v) session.commit() session.close() return {'id':categoryID}
def createVariant(cls, commandID, row =None, **kwargs): session = Session() commandIndex = UserCommands.getIndexFromCommandID(commandID) if row is None or row < 0: row = session.query(CommandVariants).filter( CommandVariants.usrcmdfk == commandIndex ).count() for n,v in kwargs.iteritems(): if isinstance(v, str): kwargs[n] = unicode(v) variant = CommandVariants(usrcmdfk=commandIndex, row=row, **kwargs) session.add(variant) session.commit() out = variant.toButtonDict() session.close() return out
def createCommand(cls, categoryID, column, row =None, **kwargs): session = Session() categoryIndex = CommandCategories.getIndexFromCategoryID(categoryID) if row is None or row < 0: row = session.query(UserCommands).filter( and_( UserCommands.categoryfk == categoryIndex, UserCommands.column == column ) ).count() for n,v in kwargs.iteritems(): if isinstance(v, str): kwargs[n] = unicode(v) command = UserCommands(categoryfk=categoryIndex, column=column, row=row, **kwargs) session.add(command) session.commit() out = command.toButtonDict() session.close() return out