Esempio n. 1
0
def team_member_new(member_pk):
    if getattr(settings, "MESSAGES_DISABLED", False):
        return
    from messages.models import Message
    from teams.models import TeamMember
    member = TeamMember.objects.get(pk=member_pk)
    if not _team_sends_notification(member.team,
                                    'block_team_member_new_message'):
        return False
    from videos.models import Action
    from teams.models import TeamMember
    # the feed item should appear on the timeline for all team members
    # as a team might have thousands of members, this one item has
    # to show up on all of them
    Action.create_new_member_handler(member)
    # notify  admins and owners through messages
    notifiable = TeamMember.objects.filter(
        team=member.team,
        role__in=[TeamMember.ROLE_ADMIN,
                  TeamMember.ROLE_OWNER]).exclude(pk=member.pk)
    for m in notifiable:
        context = {
            "new_member": member.user,
            "team": member.team,
            "user": m.user,
            "role": member.role,
            "url_base": get_url_base(),
        }
        body = render_to_string("messages/team-new-member.txt", context)
        subject = ugettext("%s team has a new member" % (member.team))
        if m.user.notify_by_message:
            msg = Message()
            msg.subject = subject
            msg.content = body
            msg.user = m.user
            msg.object = m.team
            msg.save()
        template_name = "messages/email/team-new-member.html"
        Meter('templated-emails-sent-by-type.teams.new-member').inc()
        send_templated_email(m.user, subject, template_name, context)

    # now send welcome mail to the new member
    template_name = "messages/team-welcome.txt"
    context = {
        "team": member.team,
        "url_base": get_url_base(),
        "role": member.role,
        "user": member.user,
    }
    body = render_to_string(template_name, context)

    msg = Message()
    msg.subject = ugettext("You've joined the %s team!" % (member.team))
    msg.content = body
    msg.user = member.user
    msg.object = member.team
    msg.save()
    template_name = "messages/email/team-welcome.html"
    Meter('templated-emails-sent-by-type.teams.welcome').inc()
    send_templated_email(msg.user, msg.subject, template_name, context)
Esempio n. 2
0
def team_member_leave(team_pk, user_pk):
    if getattr(settings, "MESSAGES_DISABLED", False):
        return
    from messages.models import Message
    from teams.models import TeamMember, Team
    user = User.objects.get(pk=user_pk)
    team = Team.objects.get(pk=team_pk)
    if not team_sends_notification(
            team, 'block_team_member_leave_message') or not user.is_active:
        return False
    # the feed item should appear on the timeline for all team members
    # as a team might have thousands of members, this one item has
    # to show up on all of them
    # notify  admins and owners through messages
    notifiable = TeamMember.objects.filter(
        team=team,
        user__is_active=True,
        role__in=[TeamMember.ROLE_ADMIN, TeamMember.ROLE_OWNER])
    subject = fmt(ugettext(u"%(user)s has left the %(team)s team"),
                  user=user,
                  team=team)
    for m in notifiable:
        context = {
            "parting_member": user,
            "team": team,
            "user": m.user,
            "url_base": get_url_base(),
        }
        body = render_to_string("messages/team-member-left.txt", context)
        if m.user.notify_by_message:
            msg = Message()
            msg.message_type = 'S'
            msg.subject = subject
            msg.content = body
            msg.user = m.user
            msg.object = team
            msg.save()
        send_templated_email(m.user, subject,
                             "messages/email/team-member-left.html", context)

    context = {
        "team": team,
        "user": user,
        "url_base": get_url_base(),
    }
    subject = fmt(ugettext("You've left the %(team)s team!"), team=team)
    if user.notify_by_message:
        template_name = "messages/team-member-you-have-left.txt"
        msg = Message()
        msg.message_type = 'S'
        msg.subject = subject
        msg.content = render_to_string(template_name, context)
        msg.user = user
        msg.object = team
        msg.save()
    template_name = "messages/email/team-member-you-have-left.html"
    send_templated_email(user, subject, template_name, context)
