def DraftPublishEventHandler(session, msg): elements = msg['content'].split('\t') if len(elements) != 6: logging.warning("got bad message: %s", elements) return record = dict() record["timestamp"] = datetime.datetime.utcfromtimestamp(msg['ctime']) record["op_type"] = elements[0] record["obj_type"] = elements[1] record["repo_id"] = elements[2] repo = seafile_api.get_repo(elements[2]) record["repo_name"] = repo.name if repo else '' record["op_user"] = elements[3] record["path"] = elements[4] record["old_path"] = elements[5] users = [] org_id = get_org_id_by_repo_id(elements[2]) if org_id > 0: users.extend(seafile_api.org_get_shared_users_by_repo(org_id, elements[2])) owner = seafile_api.get_org_repo_owner(elements[2]) else: users.extend(seafile_api.get_shared_users_by_repo(elements[2])) owner = seafile_api.get_repo_owner(elements[2]) if owner not in users: users = users + [owner] if not users: return record["related_users"] = users save_user_activity(session, record)
def test_save_user_events(self): session = self.get_session() rows = session.query(Activity).all() self.assertEqual(len(rows), 0) session.close() session = self.get_session() save_user_activity(session, self.record) session.close() session = self.get_session() rows = session.query(Activity).all() self.assertEqual(len(rows), 1) session.close()
def save_repo_rename_activity(session, commit, repo_id, parent, org_id, related_users, time): repo = seafile_api.get_repo(repo_id) record = { 'op_type': 'rename', 'obj_type': 'repo', 'timestamp': time, 'repo_id': repo_id, 'repo_name': repo.repo_name, 'path': '/', 'op_user': commit.creator_name, 'related_users': related_users, 'commit_id': commit.commit_id, 'old_repo_name': parent.repo_name } save_user_activity(session, record)
def test_get_user_activites(self): session = self.get_session() rows, total_count = get_user_activities(session, '*****@*****.**', 0, 2) self.assertEqual(len(rows), 0) session.close() session = self.get_session() save_user_activity(session, self.record) session.close() session = self.get_session() rows, total_count = get_user_activities(session, '*****@*****.**', 0, 2) self.assertEqual(len(rows), 1) session.close()
def test_save_invalid_events(self): session = self.get_session() rows = session.query(Activity).all() self.assertEqual(len(rows), 0) session.close() session = self.get_session() record = copy.copy(self.record) del (record['related_users']) save_user_activity(session, record) session.close() session = self.get_session() rows = session.query(Activity).all() self.assertEqual(len(rows), 0) session.close()
def test_save_extra_event(self): session = self.get_session() rows = session.query(Activity).all() self.assertEqual(len(rows), 0) session.close() session = self.get_session() record = copy.copy(self.record) record['op_type'] = 'clear-up-trash' record['days'] = 0 save_user_activity(session, self.record) session.close() session = self.get_session() rows = session.query(Activity).all() self.assertEqual(len(rows), 1) session.close()
def save_user_activities(session, records): # If a file was edited many times by same user in 30 minutes, just update timestamp. if len(records) == 1 and records[0]['op_type'] == 'edit': record = records[0] _timestamp = record['timestamp'] - timedelta(minutes=30) q = session.query(Activity) q = q.filter(Activity.repo_id==record['repo_id'], Activity.op_type==record['op_type'], Activity.op_user==record['op_user'], Activity.path==record['path'], Activity.timestamp > _timestamp) row = q.first() if row: activity_id = row.id update_user_activity_timestamp(session, activity_id, record) else: save_user_activity(session, record) else: for record in records: save_user_activity(session, record)
def test_get_user_activities_by_page(self): session = self.get_session() rows, total_count = get_user_activities(session, '*****@*****.**', 0, 2) self.assertEqual(len(rows), 0) session.close() session = self.get_session() save_user_activity(session, self.record) session.close() session = self.get_session() record = copy.copy(self.record) record['op_type'] = 'delete' time.sleep(1) record['timestamp'] = datetime.datetime.utcnow() save_user_activity(session, record) session.close() session = self.get_session() record = copy.copy(self.record) record['op_type'] = 'recover' time.sleep(1) record['timestamp'] = datetime.datetime.utcnow() save_user_activity(session, record) session.close() session = self.get_session() rows, total_count = get_user_activities(session, '*****@*****.**', 0, 3) self.assertEqual(len(rows), 3) self.assertEqual(rows[0].op_type, 'recover') session.close() session = self.get_session() rows, total_count = get_user_activities(session, '*****@*****.**', 0, 2) self.assertEqual(len(rows), 2) session.close() session = self.get_session() rows, total_count = get_user_activities(session, '*****@*****.**', 1, 1) self.assertEqual(len(rows), 1) session.close()