示例#1
0
    def post(self, request, *args, **kwargs):
        original_module_leader = self.get_object().module_leader
        response = super(
            AdminModuleUpdateView, self).post(request, *args, **kwargs)

        # check to see that the module leader may have changed.
        current_module_leader = self.object.module_leader
        if current_module_leader != original_module_leader:
            # create watcher objects for module leaders
            original_ml_watcher = WatcherWrapper(original_module_leader)
            current_ml_watcher = WatcherWrapper(current_module_leader)

            # update module status by removing or adding module
            original_ml_watcher.remove_module(self.object)
            current_ml_watcher.add_module(self.object)

            # push the notification to the new module leader
            push_notification(
                'module_leader',
                module_code=self.object.module_code,
                module_leader=current_module_leader
            )
        # publish changes to timeline
        publish_changes(self.object, request.user)
        return response
示例#2
0
    def post(self, request, *args, **kwargs):
        """
        Post method to take the current entry and
        move it to the next status. If confirmed from
        being at status 'Staged', it will make the changes
        to the model.
        """
        entry_pk = kwargs['pk']

        # get the current timeline entry.
        entry = get_object_or_404(TimelineEntry, pk=entry_pk)

        if entry.status == 'Draft':
            entry.status = 'Staged'
        elif entry.status == 'Staged':  # pragma: no cover
            entry.status = 'Confirmed'

            # update the tracking form data
            module = Module.objects.get(module_code=entry.module_code)
            form = StagedTrackingFormWrapper(module)
            form.change_to_current()
        else:
            pass
        entry.approved_by = request.user
        entry.save()
        push_notification(entry.status, entry=entry, user=request.user)
        return redirect(self._get_url(**kwargs))
def push_mention_notifications(mentions, comment_author, entry):
    """
    Wrapper function that will handle mentions and notifications
    in one operation.

    Arguments:
        mentions            list of usernames, such as ['ryan', 'test']
                            or is a string, such as "@ryan hello world".

        comment_author      author that created the comment that will
                            trigger the notifications.

        entry               timeline entry for this comment

    Return:
        number of mentions it has seen
    """
    # only allow strings and lists to be processed
    if not isinstance(mentions, str) and not isinstance(mentions, list):
        raise ValueError("Expected a list or string for mentions")

    # ensure only users are processed
    if not isinstance(comment_author, User):
        raise ValueError("comment author expects a core:User type")

    # ensure only timeline entires are processed
    if not isinstance(entry, TimelineEntry):
        raise ValueError("entry expects TimelineEntry type")

    # if empty, automatically return o
    if len(mentions) < 1:
        return 0

    username = comment_author.username

    # if mentions is a string, then process to extract usernames
    if isinstance(mentions, str):
        # extract mentions from comment
        mentions = extract_mentions(mentions, username)

    # loop through each mention username and push
    # a notification to them.
    for m in mentions:
        try:
            user = User.objects.get(username=m)
            push_notification('mention',
                              author=comment_author,
                              entry=entry,
                              mention=user)
        except User.DoesNotExist:
            pass
    return len(mentions)
示例#4
0
    def get_success_url(self):
        # before sending success url, connect the user
        # to recieve notifiations
        obj = self.object
        publish_changes(obj, self.request.user)
        watcher = WatcherWrapper(obj.module_leader)

        # add the module created to thier list
        watcher.add_module(obj)

        # push notification to module leader
        push_notification(
            'module_leader',
            module_code=obj.module_code,
            module_leader=obj.module_leader
        )
        return reverse('all_modules')
示例#5
0
    def post(self, request, *args, **kwargs):
        """
        Post request to enable the rollback
        """
        entry_pk = kwargs['pk']

        entry = get_object_or_404(TimelineEntry, pk=entry_pk)
        if entry.status == 'Draft':
            # remove the changes as it is no longer needed
            revert_changes(entry)
            push_notification("cancelled", entry=entry, user=request.user)
        elif entry.status == 'Staged':
            # move the status back to 'Draft'
            entry.status = 'Draft'
            entry.save()
        else:
            pass
        return redirect(self._get_url(**kwargs))
    def __process_new_discussion(self, request, *args, **kwargs):
        comment = request.POST.get('comment', '')
        form = DiscussionForm({'comment': comment})
        discussion = {}
        if form.is_valid():
            # add the author and comments
            discussion['comment'] = form.cleaned_data['comment']
            discussion['author'] = request.user

            # get the timeline entry
            entry_id = kwargs['pk']
            discussion['entry'] = TimelineEntry.objects.get(pk=entry_id)

            # get the parent if one has been provided
            parent_id = request.POST.get('parent', None)
            if parent_id is not None:
                discussion['parent'] = Discussion.objects.get(pk=parent_id)

            # create the discussion
            discussion_obj = Discussion.objects.create(**discussion)

            # if thre is not parent id, then we push a discussion
            # notification.
            if parent_id is None:
                push_notification("discussion",
                                  discussion=discussion_obj,
                                  user=request.user)
            # otherwise, if there is a parent id, then it is a response
            else:
                push_notification("reply",
                                  discussion=discussion_obj,
                                  user=request.user,
                                  parent=discussion['parent'])

            # process any mentions that are in the comment
            # and send notifications to these users.
            push_mention_notifications(discussion['comment'],
                                       discussion['author'],
                                       discussion['entry'])
            return discussion_obj
        return None