Esempio n. 3
0
 def setUp(self):
     self.user1 = User.objects.create_user('user3', '*****@*****.**', '123456')
     self.user2 = User.objects.create_user('user4', '*****@*****.**', '123456')
     self.msg1 = Message(sender=self.user1, recipient=self.user2, subject='Subject Text 1', body='Body Text 1')
     self.msg2 = Message(sender=self.user1, recipient=self.user2, subject='Subject Text 2', body='Body Text 2')
     self.msg1.sender_deleted_at = datetime.datetime.now()
     self.msg2.recipient_deleted_at = datetime.datetime.now()
     self.msg1.save()
     self.msg2.save()
Esempio n. 4
0
def team_member_leave(team_pk, user_pk):
    if getattr(settings, "MESSAGES_DISABLED", False):
        return
    from messages.models import Message
    from teams.models import TeamMember, Team
    user = User.objects.get(pk=user_pk)
    team = Team.objects.get(pk=team_pk)
    from videos.models import Action
    # the feed item should appear on the timeline for all team members
    # as a team might have thousands of members, this one item has
    # to show up on all of them
    Action.create_member_left_handler(team, user)
    # notify  admins and owners through messages
    notifiable = TeamMember.objects.filter(
        team=team, role__in=[TeamMember.ROLE_ADMIN, TeamMember.ROLE_OWNER])
    subject = ugettext(u"%(user)s has left the %(team)s team" %
                       dict(user=user, team=team))
    for m in notifiable:
        context = {
            "parting_member": user,
            "team": team,
            "user": m.user,
            "url_base": get_url_base(),
        }
        body = render_to_string("messages/team-member-left.txt", context)
        if m.user.notify_by_message:
            msg = Message()
            msg.subject = subject
            msg.content = body
            msg.user = m.user
            msg.object = team
            msg.save()
        Meter('templated-emails-sent-by-type.teams.someone-left').inc()
        send_templated_email(m.user, subject,
                             "messages/email/team-member-left.html", context)

    context = {
        "team": team,
        "user": user,
        "url_base": get_url_base(),
    }
    subject = ugettext("You've left the %s team!" % (team))
    if user.notify_by_message:
        template_name = "messages/team-member-you-have-left.txt"
        msg = Message()
        msg.subject = subject
        msg.content = render_to_string(template_name, context)
        msg.user = user
        msg.object = team
        msg.save()
    template_name = "messages/email/team-member-you-have-left.html"
    Meter('templated-emails-sent-by-type.teams.you-left').inc()
    send_templated_email(user, subject, template_name, context)
Esempio n. 5
0
def application_sent(application_pk):
    if getattr(settings, "MESSAGES_DISABLED", False):
        return
    from messages.models import Message
    from teams.models import Application, TeamMember
    application = Application.objects.get(pk=application_pk)
    notifiable = TeamMember.objects.filter(
        team=application.team,
        role__in=[TeamMember.ROLE_ADMIN, TeamMember.ROLE_OWNER])
    for m in notifiable:

        template_name = "messages/application-sent.txt"
        context = {
            "applicant": application.user,
            "url_base": get_url_base(),
            "team": application.team,
            "note": application.note,
            "user": m.user,
        }
        body = render_to_string(template_name, context)
        subject = ugettext(u'%(user)s is applying for team %(team)s') % dict(
            user=application.user, team=application.team.name)
        if m.user.notify_by_message:
            msg = Message()
            msg.subject = subject
            msg.content = body
            msg.user = m.user
            msg.object = application.team
            msg.author = application.user
            msg.save()
        Meter('templated-emails-sent-by-type.teams.application-sent').inc()
        send_templated_email(m.user, subject,
                             "messages/email/application-sent-email.html",
                             context)
    return True
