def list_users(self, request, *args, **kwargs): self.check_permissions(request, "list_users", None) token = request.DATA.get('token', None) project_id = request.DATA.get('project', None) if not project_id: raise exc.WrongArguments(_("The project param is needed")) importer = PivotalImporter(request.user, token) users = importer.list_users(project_id) for user in users: user['user'] = None if not user['email']: continue try: tuesmon_user = User.objects.get(email=user['email']) except User.DoesNotExist: continue user['user'] = { 'id': tuesmon_user.id, 'full_name': tuesmon_user.get_full_name(), 'gravatar_id': get_user_gravatar_id(tuesmon_user), 'photo': get_user_photo_url(tuesmon_user), } return response.Ok(users)
def send_contact_email(contact_entry_id): contact_entry = models.ContactEntry.objects.filter( id=contact_entry_id).first() if contact_entry is None: return ctx = { "comment": contact_entry.comment, "full_name": contact_entry.user.get_full_name(), "project_name": contact_entry.project.name, "photo_url": get_user_photo_url(contact_entry.user), "user_profile_url": resolve_front_url("user", contact_entry.user.username), "project_settings_url": resolve_front_url("project-admin", contact_entry.project.slug), } users = contact_entry.project.get_users( with_admin_privileges=True).exclude(id=contact_entry.user_id) addresses = ", ".join([u.email for u in users]) email = mail_builder.contact_notification(addresses, ctx) email.extra_headers["Reply-To"] = ", ".join([contact_entry.user.email]) email.send()
def get_data(self, obj): # Updates the data user info saved if the user exists if hasattr(obj, "_prefetched_user"): user = obj._prefetched_user else: User = get_user_model() userData = obj.data.get("user", None) try: user = User.objects.get(id=userData["id"]) except User.DoesNotExist: user = None if user is not None: obj.data["user"] = { "id": user.pk, "name": user.get_full_name(), "photo": get_user_photo_url(user), "big_photo": get_user_big_photo_url(user), "gravatar_id": get_user_gravatar_id(user), "username": user.username, "is_profile_visible": user.is_active and not user.is_system, "date_joined": user.date_joined } if "values_diff" in obj.data and "attachments" in obj.data["values_diff"]: [[self.parse_url(item) for item in value] for key, value in obj.data["values_diff"]["attachments"].items() if value] return obj.data
def list_users(self, request, *args, **kwargs): self.check_permissions(request, "list_users", None) token = request.DATA.get('token', None) project_id = request.DATA.get('project', None) if not project_id: raise exc.WrongArguments(_("The project param is needed")) importer = AsanaImporter(request.user, token) try: users = importer.list_users(project_id) except exceptions.InvalidRequest: raise exc.BadRequest(_('Invalid Asana API request')) except exceptions.FailedRequest: raise exc.BadRequest(_('Failed to make the request to Asana API')) for user in users: if user['detected_user']: user['user'] = { 'id': user['detected_user'].id, 'full_name': user['detected_user'].get_full_name(), 'gravatar_id': get_user_gravatar_id(user['detected_user']), 'photo': get_user_photo_url(user['detected_user']), } del (user['detected_user']) return response.Ok(users)
def create_slackhook(url, channel, notify_config, obj): obj_type = _get_type(obj) if not _check_notify_permission(notify_config, obj_type, 'create'): return template = loader.get_template('tuesmon_contrib_slack/create.jinja') context = Context({"obj": obj, "obj_type": obj_type}) create_text = template.render(context.flatten()) data = { "text": create_text.strip(), "attachments": [{ "color": "good", "fields": [{ "title": "Creator", "value": obj.owner.get_full_name(), "short": True, }] }] } if obj_type == "wikipage": # For wikipages content = getattr(obj, 'content', None) if content: data["attachments"][0]["fields"].append({ "title": "Content", "value": content, "short": False, }) else: # For stories, tasks, issues and epics description = getattr(obj, 'description', None) if description: data["attachments"][0]["fields"].append({ "title": "Description", "value": description, "short": False, }) if channel: data["channel"] = channel data["username"] = "******".format( getattr(settings, "SLACKHOOKS_USERNAME", "Tuesmon"), obj.owner.get_full_name()) data["icon_url"] = get_user_photo_url(obj.owner) if data["icon_url"] and not data["icon_url"].startswith("http"): data["icon_url"] = "https:{}".format(data["icon_url"]) _send_request(url, data)
def delete_slackhook(url, channel, notify_config, obj, change): obj_type = _get_type(obj) if not _check_notify_permission(notify_config, obj_type, 'delete'): return template = loader.get_template('tuesmon_contrib_slack/delete.jinja') context = Context({"obj": obj, "obj_type": obj_type}) delete_text = template.render(context.flatten()) data = { "text": delete_text.strip(), "attachments": [{ "color": "danger", "fields": [] }] } if obj_type == "wikipage": # For wikipages content = getattr(obj, 'content', None) if content: data["attachments"][0]["fields"].append({ "title": "Content", "value": content, "short": False, }) else: # For stories, tasks, issues and epics description = getattr(obj, 'description', None) if description: data["attachments"][0]["fields"].append({ "title": "Description", "value": description, "short": False }) if channel: data["channel"] = channel data["username"] = "******".format( getattr(settings, "SLACKHOOKS_USERNAME", "Tuesmon"), change.user['name']) try: user = User.objects.get(pk=change.user['pk']) data["icon_url"] = get_user_photo_url(user) if data["icon_url"] and not data["icon_url"].startswith("http"): data["icon_url"] = "https:{}".format(data["icon_url"]) except User.DoesNotExist: data["icon_url"] = getattr( settings, "SLACKHOOKS_ICON", "https://manage.tuesmon.com/images/favicon.png") _send_request(url, data)
def get_user(self, entry): user = {"pk": None, "username": None, "name": None, "photo": None, "is_active": False} user.update(entry.user) user["photo"] = get_user_photo_url(entry.owner) user["gravatar_id"] = get_user_gravatar_id(entry.owner) if entry.owner: user["is_active"] = entry.owner.is_active if entry.owner.is_active or entry.owner.is_system: user["name"] = entry.owner.get_full_name() user["username"] = entry.owner.username return user
def list_users(self, request, *args, **kwargs): self.check_permissions(request, "list_users", None) token = request.DATA.get('token', None) project_id = request.DATA.get('project', None) if not project_id: raise exc.WrongArguments(_("The project param is needed")) importer = GithubImporter(request.user, token) users = importer.list_users(project_id) for user in users: if user['detected_user']: user['user'] = { 'id': user['detected_user'].id, 'full_name': user['detected_user'].get_full_name(), 'gravatar_id': get_user_gravatar_id(user['detected_user']), 'photo': get_user_photo_url(user['detected_user']), } del (user['detected_user']) return response.Ok(users)
def change_slackhook(url, channel, notify_config, obj, change): obj_type = _get_type(obj) if not _check_notify_permission(notify_config, obj_type, 'change'): return template_change = loader.get_template('tuesmon_contrib_slack/change.jinja') comment = re.sub(LINK_RE, _link_transform, change.comment) context = Context({ "obj": obj, "obj_type": obj_type, "change": change, "comment": comment }) change_text = template_change.render(context.flatten()) data = {"text": change_text.strip()} data['attachments'] = [] if channel: data["channel"] = channel # Get markdown fields if change.diff: template_field = loader.get_template( 'tuesmon_contrib_slack/field-diff.jinja') included_fields = ["description", "content", "blocked_note"] for field_name, values in change.diff.items(): if field_name in included_fields: attachment = _markdown_field_to_attachment( template_field, field_name, values) data['attachments'].append(attachment) # Get rest of fields if change.values_diff: template_field = loader.get_template( 'tuesmon_contrib_slack/field-diff.jinja') excluded_fields = [ "description_diff", "description_html", "content_diff", "content_html", "blocked_note_diff", "blocked_note_html", "backlog_order", "kanban_order", "taskboard_order", "us_order", "finish_date", "is_closed" ] for field_name, values in change.values_diff.items(): if field_name in excluded_fields: continue attachment = _field_to_attachment(template_field, field_name, values) if attachment: data['attachments'].append(attachment) data["username"] = "******".format( getattr(settings, "SLACKHOOKS_USERNAME", "Tuesmon"), change.user['name']) try: user = User.objects.get(pk=change.user['pk']) data["icon_url"] = get_user_photo_url(user) if data["icon_url"] and not data["icon_url"].startswith("http"): data["icon_url"] = "https:{}".format(data["icon_url"]) except User.DoesNotExist: data["icon_url"] = getattr( settings, "SLACKHOOKS_ICON", "https://manage.tuesmon.com/images/favicon.png") _send_request(url, data)
def handle(self, *args, **options): locale = options.get('locale') email_address = options.get('email') # Register email context = { "lang": locale, "user": get_user_model().objects.all().order_by("?").first(), "cancel_token": "cancel-token" } email = mail_builder.registered_user(email_address, context) email.send() # Membership invitation membership = Membership.objects.order_by("?").filter( user__isnull=True).first() membership.invited_by = get_user_model().objects.all().order_by( "?").first() membership.invitation_extra_text = "Text example, Text example,\nText example,\n\nText example" context = {"lang": locale, "membership": membership} email = mail_builder.membership_invitation(email_address, context) email.send() # Membership notification context = { "lang": locale, "membership": Membership.objects.order_by("?").filter( user__isnull=False).first() } email = mail_builder.membership_notification(email_address, context) email.send() # Feedback context = { "lang": locale, "feedback_entry": { "full_name": "Test full name", "email": "*****@*****.**", "comment": "Test comment", }, "extra": { "key1": "value1", "key2": "value2", }, } email = mail_builder.feedback_notification(email_address, context) email.send() # Password recovery context = { "lang": locale, "user": get_user_model().objects.all().order_by("?").first() } email = mail_builder.password_recovery(email_address, context) email.send() # Change email context = { "lang": locale, "user": get_user_model().objects.all().order_by("?").first() } email = mail_builder.change_email(email_address, context) email.send() # Export/Import emails context = { "lang": locale, "user": get_user_model().objects.all().order_by("?").first(), "project": Project.objects.all().order_by("?").first(), "error_subject": "Error generating project dump", "error_message": "Error generating project dump", } email = mail_builder.export_error(email_address, context) email.send() context = { "lang": locale, "user": get_user_model().objects.all().order_by("?").first(), "error_subject": "Error importing project dump", "error_message": "Error importing project dump", } email = mail_builder.import_error(email_address, context) email.send() deletion_date = timezone.now() + datetime.timedelta(seconds=60 * 60 * 24) context = { "lang": locale, "url": "http://dummyurl.com", "user": get_user_model().objects.all().order_by("?").first(), "project": Project.objects.all().order_by("?").first(), "deletion_date": deletion_date, } email = mail_builder.dump_project(email_address, context) email.send() context = { "lang": locale, "user": get_user_model().objects.all().order_by("?").first(), "project": Project.objects.all().order_by("?").first(), } email = mail_builder.load_dump(email_address, context) email.send() # Notification emails notification_emails = [ ("issues.Issue", "issues/issue-change"), ("issues.Issue", "issues/issue-create"), ("issues.Issue", "issues/issue-delete"), ("tasks.Task", "tasks/task-change"), ("tasks.Task", "tasks/task-create"), ("tasks.Task", "tasks/task-delete"), ("userstories.UserStory", "userstories/userstory-change"), ("userstories.UserStory", "userstories/userstory-create"), ("userstories.UserStory", "userstories/userstory-delete"), ("milestones.Milestone", "milestones/milestone-change"), ("milestones.Milestone", "milestones/milestone-create"), ("milestones.Milestone", "milestones/milestone-delete"), ("wiki.WikiPage", "wiki/wikipage-change"), ("wiki.WikiPage", "wiki/wikipage-create"), ("wiki.WikiPage", "wiki/wikipage-delete"), ] context = { "lang": locale, "project": Project.objects.all().order_by("?").first(), "changer": get_user_model().objects.all().order_by("?").first(), "history_entries": HistoryEntry.objects.all().order_by("?")[0:5], "user": get_user_model().objects.all().order_by("?").first(), } for notification_email in notification_emails: model = apps.get_model(*notification_email[0].split(".")) snapshot = { "subject": "Tests subject", "ref": 123123, "name": "Tests name", "slug": "test-slug" } queryset = model.objects.all().order_by("?") for obj in queryset: end = False entries = get_history_queryset_by_model_instance(obj).filter( is_snapshot=True).order_by("?") for entry in entries: if entry.snapshot: snapshot = entry.snapshot end = True break if end: break context["snapshot"] = snapshot cls = type("InlineCSSTemplateMail", (InlineCSSTemplateMail, ), {"name": notification_email[1]}) email = cls() email.send(email_address, context) # Transfer Emails context = { "project": Project.objects.all().order_by("?").first(), "requester": get_user_model().objects.all().order_by("?").first(), } email = mail_builder.transfer_request(email_address, context) email.send() context = { "project": Project.objects.all().order_by("?").first(), "receiver": get_user_model().objects.all().order_by("?").first(), "token": "test-token", "reason": "Test reason" } email = mail_builder.transfer_start(email_address, context) email.send() context = { "project": Project.objects.all().order_by("?").first(), "old_owner": get_user_model().objects.all().order_by("?").first(), "new_owner": get_user_model().objects.all().order_by("?").first(), "reason": "Test reason" } email = mail_builder.transfer_accept(email_address, context) email.send() context = { "project": Project.objects.all().order_by("?").first(), "rejecter": get_user_model().objects.all().order_by("?").first(), "reason": "Test reason" } email = mail_builder.transfer_reject(email_address, context) email.send() # Contact with project admins email project = Project.objects.all().order_by("?").first() user = get_user_model().objects.all().order_by("?").first() context = { "full_name": user.get_full_name(), "project_name": project.name, "photo_url": get_user_photo_url(user), "user_profile_url": resolve_front_url("user", user.username), "project_settings_url": resolve_front_url("project-admin", project.slug), "comment": "Test comment notification." } email = mail_builder.contact_notification(email_address, context) email.send() # GitHub importer email context = { "project": Project.objects.all().order_by("?").first(), "user": get_user_model().objects.all().order_by("?").first() } email = mail_builder.github_import_success(email_address, context) email.send() # Jira importer email context = { "project": Project.objects.all().order_by("?").first(), "user": get_user_model().objects.all().order_by("?").first() } email = mail_builder.jira_import_success(email_address, context) email.send() # Trello importer email context = { "project": Project.objects.all().order_by("?").first(), "user": get_user_model().objects.all().order_by("?").first() } email = mail_builder.trello_import_success(email_address, context) email.send() # Asana importer email context = { "project": Project.objects.all().order_by("?").first(), "user": get_user_model().objects.all().order_by("?").first() } email = mail_builder.asana_import_success(email_address, context) email.send() # Error importer email context = { "user": get_user_model().objects.all().order_by("?").first(), "error_subject": "Error importing GitHub project", "error_message": "Error importing GitHub project", "project": 1234, "exception": "Exception message" } email = mail_builder.importer_import_error(email_address, context) email.send()
def get_photo(self, obj): return get_user_photo_url(obj.user)