def test_update_membership_timeline(): user_1 = factories.UserFactory.create() user_2 = factories.UserFactory.create() membership = factories.MembershipFactory.create(user=user_1) membership.user = user_2 membership.save() project_timeline = service.get_project_timeline(membership.project) user_1_timeline = service.get_user_timeline(user_1) user_2_timeline = service.get_user_timeline(user_2) assert project_timeline[0].event_type == "projects.membership.delete" assert project_timeline[0].data["project"]["id"] == membership.project.id assert project_timeline[0].data["user"]["id"] == user_1.id assert project_timeline[0].data["user"]["name"] == user_1.get_full_name() assert project_timeline[1].event_type == "projects.membership.create" assert project_timeline[1].data["project"]["id"] == membership.project.id assert project_timeline[1].data["user"]["id"] == user_2.id assert project_timeline[1].data["user"]["name"] == user_2.get_full_name() assert user_1_timeline[0].event_type == "projects.membership.delete" assert user_1_timeline[0].data["project"]["id"] == membership.project.id assert user_1_timeline[0].data["user"]["id"] == user_1.id assert user_1_timeline[0].data["user"]["name"] == user_1.get_full_name() assert user_2_timeline[0].event_type == "projects.membership.create" assert user_2_timeline[0].data["project"]["id"] == membership.project.id assert user_2_timeline[0].data["user"]["id"] == user_2.id assert user_2_timeline[0].data["user"]["name"] == user_2.get_full_name()
def test_create_milestone_timeline(): milestone = factories.MilestoneFactory.create(name="test milestone timeline") history_services.take_snapshot(milestone, user=milestone.owner) milestone_timeline = service.get_project_timeline(milestone.project) assert milestone_timeline[0].event_type == "milestones.milestone.create" assert milestone_timeline[0].data["milestone"]["name"] == "test milestone timeline" assert milestone_timeline[0].data["user"]["id"] == milestone.owner.id
def test_update_user_story_timeline(): user_watcher = factories.UserFactory() user_story = factories.UserStoryFactory.create(subject="test us timeline") history_services.take_snapshot(user_story, user=user_story.owner) user_story.add_watcher(user_watcher) user_story.subject = "test us timeline updated" user_story.save() history_services.take_snapshot(user_story, user=user_story.owner) project_timeline = service.get_project_timeline(user_story.project) assert project_timeline[0].event_type == "userstories.userstory.change" assert project_timeline[0].data["userstory"][ "subject"] == "test us timeline updated" assert project_timeline[0].data["values_diff"]["subject"][ 0] == "test us timeline" assert project_timeline[0].data["values_diff"]["subject"][ 1] == "test us timeline updated" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[ 0].event_type == "userstories.userstory.change" assert user_watcher_timeline[0].data["userstory"][ "subject"] == "test us timeline updated" assert user_watcher_timeline[0].data["values_diff"]["subject"][ 0] == "test us timeline" assert user_watcher_timeline[0].data["values_diff"]["subject"][ 1] == "test us timeline updated"
def test_update_milestone_timeline(): user_watcher = factories.UserFactory() milestone = factories.MilestoneFactory.create( name="test milestone timeline") history_services.take_snapshot(milestone, user=milestone.owner) milestone.add_watcher(user_watcher) milestone.name = "test milestone timeline updated" milestone.save() history_services.take_snapshot(milestone, user=milestone.owner) project_timeline = service.get_project_timeline(milestone.project) assert project_timeline[0].event_type == "milestones.milestone.change" assert project_timeline[0].data["milestone"][ "name"] == "test milestone timeline updated" assert project_timeline[0].data["values_diff"]["name"][ 0] == "test milestone timeline" assert project_timeline[0].data["values_diff"]["name"][ 1] == "test milestone timeline updated" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "milestones.milestone.change" assert user_watcher_timeline[0].data["milestone"][ "name"] == "test milestone timeline updated" assert user_watcher_timeline[0].data["values_diff"]["name"][ 0] == "test milestone timeline" assert user_watcher_timeline[0].data["values_diff"]["name"][ 1] == "test milestone timeline updated"
def test_create_issue_timeline(): issue = factories.IssueFactory.create(subject="test issue timeline") history_services.take_snapshot(issue, user=issue.owner) project_timeline = service.get_project_timeline(issue.project) assert project_timeline[0].event_type == "issues.issue.create" assert project_timeline[0].data["issue"]["subject"] == "test issue timeline" assert project_timeline[0].data["user"]["id"] == issue.owner.id
def test_create_user_story_timeline(): user_story = factories.UserStoryFactory.create(subject="test us timeline") history_services.take_snapshot(user_story, user=user_story.owner) project_timeline = service.get_project_timeline(user_story.project) assert project_timeline[0].event_type == "userstories.userstory.create" assert project_timeline[0].data["userstory"]["subject"] == "test us timeline" assert project_timeline[0].data["user"]["id"] == user_story.owner.id
def _helper_get_timelines_for_accessing_users(project, users): """ Get the number of timeline entries (of 'epics.relateduserstory' type) that the accessing users are able to see, for both a given project's timeline and the user's timelines :param project: the project with the epic which contains the related user stories :param users: both the accessing users, and the users from which recover their (user) timelines :return: Dict with counters for 'epics.relateduserstory' entries for both the (project) and (users) timelines, according to the accessing users privileges """ timeline_counts = {'project_timelines': [], 'user_timelines': {}} # An anonymous user doesn't have a timeline to be recovered timeline_users = list(filter(lambda au: au != AnonymousUser(), users)) for accessing_user in users: project_timeline = service.get_project_timeline( project, accessing_user) project_timeline = project_timeline.exclude( event_type__in=["projects.membership.create"]) timeline_counts['project_timelines'].append(project_timeline.count()) timeline_counts['user_timelines'][users.index(accessing_user)] = [] for user in timeline_users: user_timeline = service.get_user_timeline(user, accessing_user) user_timeline = user_timeline.exclude(event_type__in=[ "users.user.create", "projects.membership.create" ]) timeline_counts['user_timelines'][users.index( accessing_user)].append(user_timeline.count()) return timeline_counts
def test_create_task_timeline(): task = factories.TaskFactory.create(subject="test task timeline") history_services.take_snapshot(task, user=task.owner) project_timeline = service.get_project_timeline(task.project) assert project_timeline[0].event_type == "tasks.task.create" assert project_timeline[0].data["task"]["subject"] == "test task timeline" assert project_timeline[0].data["user"]["id"] == task.owner.id
def test_update_project_timeline(): user_watcher = factories.UserFactory() project = factories.ProjectFactory.create(name="test project timeline") history_services.take_snapshot(project, user=project.owner) project.add_watcher(user_watcher) print("PPPP") project.name = "test project timeline updated" project.save() history_services.take_snapshot(project, user=project.owner) project_timeline = service.get_project_timeline(project) assert project_timeline[0].event_type == "projects.project.change" assert project_timeline[0].data["project"][ "name"] == "test project timeline updated" assert project_timeline[0].data["values_diff"]["name"][ 0] == "test project timeline" assert project_timeline[0].data["values_diff"]["name"][ 1] == "test project timeline updated" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "projects.project.change" assert user_watcher_timeline[0].data["project"][ "name"] == "test project timeline updated" assert user_watcher_timeline[0].data["values_diff"]["name"][ 0] == "test project timeline" assert user_watcher_timeline[0].data["values_diff"]["name"][ 1] == "test project timeline updated"
def test_create_wiki_page_timeline(): page = factories.WikiPageFactory.create(slug="test wiki page timeline") history_services.take_snapshot(page, user=page.owner) project_timeline = service.get_project_timeline(page.project) assert project_timeline[0].event_type == "wiki.wikipage.create" assert project_timeline[0].data["wikipage"]["slug"] == "test wiki page timeline" assert project_timeline[0].data["user"]["id"] == page.owner.id
def test_create_project_timeline(): project = factories.ProjectFactory.create(name="test project timeline") history_services.take_snapshot(project, user=project.owner) project_timeline = service.get_project_timeline(project) assert project_timeline[0].event_type == "projects.project.create" assert project_timeline[0].data["project"]["name"] == "test project timeline" assert project_timeline[0].data["user"]["id"] == project.owner.id
def test_user_data_for_system_users(): user_story = factories.UserStoryFactory.create(subject="test us timeline") user_story.owner.is_system = True user_story.owner.save() history_services.take_snapshot(user_story, user=user_story.owner) project_timeline = service.get_project_timeline(user_story.project) serialized_obj = TimelineSerializer(project_timeline[0]) serialized_obj.data["data"]["user"]["is_profile_visible"] = False
def test_create_membership_timeline(): membership = factories.MembershipFactory.create() project_timeline = service.get_project_timeline(membership.project) user_timeline = service.get_user_timeline(membership.user) assert project_timeline[0].event_type == "projects.membership.create" assert project_timeline[0].data["project"]["id"] == membership.project.id assert project_timeline[0].data["user"]["id"] == membership.user.id assert user_timeline[0].event_type == "projects.membership.create" assert user_timeline[0].data["project"]["id"] == membership.project.id assert user_timeline[0].data["user"]["id"] == membership.user.id
def test_update_milestone_timeline(): milestone = factories.MilestoneFactory.create(name="test milestone timeline") history_services.take_snapshot(milestone, user=milestone.owner) milestone.name = "test milestone timeline updated" milestone.save() history_services.take_snapshot(milestone, user=milestone.owner) project_timeline = service.get_project_timeline(milestone.project) assert project_timeline[0].event_type == "milestones.milestone.change" assert project_timeline[0].data["milestone"]["name"] == "test milestone timeline updated" assert project_timeline[0].data["values_diff"]["name"][0] == "test milestone timeline" assert project_timeline[0].data["values_diff"]["name"][1] == "test milestone timeline updated"
def test_update_project_timeline(): project = factories.ProjectFactory.create(name="test project timeline") history_services.take_snapshot(project, user=project.owner) project.name = "test project timeline updated" project.save() history_services.take_snapshot(project, user=project.owner) project_timeline = service.get_project_timeline(project) assert project_timeline[0].event_type == "projects.project.change" assert project_timeline[0].data["project"]["name"] == "test project timeline updated" assert project_timeline[0].data["values_diff"]["name"][0] == "test project timeline" assert project_timeline[0].data["values_diff"]["name"][1] == "test project timeline updated"
def test_delete_task_timeline(): task = factories.TaskFactory.create(subject="test task timeline") user_watcher= factories.UserFactory() task.add_watcher(user_watcher) history_services.take_snapshot(task, user=task.owner, delete=True) project_timeline = service.get_project_timeline(task.project) assert project_timeline[0].event_type == "tasks.task.delete" assert project_timeline[0].data["task"]["subject"] == "test task timeline" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "tasks.task.delete" assert user_watcher_timeline[0].data["task"]["subject"] == "test task timeline"
def test_delete_wiki_page_timeline(): page = factories.WikiPageFactory.create(slug="test wiki page timeline") user_watcher= factories.UserFactory() page.add_watcher(user_watcher) history_services.take_snapshot(page, user=page.owner, delete=True) project_timeline = service.get_project_timeline(page.project) assert project_timeline[0].event_type == "wiki.wikipage.delete" assert project_timeline[0].data["wikipage"]["slug"] == "test wiki page timeline" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "wiki.wikipage.delete" assert user_watcher_timeline[0].data["wikipage"]["slug"] == "test wiki page timeline"
def test_delete_user_story_timeline(): user_story = factories.UserStoryFactory.create(subject="test us timeline") user_watcher= factories.UserFactory() user_story.add_watcher(user_watcher) history_services.take_snapshot(user_story, user=user_story.owner, delete=True) project_timeline = service.get_project_timeline(user_story.project) assert project_timeline[0].event_type == "userstories.userstory.delete" assert project_timeline[0].data["userstory"]["subject"] == "test us timeline" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "userstories.userstory.delete" assert user_watcher_timeline[0].data["userstory"]["subject"] == "test us timeline"
def test_delete_issue_timeline(): issue = factories.IssueFactory.create(subject="test issue timeline") user_watcher= factories.UserFactory() issue.add_watcher(user_watcher) history_services.take_snapshot(issue, user=issue.owner, delete=True) project_timeline = service.get_project_timeline(issue.project) assert project_timeline[0].event_type == "issues.issue.delete" assert project_timeline[0].data["issue"]["subject"] == "test issue timeline" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "issues.issue.delete" assert user_watcher_timeline[0].data["issue"]["subject"] == "test issue timeline"
def test_delete_milestone_timeline(): milestone = factories.MilestoneFactory.create(name="test milestone timeline") user_watcher= factories.UserFactory() milestone.add_watcher(user_watcher) history_services.take_snapshot(milestone, user=milestone.owner, delete=True) project_timeline = service.get_project_timeline(milestone.project) assert project_timeline[0].event_type == "milestones.milestone.delete" assert project_timeline[0].data["milestone"]["name"] == "test milestone timeline" user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "milestones.milestone.delete" assert user_watcher_timeline[0].data["milestone"]["name"] == "test milestone timeline"
def test_update_user_story_timeline(): user_story = factories.UserStoryFactory.create(subject="test us timeline") history_services.take_snapshot(user_story, user=user_story.owner) user_story.subject = "test us timeline updated" user_story.save() history_services.take_snapshot(user_story, user=user_story.owner) project_timeline = service.get_project_timeline(user_story.project) assert project_timeline[0].event_type == "userstories.userstory.change" assert project_timeline[0].data["userstory"]["subject"] == "test us timeline updated" assert project_timeline[0].data["values_diff"]["subject"][0] == "test us timeline" assert project_timeline[0].data["values_diff"]["subject"][1] == "test us timeline updated"
def test_update_issue_timeline(): issue = factories.IssueFactory.create(subject="test issue timeline") history_services.take_snapshot(issue, user=issue.owner) issue.subject = "test issue timeline updated" issue.save() history_services.take_snapshot(issue, user=issue.owner) project_timeline = service.get_project_timeline(issue.project) assert project_timeline[0].event_type == "issues.issue.change" assert project_timeline[0].data["issue"]["subject"] == "test issue timeline updated" assert project_timeline[0].data["values_diff"]["subject"][0] == "test issue timeline" assert project_timeline[0].data["values_diff"]["subject"][1] == "test issue timeline updated"
def test_update_task_timeline(): task = factories.TaskFactory.create(subject="test task timeline") history_services.take_snapshot(task, user=task.owner) task.subject = "test task timeline updated" task.save() history_services.take_snapshot(task, user=task.owner) project_timeline = service.get_project_timeline(task.project) assert project_timeline[0].event_type == "tasks.task.change" assert project_timeline[0].data["task"]["subject"] == "test task timeline updated" assert project_timeline[0].data["values_diff"]["subject"][0] == "test task timeline" assert project_timeline[0].data["values_diff"]["subject"][1] == "test task timeline updated"
def test_update_wiki_page_timeline(): page = factories.WikiPageFactory.create(slug="test wiki page timeline") history_services.take_snapshot(page, user=page.owner) page.slug = "test wiki page timeline updated" page.save() history_services.take_snapshot(page, user=page.owner) project_timeline = service.get_project_timeline(page.project) assert project_timeline[0].event_type == "wiki.wikipage.change" assert project_timeline[0].data["wikipage"]["slug"] == "test wiki page timeline updated" assert project_timeline[0].data["values_diff"]["slug"][0] == "test wiki page timeline" assert project_timeline[0].data["values_diff"]["slug"][1] == "test wiki page timeline updated"
def test_delete_project_timeline(): project = factories.ProjectFactory.create(name="test project timeline") user_watcher= factories.UserFactory() project.add_watcher(user_watcher) history_services.take_snapshot(project, user=project.owner, delete=True) user_timeline = service.get_project_timeline(project) assert user_timeline[0].event_type == "projects.project.delete" assert user_timeline[0].data["project"]["id"] == project.id user_watcher_timeline = service.get_profile_timeline(user_watcher) assert user_watcher_timeline[0].event_type == "projects.project.delete" assert user_watcher_timeline[0].data["project"]["id"] == project.id
def test_update_task_timeline(): task = factories.TaskFactory.create(subject="test task timeline") history_services.take_snapshot(task, user=task.owner) task.subject = "test task timeline updated" task.save() history_services.take_snapshot(task, user=task.owner) project_timeline = service.get_project_timeline(task.project) assert project_timeline[0].event_type == "tasks.task.change" assert project_timeline[0].data["task"][ "subject"] == "test task timeline updated" assert project_timeline[0].data["values_diff"]["subject"][ 0] == "test task timeline" assert project_timeline[0].data["values_diff"]["subject"][ 1] == "test task timeline updated"
def test_update_issue_timeline(): issue = factories.IssueFactory.create(subject="test issue timeline") history_services.take_snapshot(issue, user=issue.owner) issue.subject = "test issue timeline updated" issue.save() history_services.take_snapshot(issue, user=issue.owner) project_timeline = service.get_project_timeline(issue.project) assert project_timeline[0].event_type == "issues.issue.change" assert project_timeline[0].data["issue"][ "subject"] == "test issue timeline updated" assert project_timeline[0].data["values_diff"]["subject"][ 0] == "test issue timeline" assert project_timeline[0].data["values_diff"]["subject"][ 1] == "test issue timeline updated"
def test_update_wiki_page_timeline(): page = factories.WikiPageFactory.create(slug="test wiki page timeline") history_services.take_snapshot(page, user=page.owner) page.slug = "test wiki page timeline updated" page.save() history_services.take_snapshot(page, user=page.owner) project_timeline = service.get_project_timeline(page.project) assert project_timeline[0].event_type == "wiki.wikipage.change" assert project_timeline[0].data["wikipage"][ "slug"] == "test wiki page timeline updated" assert project_timeline[0].data["values_diff"]["slug"][ 0] == "test wiki page timeline" assert project_timeline[0].data["values_diff"]["slug"][ 1] == "test wiki page timeline updated"
def render_project(project, outfile, chunk_size = 8190): serializer = serializers.ProjectExportSerializer(project) outfile.write('{\n') first_field = True for field_name in serializer.fields.keys(): # Avoid writing "," in the last element if not first_field: outfile.write(",\n") else: first_field = False field = serializer.fields.get(field_name) field.initialize(parent=serializer, field_name=field_name) # These four "special" fields hava attachments so we use them in a special way if field_name in ["wiki_pages", "user_stories", "tasks", "issues"]: value = get_component(project, field_name) outfile.write('"{}": [\n'.format(field_name)) attachments_field = field.fields.pop("attachments", None) if attachments_field: attachments_field.initialize(parent=field, field_name="attachments") first_item = True for item in value.iterator(): # Avoid writing "," in the last element if not first_item: outfile.write(",\n") else: first_item = False dumped_value = json.dumps(field.to_native(item)) writing_value = dumped_value[:-1]+ ',\n "attachments": [\n' outfile.write(writing_value) first_attachment = True for attachment in item.attachments.iterator(): # Avoid writing "," in the last element if not first_attachment: outfile.write(",\n") else: first_attachment = False # Write all the data expect the serialized file attachment_serializer = serializers.AttachmentExportSerializer(instance=attachment) attached_file_serializer = attachment_serializer.fields.pop("attached_file") dumped_value = json.dumps(attachment_serializer.data) dumped_value = dumped_value[:-1] + ',\n "attached_file":{\n "data":"' outfile.write(dumped_value) # We write the attached_files by chunks so the memory used is not increased attachment_file = attachment.attached_file if default_storage.exists(attachment_file.name): with default_storage.open(attachment_file.name) as f: while True: bin_data = f.read(chunk_size) if not bin_data: break b64_data = base64.b64encode(bin_data).decode('utf-8') outfile.write(b64_data) outfile.write('", \n "name":"{}"}}\n}}'.format( os.path.basename(attachment_file.name))) outfile.write(']}') outfile.flush() gc.collect() outfile.write(']') else: value = field.field_to_native(project, field_name) outfile.write('"{}": {}'.format(field_name, json.dumps(value))) # Generate the timeline outfile.write(',\n"timeline": [\n') first_timeline = True for timeline_item in get_project_timeline(project).iterator(): # Avoid writing "," in the last element if not first_timeline: outfile.write(",\n") else: first_timeline = False dumped_value = json.dumps(serializers.TimelineExportSerializer(timeline_item).data) outfile.write(dumped_value) outfile.write(']}\n')
def render_project(project, outfile, chunk_size=8190): serializer = serializers.ProjectExportSerializer(project) outfile.write(b'{\n') first_field = True for field_name in serializer._field_map.keys(): # Avoid writing "," in the last element if not first_field: outfile.write(b",\n") else: first_field = False field = serializer._field_map.get(field_name) # field.initialize(parent=serializer, field_name=field_name) # These four "special" fields hava attachments so we use them in a special way if field_name in [ "wiki_pages", "user_stories", "tasks", "issues", "epics" ]: value = get_component(project, field_name) if field_name != "wiki_pages": value = value.select_related('owner', 'status', 'project', 'assigned_to', 'custom_attributes_values') if field_name in ["user_stories", "tasks", "issues"]: value = value.select_related('milestone') if field_name == "issues": value = value.select_related('severity', 'priority', 'type') value = value.prefetch_related('history_entry', 'attachments') outfile.write('"{}": [\n'.format(field_name).encode()) first_item = True for item in value.iterator(): # Avoid writing "," in the last element if not first_item: outfile.write(b",\n") else: first_item = False field.many = False dumped_value = json.dumps(field.to_value(item)) outfile.write(dumped_value.encode()) outfile.flush() gc.collect() outfile.write(b']') else: if isinstance(field, MethodField): value = field.as_getter(field_name, serializers.ProjectExportSerializer)( serializer, project) else: attr = getattr(project, field_name) value = field.to_value(attr) outfile.write('"{}": {}'.format(field_name, json.dumps(value)).encode()) # Generate the timeline outfile.write(b',\n"timeline": [\n') first_timeline = True for timeline_item in get_project_timeline(project).iterator(): # Avoid writing "," in the last element if not first_timeline: outfile.write(b",\n") else: first_timeline = False dumped_value = json.dumps( serializers.TimelineExportSerializer(timeline_item).data) outfile.write(dumped_value.encode()) outfile.write(b']}\n')
def get_timeline(self, obj): timeline_qs = timeline_service.get_project_timeline(obj) return TimelineExportSerializer(timeline_qs, many=True).data
def render_project(project, outfile, chunk_size=8190): serializer = serializers.ProjectExportSerializer(project) outfile.write(b'{\n') first_field = True for field_name in serializer._field_map.keys(): # Avoid writing "," in the last element if not first_field: outfile.write(b",\n") else: first_field = False field = serializer._field_map.get(field_name) # field.initialize(parent=serializer, field_name=field_name) # These four "special" fields hava attachments so we use them in a special way if field_name in ["wiki_pages", "user_stories", "tasks", "issues"]: value = get_component(project, field_name) if field_name != "wiki_pages": value = value.select_related('owner', 'status', 'milestone', 'project', 'assigned_to', 'custom_attributes_values') if field_name == "issues": value = value.select_related('severity', 'priority', 'type') value = value.prefetch_related('history_entry', 'attachments') outfile.write('"{}": [\n'.format(field_name).encode()) first_item = True for item in value.iterator(): # Avoid writing "," in the last element if not first_item: outfile.write(b",\n") else: first_item = False field.many = False dumped_value = json.dumps(field.to_value(item)) outfile.write(dumped_value.encode()) outfile.flush() gc.collect() outfile.write(b']') else: if isinstance(field, MethodField): value = field.as_getter(field_name, serializers.ProjectExportSerializer)(serializer, project) else: attr = getattr(project, field_name) value = field.to_value(attr) outfile.write('"{}": {}'.format(field_name, json.dumps(value)).encode()) # Generate the timeline outfile.write(b',\n"timeline": [\n') first_timeline = True for timeline_item in get_project_timeline(project).iterator(): # Avoid writing "," in the last element if not first_timeline: outfile.write(b",\n") else: first_timeline = False dumped_value = json.dumps(serializers.TimelineExportSerializer(timeline_item).data) outfile.write(dumped_value.encode()) outfile.write(b']}\n')