Esempio n. 6
0
    def save(self, sender, parent_msg=None):
        project = self.cleaned_data['project']
        try:
            project = Project.objects.get(id=int(project))
        except Project.DoesNotExist:
            raise forms.ValidationError(_(u'That study group does not exist.'))
        recipients = project.organizers()
        subject = "[%s] " % project.name[:20] + self.cleaned_data['subject']
        body = self.cleaned_data['body']
        body = '%s\n\n%s' % (
            self.cleaned_data['body'],
            _('You received this message through the Contact Organizer form '
              'at %(project)s: http://%(domain)s%(url)s') % {
                  'project': project.name,
                  'domain': Site.objects.get_current().domain,
                  'url': project.get_absolute_url()
              })

        message_list = []
        for r in recipients:
            msg = Message(
                sender=sender,
                recipient=r.user.user,
                subject=subject,
                body=body,
            )
            if parent_msg is not None:
                msg.parent_msg = parent_msg
                parent_msg.replied_at = datetime.datetime.now()
                parent_msg.save()
            msg.save()
            message_list.append(msg)
        return message_list
Esempio n. 7
0
    def save(self, sender, parent_msg=None):
        r = self.cleaned_data['recipient']
        subject = self.cleaned_data['subject']
        body = self.cleaned_data['body']
        message_list = []

        r = User.objects.get(id=r)

        msg = Message(
            sender=sender,
            recipient=r,
            subject=subject,
            body=body,
        )
        if parent_msg is not None:
            msg.parent_msg = parent_msg
            parent_msg.replied_at = datetime.datetime.now()
            parent_msg.save()
        msg.save()
        message_list.append(msg)
        if notification:
            if parent_msg is not None:
                notification.send([sender], "messages_replied", {
                    'message': msg,
                })
            else:
                notification.send([sender], "messages_sent", {
                    'message': msg,
                })
        return message_list
Esempio n. 8
0
def message_send(request, username):
    return_dict = {
        'status': 1,
    }
    if request.method == 'POST':
        try:
            friend_user = User.objects.get(username__exact=username)
        except:
            return_dict['status'] = 0
            return HttpResponse(json.dumps(return_dict))
        friend_member = friend_user.members

        message = Message(member1=request.user.members, member2=friend_member)
        message.content = request.POST['message']
        message.timestamp = datetime.utcnow().replace(tzinfo=utc)
        message.save()
        template_dict = {
            'message': message,
            'member': request.user.members,
        }
        return render_to_response(
            "user_pages/ajax_delivery/message_after_send.html",
            template_dict,
            context_instance=RequestContext(request))

    else:
        return_dict['status'] = 0
    return HttpResponse(json.dumps(return_dict))
Esempio n. 9
0
 def send_complete_message(self):
     msg = Message()
     msg.user = self.user
     msg.subject = u'Your request was completed'
     msg.object = self
     msg.save()
     return msg
Esempio n. 10
0
def team_application_denied(application_pk):

    if getattr(settings, "MESSAGES_DISABLED", False):
        return
    from messages.models import Message
    from teams.models import Application
    application = Application.objects.get(pk=application_pk)
    template_name = "messages/email/team-application-denied.html"
    context = {
        "team": application.team,
        "user": application.user,
        "url_base": get_url_base(),
        "note": application.note,
    }
    if application.user.notify_by_message:
        msg = Message()
        msg.subject = ugettext(
            u'Your application to join the %s team has been declined' %
            application.team.name)
        msg.content = render_to_string("messages/team-application-denied.txt",
                                       context)
        msg.user = application.user
        msg.object = application.team
        msg.save()
    Meter('templated-emails-sent-by-type.teams.application-declined').inc()
    send_templated_email(msg.user, msg.subject, template_name, context)
    application.delete()
