def remove_old_events(days_old=90): """ Remove events older than *days_old*. """ limit_date = datetime.datetime.now() - datetime.timedelta(days=90) ApiEvent.query.filter(ApiEvent.created_at < limit_date).delete() ApiEvent.commit()
def test_get_last_events(self): asset = assets_service.create_asset(self.project.id, self.asset_type.id, "test 1", "", {}) after = asset["created_at"] time.sleep(1) asset = assets_service.create_asset(self.project.id, self.asset_type.id, "test 2", "", {}) time.sleep(1) asset = assets_service.create_asset(self.project.id, self.asset_type.id, "test 3", "", {}) before = asset["created_at"] time.sleep(1) asset = assets_service.create_asset(self.project.id, self.asset_type.id, "test 4", "", {}) events = self.get("/data/events/last") self.assertEqual(len(events), 4) events = self.get("/data/events/last?page_size=2") self.assertEqual(len(events), 2) events = self.get("/data/events/last?before=%s" % before) self.assertEqual(len(events), 2) events = self.get("/data/events/last?before=%s&after=%s" % (before, after)) self.assertEqual(len(events), 2) ApiEvent.create(name="preview-file:add-file") ApiEvent.create(name="person:set-thumbnail") events = self.get("/data/events/last") self.assertEqual(len(events), 6) events = self.get("/data/events/last?only_files=true") self.assertEqual(len(events), 2)
def remove_person(person_id, force=True): person = Person.get(person_id) if force: for comment in Comment.get_all_by(person_id=person_id): remove_comment(comment.id) comments = Comment.query.filter( Comment.acknowledgements.contains(person) ) for comment in comments: comment.acknowledgements = [ member for member in comment.acknowledgements if str(member.id) != person_id ] comment.save() ApiEvent.delete_all_by(user_id=person_id) Notification.delete_all_by(person_id=person_id) SearchFilter.delete_all_by(person_id=person_id) DesktopLoginLog.delete_all_by(person_id=person_id) LoginLog.delete_all_by(person_id=person_id) Subscription.delete_all_by(person_id=person_id) TimeSpent.delete_all_by(person_id=person_id) for project in Project.query.filter(Project.team.contains(person)): project.team = [ member for member in project.team if str(member.id) != person_id ] project.save() for task in Task.query.filter(Task.assignees.contains(person)): task.assignees = [ assignee for assignee in task.assignees if str(assignee.id) != person_id ] task.save() for task in Task.get_all_by(assigner_id=person_id): task.update({"assigner_id": None}) for output_file in OutputFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) for working_file in WorkingFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) for task in WorkingFile.get_all_by(person_id=person_id): output_file.update({"person_id": None}) try: person.delete() events.emit("person:delete", {"person_id": person.id}) except IntegrityError: raise ModelWithRelationsDeletionException( "Some data are still linked to given person." ) return person.serialize_safe()
def save_event(event, data): """ Store event information in the database. """ try: from zou.app.services.persons_service import get_current_user_raw person = get_current_user_raw() person_id = person.id except: person_id = None return ApiEvent.create(name=event, data=data, user_id=person_id)
def remove_project(project_id): from zou.app.services import playlists_service tasks = Task.query.filter_by(project_id=project_id) for task in tasks: remove_task(task.id, force=True) query = EntityLink.query.join(Entity, EntityLink.entity_in_id == Entity.id).filter( Entity.project_id == project_id) for link in query: link.delete_no_commit() EntityLink.commit() query = EntityVersion.query.join( Entity, EntityVersion.entity_id == Entity.id).filter( Entity.project_id == project_id) for version in query: version.delete_no_commit() EntityLink.commit() playlists = Playlist.query.filter_by(project_id=project_id) for playlist in playlists: playlists_service.remove_playlist(playlist.id) ApiEvent.delete_all_by(project_id=project_id) Entity.delete_all_by(project_id=project_id) MetadataDescriptor.delete_all_by(project_id=project_id) Milestone.delete_all_by(project_id=project_id) ScheduleItem.delete_all_by(project_id=project_id) SearchFilter.delete_all_by(project_id=project_id) for news in News.query.join(Task).filter_by(project_id=project_id).all(): news.delete_no_commit() News.commit() project = Project.get(project_id) project.delete() events.emit("project:delete", {"project_id": project.id}) return project_id