def create_group(self, name): try: group = Group(name=name, item_order=[]) session.flush() except DuplicateKeyError: raise Exception('--- That group already exists') return group
def remove_value_on_day(self, name, date): next_day = date + timedelta(days=1) print('between', date, next_day) resp = Item.query.update( {'name': name}, {'$pull': {'values': {'elemMatch': {'timestamp': {'$gte': date, '$lte': next_day}}}}}, multi=True ) print(resp) session.flush()
def remove_item(self, name): item = Item.query.get(name=name) if item: Group.query.update( {'item_order': {'$elemMatch': {'$in': [item.name]}}}, {'$pull': {'item_order': {'$in': [item.name]}}}, multi=True) item.delete() session.flush() return True else: return False
def remove_last_addition(self): if hasattr(self, 'last_action') and self.last_action: if self.last_action['func'].__name__ == self.add_items.__name__: items_values = self.last_action['args'][0] for name, value in items_values: item = Item.query.get(name=name) item.values.pop() if self.last_action['func'].__name__ == self.add_item.__name__: name, values = self.last_action['args'][0], self.last_action['args'][1:] item = Item.query.get(name=name) for val in values: item.values.pop() session.flush()
def remove_last_addition(self): if hasattr(self, 'last_action') and self.last_action: if self.last_action['func'].__name__ == self.add_items.__name__: items_values = self.last_action['args'][0] for name, value in items_values: item = Item.query.get(name=name) item.values.pop() if self.last_action['func'].__name__ == self.add_item.__name__: name, values = self.last_action['args'][0], self.last_action[ 'args'][1:] item = Item.query.get(name=name) for val in values: item.values.pop() session.flush()
def remove_group(self, name): session.clear() group = Group.query.get(name=name) if not group: return if group: Item.query.update( {'groups': {'$elemMatch': {'$in': [group.name]}}}, {'$pull': {'groups': {'$in': [group.name]}}}, multi=True) group.delete() session.flush() return True else: return False
def set_group_item_order(self, name, *args): group = Group.query.get(name=name) if not group: return False group.item_order = [] for arg in args: group.item_order.append(arg) # verify item is also registered with group item = Item.query.get(arg) if group.name not in item.groups: item.groups.append(group.name) session.flush() return True
def link_item(self, name, *args): item = Item.query.get(name=name) if not item: item = Item(name=name, values=[], groups=[]) if args: for arg in args: group = Group.query.get(name=arg) if not group: Group(name=arg, item_order=[item.name]) else: group.item_order.append(item.name) item.groups.append(arg) session.flush() return item.groups
def remove_value_on_day(self, name, date): next_day = date + timedelta(days=1) print('between', date, next_day) resp = Item.query.update({'name': name}, { '$pull': { 'values': { 'elemMatch': { 'timestamp': { '$gte': date, '$lte': next_day } } } } }, multi=True) print(resp) session.flush()
def set_default_group(self, group=None): if not group: self.current_group = Config.query.get(name='default_group') if not self.current_group: self.current_group = Config(name='default_group', value='---') session.flush() self.current_group = self.current_group.value else: new_group = Config.query.get(name='default_group') if not new_group: new_group = Config(name='default_group', value='---') new_group.value = group if new_group: self.current_group = new_group.value session.flush() return self.current_group
def add_item(self, name, *values): date = self.date or None # TODO: fuzzy logic search to avoid creating weird new items item = Item.query.get(name=name) if not item: item = Item(name=name, values=[], groups=[]) if not date: date = datetime.now() if values: for arg in values: print(' adding ', arg, ' to ', item.name) item.values.append({'timestamp': date, 'value': arg}) session.flush() return item
def remove_item(self, name): item = Item.query.get(name=name) if item: Group.query.update( {'item_order': { '$elemMatch': { '$in': [item.name] } }}, {'$pull': { 'item_order': { '$in': [item.name] } }}, multi=True) item.delete() session.flush() return True else: return False
def remove_group(self, name): session.clear() group = Group.query.get(name=name) if not group: return if group: Item.query.update( {'groups': { '$elemMatch': { '$in': [group.name] } }}, {'$pull': { 'groups': { '$in': [group.name] } }}, multi=True) group.delete() session.flush() return True else: return False