Esempio n. 11
0
def team_application_denied(application_pk):

    if getattr(settings, "MESSAGES_DISABLED", False):
        return
    from messages.models import Message
    from teams.models import Application
    application = Application.objects.get(pk=application_pk)
    if not team_sends_notification(application.team,
                                   'block_application_denided_message'
                                   ) or not application.user.is_active:
        return False
    template_name = "messages/email/team-application-denied.html"
    context = {
        "team": application.team,
        "user": application.user,
        "url_base": get_url_base(),
        "note": application.note,
    }
    subject = fmt(ugettext(u'Your application to join the %(team)s '
                           u'team has been declined'),
                  team=application.team.name)
    if application.user.notify_by_message:
        msg = Message()
        msg.message_type = 'S'
        msg.subject = subject
        msg.content = render_to_string("messages/team-application-denied.txt",
                                       context)
        msg.user = application.user
        msg.object = application.team
        msg.save()
    send_templated_email(application.user, subject, template_name, context)
Esempio n. 12
0
 def save(self, sender, parent_msg=None):
     project = self.cleaned_data['project']
     try:
         project = Project.objects.get(id=int(project))
     except Project.DoesNotExist:
         raise forms.ValidationError(
             _(u'Hmm, that does not look like a valid project'))
     recipients = project.followers()
     subject = self.cleaned_data['subject']
     body = self.cleaned_data['body']
     message_list = []
     for r in recipients:
         msg = Message(
             sender=sender,
             recipient=r.user,
             subject=subject,
             body=body,
         )
         if parent_msg is not None:
             msg.parent_msg = parent_msg
             parent_msg.replied_at = datetime.datetime.now()
             parent_msg.save()
         msg.save()
         message_list.append(msg)
     return message_list
Esempio n. 13
0
def send_message(template: MessageTemplate,
                 member,
                 db_session=None,
                 render_template=None,
                 **kwargs):

    if render_template is None:
        from flask import render_template

    subject = render_template(
        f"{template.value}.subject.html",
        public_url=get_public_url,
        member=member,
        **kwargs,
    )

    body = render_template(
        f"{template.value}.body.html",
        public_url=get_public_url,
        member=member,
        **kwargs,
    )

    if not db_session:
        from service.db import db_session

    db_session.add(
        Message(
            subject=subject,
            body=body,
            member_id=member.member_id,
            recipient=member.email,
            status=Message.QUEUED,
            template=template.value,
        ))
Esempio n. 14
0
 def create_message(self):
     message = Message(sender=self.her,
                       recipient=self.me,
                       subject='Subject Text',
                       body='Body Text')
     message.save()
     return message
Esempio n. 15
0
def send_reject_notification(task_pk, sent_back):
    raise NotImplementedError()
    from teams.models import Task
    from videos.models import Action
    from messages.models import Message
    try:
        task = Task.objects.select_related(
            "team_video__video", "team_video", "assignee", "subtitle_version").get(
                pk=task_pk)
    except Task.DoesNotExist:
        return False

    if task.new_review_base_version:
        user = task.new_review_base_version.author
    else:
        user = version.author
    if not user.is_active:
        return False
    version = task.get_subtitle_version()
    subject = ugettext(u"Your subtitles were not accepted")
    task_language = get_language_label(task.language)
    reviewer = task.assignee
    video = task.team_video.video
    subs_url = "%s%s" % (get_url_base(), reverse("videos:translation_history", kwargs={
        'video_id': video.video_id,
        'lang': task.language,
        'lang_id': version.subtitle_language.pk,

    }))
    reviewer_message_url = "%s%s?user=%s" % (
        get_url_base(), reverse("messages:new"), reviewer.username)

    context = {
        "team":task.team,
        "title": version.subtitle_language.get_title(),
        "user":user,
        "task_language": task_language,
        "url_base":get_url_base(),
        "task":task,
        "reviewer":reviewer,
        "note":task.body,
        "sent_back": sent_back,
        "subs_url": subs_url,
        "reviewer_message_url": reviewer_message_url,
    }
    msg = None
    if user.notify_by_message:
        template_name = "messages/team-task-rejected.txt"
        msg = Message()
        msg.message_type = 'S'
        msg.subject = subject
        msg.content = render_to_string(template_name,context)
        msg.user = user
        msg.object = task.team
        msg.save()

    template_name = "messages/email/team-task-rejected.html"
    email_res =  send_templated_email(user, subject, template_name, context)
    Action.create_rejected_video_handler(version, reviewer)
    return msg, email_res
