def remove_tasks(self): """removes the user from task """ task_ids = self.get_multi_integer(self.request, 'task_id') as_param = self.request.params.get('as', None) from stalker import Task # get the tasks that the user is a resource only task_filter = Task.query\ .filter(Task.id.in_(task_ids)) if as_param is None or as_param == 'resource': tasks = task_filter\ .filter(Task.resources.contains(self.entity))\ .all() for task in tasks: task.resources.remove(self.entity) elif as_param == 'responsible': tasks = task_filter\ .filter(Task.responsible.contains(self.entity))\ .all() for task in tasks: task.responsible.remove(self.entity) elif as_param == 'watcher': tasks = task_filter\ .filter(Task.watchers.contains(self.entity))\ .all() for task in tasks: task.watchers.remove(self.entity) from stalker.db.session import DBSession DBSession.flush()
def create_entity_statuses(entity_type="", status_names=None, status_codes=None, user=None): """creates the default task statuses """ if not entity_type: raise ValueError("Please supply entity_type") if not status_names: raise ValueError("Please supply status names") if not status_codes: raise ValueError("Please supply status codes") # create statuses for entity from stalker import Status, StatusList logger.debug("Creating %s Statuses" % entity_type) statuses = Status.query.filter(Status.name.in_(status_names)).all() status_names_in_db = map(lambda x: x.name, statuses) for name, code in zip(status_names, status_codes): if name not in status_names_in_db: logger.debug("Creating Status: %s (%s)" % (name, code)) new_status = Status(name=name, code=code, created_by=user, updated_by=user) statuses.append(new_status) DBSession.add(new_status) # create the Status List status_list = StatusList.query.filter(StatusList.target_entity_type == entity_type).first() if status_list is None: logger.debug("No %s Status List found, creating new!" % entity_type) status_list = StatusList( name="%s Statuses" % entity_type, target_entity_type=entity_type, created_by=user, updated_by=user ) else: logger.debug("%s Status List already created, updating statuses" % entity_type) status_list.statuses = statuses DBSession.add(status_list) try: DBSession.commit() except IntegrityError as e: logger.debug("error in DBSession.commit, rolling back: %s" % e) DBSession.rollback() else: logger.debug("Created %s Statuses successfully" % entity_type) DBSession.flush()
def remove_users(self): """removes users from Department.users attribute """ # get user ids user_ids = self.get_multi_integer(self.request, 'user_id') from stalker import User users = User.query.filter(User.id.in_(user_ids)).all() from stalker.db.session import DBSession with DBSession.no_autoflush: for user in users: try: self.entity.users.remove(user) except ValueError: pass DBSession.flush()
def update_projects(self): """updates the user projects """ # get projects project_ids = self.get_multi_integer(self.request, 'project_id[]') from stalker import Project all_projects = Project.query \ .filter(Project.id.in_(project_ids)).all() if self.request.method == 'POST': self.entity.projects = all_projects elif self.request.method == 'PATCH': self.entity.projects += all_projects from stalker.db.session import DBSession DBSession.add(self.entity) DBSession.flush()
def update_groups(self): """updates the user groups """ # get groups group_ids = self.get_multi_integer(self.request, 'group_id[]') from stalker import Group all_groups = Group.query \ .filter(Group.id.in_(group_ids)).all() if self.request.method == 'POST': self.entity.groups = all_groups elif self.request.method == 'PATCH': self.entity.groups += all_groups from stalker.db.session import DBSession DBSession.add(self.entity) DBSession.flush()
def update_departments(self): """updates the user departments """ # get departments dep_ids = self.get_multi_integer(self.request, 'dep_id[]') from stalker.db.session import DBSession from stalker import Department with DBSession.no_autoflush: all_deps = Department.query\ .filter(Department.id.in_(dep_ids)).all() if self.request.method == 'POST': with DBSession.no_autoflush: self.entity.departments = all_deps elif self.request.method == 'PATCH': with DBSession.no_autoflush: self.entity.departments += all_deps DBSession.add(self.entity) DBSession.flush()
def entity_creator(self, entity_class, param_resolution): """Creates SOM class instances by using param_resolution. :param entity_class: A SOM Class :param param_resolution: A list of dictionaries. See :method:``.resolve_param_resolution`` for details. """ args = self.resolve_param_resolution(param_resolution) # fix created_by value if skipped # and use the logged in user as the creator if 'created_by' not in args: logged_in_user = self.get_logged_in_user(self.request) args['created_by'] = logged_in_user from stalker.db.session import DBSession new_entity = entity_class(**args) DBSession.add(new_entity) DBSession.flush() transaction.commit() return new_entity
resource = kwargs['resources'][0] print('creating %s TimeLogs' % tl_count) for i in xrange(tl_count): end = start + ten_minutes tl = TimeLog( resource=resource, task=new_task, start=start, end=end, ) DBSession.add(tl) start = end if i % 1000: print('i: %s' % i) DBSession.flush() DBSession.commit() benchmark_end = time.time() print('data created in: %s secs' % (benchmark_end - benchmark_start)) task_id = new_task.id # del all the TimeLogs del new_task.time_logs del new_task # now get back the task from db task_from_db = Task.query.get(task_id) # now query the total_logged_seconds benchmark_start = time.time() total_logged_seconds = task_from_db.total_logged_seconds
def create_entity_statuses(entity_type='', status_names=None, status_codes=None, user=None): """creates the default task statuses """ if not entity_type: raise ValueError('Please supply entity_type') if not status_names: raise ValueError('Please supply status names') if not status_codes: raise ValueError('Please supply status codes') # create statuses for entity from stalker import Status, StatusList logger.debug("Creating %s Statuses" % entity_type) statuses = Status.query.filter(Status.name.in_(status_names)).all() logger.debug('status_names: %s' % status_names) logger.debug('statuses: %s' % statuses) status_names_in_db = list(map(lambda x: x.name, statuses)) logger.debug('statuses_names_in_db: %s' % status_names_in_db) for name, code in zip(status_names, status_codes): if name not in status_names_in_db: logger.debug('Creating Status: %s (%s)' % (name, code)) new_status = Status( name=name, code=code, created_by=user, updated_by=user ) statuses.append(new_status) DBSession.add(new_status) else: logger.debug( 'Status %s (%s) is already created skipping!' % (name, code) ) # create the Status List status_list = StatusList.query\ .filter(StatusList.target_entity_type == entity_type)\ .first() if status_list is None: logger.debug('No %s Status List found, creating new!' % entity_type) status_list = StatusList( name='%s Statuses' % entity_type, target_entity_type=entity_type, created_by=user, updated_by=user ) else: logger.debug("%s Status List already created, updating statuses" % entity_type) status_list.statuses = statuses DBSession.add(status_list) try: DBSession.commit() except IntegrityError as e: logger.debug("error in DBSession.commit, rolling back: %s" % e) DBSession.rollback() else: logger.debug("Created %s Statuses successfully" % entity_type) DBSession.flush()
def delete_entity(self): """deletes one note """ from stalker.db.session import DBSession DBSession.delete(self.entity) DBSession.flush()