Example #1
0
    def has_object_permission(self, request, view, obj):
        """
        Make sure we give correct permissions to read/write the thread.
        """
        if not request.user.is_authenticated() or obj.read_permission_public:
            return obj.read_permission_public

        return user_has_perm_thread(obj, request.amo_user)
Example #2
0
def create_comm_note(app, version, author, body, note_type=comm.NO_ACTION,
                     perms=None):
    """
    Creates a note on an app version's thread.
    Creates a thread if a thread doesn't already exist.
    CC's app's Mozilla contacts to auto-join thread.

    app -- app object.
    version -- app version.
    author -- UserProfile for the note's author.
    body -- string/text for note comment.
    note_type -- integer for note_type (mkt constant), defaults to 0/NO_ACTION
                 (e.g. comm.APPROVAL, comm.REJECTION, comm.NO_ACTION).
    perms -- object of groups to grant permission to, will set flags on Thread.
             (e.g. {'developer': False, 'staff': True}).

    """
    if not waffle.switch_is_active('comm-dashboard'):
        return None, None

    # Dict of {'read_permission_GROUP_TYPE': boolean}.
    # Perm for dev, reviewer, senior_reviewer, moz_contact, staff all True by
    # default.
    perms = perms or {}
    create_perms = dict(('read_permission_%s' % key, has_perm)
                        for key, has_perm in perms.iteritems())

    # Get or create thread w/ custom permissions.
    thread = None
    threads = app.threads.filter(version=version)
    if threads.exists():
        thread = threads[0]
    else:
        # See if user has perms to create thread for this app.
        thread = CommunicationThread(addon=app, version=version,
                                     **create_perms)
        if user_has_perm_thread(thread, author):
            thread.save()
        else:
            raise PermissionDenied

    # Create note.
    note = thread.notes.create(note_type=note_type, body=body, author=author,
                               **create_perms)

    post_create_comm_note(note)

    return thread, note
Example #3
0
def save_from_email_reply(reply_text):
    parser = CommEmailParser(reply_text)
    uuid = parser.get_uuid()

    if not uuid:
        return False
    try:
        tok = CommunicationThreadToken.objects.get(uuid=uuid)
    except CommunicationThreadToken.DoesNotExist:
        log.error('An email was skipped with non-existing uuid %s' % uuid)
        return False

    if (user_has_perm_thread(tok.thread, tok.user) and tok.is_valid()):
        n = CommunicationNote.objects.create(note_type=comm.NO_ACTION,
            thread=tok.thread, author=tok.user, body=parser.get_body())
        log.info('A new note has been created (from %s using tokenid %s)' %
                 (tok.user.id, uuid))
        return n
    return False
Example #4
0
 def _eq_obj_perm(self, val):
     if self.type == 'note':
         eq_(user_has_perm_note(self.obj, self.user), val)
     else:
         eq_(user_has_perm_thread(self.obj, self.user), val)
Example #5
0
 def _eq_obj_perm(self, val):
     if self.type == 'note':
         eq_(user_has_perm_note(self.obj, self.user), val)
     else:
         eq_(user_has_perm_thread(self.obj, self.user), val)