Esempio n. 16
0
 def setUp(self):
     self.client = Client()
     user = User.objects.create_user(username="******", password="******")
     self.client.login(username="******", password="******")
     self.chat = RandomChatFactory.create()
     self.chat.save()
     message = Message(chat=self.chat, user=user, content='test')
     message.save()
Esempio n. 17
0
    def test_message_repr(self):
        """Does the REPR look how we expect"""
        m = Message(text="Test Message", user_id=self.u_id)
        db.session.add(m)
        db.session.commit()

        id = m.id
        self.assertEqual(m.__repr__(), f"<Message #{id}: u_id={self.u_id}>")
Esempio n. 18
0
def invite_send_message(sender, instance, created, **kwargs):
    if created:
        msg = Message()
        msg.subject = ugettext(u'Invitation to join a team')
        msg.user = instance.user
        msg.object = instance
        msg.author = instance.author
        msg.save()
Esempio n. 19
0
 def create_messages(self):
     messages = [
         Message(user=user,
                 content=self.validated_data['content'],
                 subject=self.validated_data['subject'],
                 author=self.context['user']) for user in self.recipients()
         if user != self.context['user']
     ]
     Message.objects.bulk_create(messages)
Esempio n. 20
0
 def setUp(self):
     self.user1 = User.objects.create_user('user1', '*****@*****.**',
                                           '123456')
     self.user2 = User.objects.create_user('user2', '*****@*****.**',
                                           '123456')
     self.msg1 = Message(sender=self.user1,
                         recipient=self.user2,
                         subject='Subject Text',
                         body='Body Text')
     self.msg1.save()
Esempio n. 21
0
def _process_post_form_messages(form, logged_user, to_user):

    msg = Message(from_user=logged_user,
                  to_user=to_user,
                  post=form.post.data,
                  message_type=POST).save()
    Feed(user=logged_user, message=msg).save()
    MessageProcessor(message_obj=msg).post_message_to_all_friends_feeds()

    return msg
Esempio n. 22
0
 def send_to_teams(self, team_ids, author):
     subject = self.cleaned_data['subject']
     content = self.cleaned_data['content']
     content = u''.join([content, '\n\n', ugettext('This message is from site administrator.')])
     users = User.objects.filter(teams__in=team_ids).exclude(pk=author.pk)
     for user in users:
         m = Message(author=author, user=user)
         m.subject = subject
         m.content = content
         m.save()
     return users.count()
Esempio n. 23
0
def SendMessage(profile, otherUserFacebookID, messageBody):
    try:
        sender = profile
        recipient = Profile.objects.get(facebookID=otherUserFacebookID)
        body = messageBody
        message = Message(sender=sender, recipient=recipient,body=body)
        message.save()
        message_sent(message)
        return True
    except:
        return False
Esempio n. 24
0
 def test_view_message(self):
     """Test user can view message in inbox."""
     Relationship(source=self.user, target_user=self.user_two).save()
     message = Message(sender=self.user_two.user,
                       recipient=self.user.user,
                       subject='test message subject',
                       body='test message body')
     message.save()
     self.client.login(username=self.test_username,
                       password=self.test_password)
     response = self.client.get("/%s/messages/inbox/" % (self.locale, ))
     self.assertContains(response, 'test message body')
Esempio n. 25
0
 def run(self, form, messages, **kwargs):
     log = self.get_logger(**kwargs)
     log.debug("Sending email to %d user(s)." % (len(messages),))
     for message in messages:
         (sender, recipient, subject, body, parent) = message
         msg = Message(sender=sender, recipient=recipient,
                       subject=subject, body=body)
         if parent is not None:
             msg.parent_msg = parent
             parent.replied_at = datetime.datetime.now()
             parent.save()
         msg.save()
Esempio n. 26
0
 def _create_message(self, to_user, message_type='M', reply_to=None):
     self.message = Message(user=to_user,
                        author=self.author,
                        subject=self.subject,
                        message_type=message_type,
                        content=self.body)
     if reply_to is not None:
         if reply_to.thread:
             self.message.thread = reply_to.thread
         else:
             self.message.thread = reply_to.pk
     self.message.save()
     return self.message
Esempio n. 27
0
def email_confirmed(user_pk):
    from messages.models import Message
    user = User.objects.get(pk=user_pk)
    subject = "Welcome aboard!"
    context = {"user": user}
    if user.notify_by_message:
        body = render_to_string("messages/email-confirmed.txt", context)
        message = Message(user=user, subject=subject, content=body)
        message.save()
    template_name = "messages/email/email-confirmed.html"
    Meter('templated-emails-sent-by-type.email-confirmed').inc()
    send_templated_email(user, subject, template_name, context)
    return True
Esempio n. 28
0
    def test_message_model(self):
        """Does basic model work?"""
        m = Message(text="Test Message", user_id=self.u_id)

        db.session.add(m)
        db.session.commit()

        self.assertEqual(m.text, "Test Message")
        self.assertEqual(m.user_id, self.u_id)
        self.assertIsNotNone(m.timestamp)
        self.assertIsNotNone(m.id)

        u = User.query.get(self.u_id)
        self.assertEqual(m.user, u)
Esempio n. 29
0
 def create_message(self, member=None, **kwargs):
     member = member or self.member
     
     obj = dict(
         member=member,
         subject=random_str(),
         body=self.fake.bs(),
         recipient=member.email if member else self.fake.email(),
         status=Message.QUEUED,
     )
     obj.update(**kwargs)
     self.message = Message(**obj)
     db_session.add(self.message)
     db_session.commit()
     return self.member
Esempio n. 30
0
def team_invitation_sent(invite_pk):
    from messages.models import Message
    from teams.models import Invite, Setting, TeamMember
    invite = Invite.objects.get(pk=invite_pk)
    if not team_sends_notification(
            invite.team,
            'block_invitation_sent_message') or not invite.user.is_active:
        return False
    # does this team have a custom message for this?
    team_default_message = None
    messages = Setting.objects.messages().filter(team=invite.team)
    if messages.exists():
        data = {}
        for m in messages:
            data[m.get_key_display()] = m.data
        mapping = {
            TeamMember.ROLE_ADMIN: data['messages_admin'],
            TeamMember.ROLE_MANAGER: data['messages_manager'],
            TeamMember.ROLE_CONTRIBUTOR: data['messages_invite'],
        }
        team_default_message = mapping.get(invite.role, None)
    context = {
        'invite': invite,
        'role': invite.role,
        "user": invite.user,
        "inviter": invite.author,
        "team": invite.team,
        "invite_pk": invite_pk,
        'note': invite.note,
        'custom_message': team_default_message,
        'url_base': get_url_base(),
    }
    title = fmt(ugettext(u"You've been invited to team %(team)s on Amara"),
                team=invite.team.name)

    if invite.user.notify_by_message:
        body = render_to_string("messages/team-you-have-been-invited.txt",
                                context)
        msg = Message()
        msg.message_type = 'S'
        msg.subject = title
        msg.user = invite.user
        msg.object = invite
        msg.author = invite.author
        msg.content = body
        msg.save()
    template_name = 'messages/email/team-you-have-been-invited.html'
    return send_templated_email(invite.user, title, template_name, context)