Example #1
0
    def get(self, request, template_name="changesetreviews/ajax_changeset_reviews.html", *args, **kwargs):

        if not request.is_ajax():
            return HttpResponseForbidden(MSG_NOT_AJAX)

        data = {}
        try:
            if not request.user.is_authenticated():
                raise exceptions.Error("Login is required.")

            task_states = djcelery_models.TaskState.objects.filter(
                name="changesetreviews.tasks.review_changeset", state__in=states.UNREADY_STATES
            )
            task_state_list = []
            for task_state in task_states:
                ar = AsyncResult(task_state.task_id)
                result = ar.result
                if result and isinstance(result, dict) and "message" in result:
                    show_message = True
                else:
                    show_message = False
                kwargs_obj = {}
                if task_state.kwargs:
                    try:
                        kwargs_obj = yaml.load(task_state.kwargs)
                        if "changeset_pk" in kwargs_obj:
                            kwargs_obj["changeset_pk"] = long(kwargs_obj["changeset_pk"])
                    except:
                        pass
                changeset_id = kwargs_obj.get("changeset_pk")
                show_changeset_view_url = False
                if changeset_id:
                    show_changeset_view_url = changesets_models.Changeset.objects.filter(pk=changeset_id).exists()

                task_state_list.append(
                    dict(
                        task_id=task_state.task_id,
                        tstamp=djcelery_humanize.naturaldate(task_state.tstamp),
                        state=task_state.state,
                        params=task_state.kwargs,
                        result=result,
                        show_message=show_message,
                        changeset_id=changeset_id,
                        show_changeset_view_url=show_changeset_view_url,
                    )
                )

            data["html"] = render_to_string(template_name, locals(), context_instance=RequestContext(request))

            data_json = json.dumps(data)

        except Exception, e:
            msg = "ERROR %s: %s" % (type(e), e)
            log.exception(msg)
            data = dict(error=msg, html="")
            data_json = json.dumps(data)
Example #2
0
def ajax_changeset_applies(
        request, template='changesetapplies/ajax_changeset_applies.html'):
    """Ajax view for changeset applies."""

    if not request.is_ajax():
        return HttpResponseForbidden(MSG_NOT_AJAX)

    data = {}
    try:
        if not request.user.is_authenticated():
            raise exceptions.Error('Login is required.')

        request_id = request.GET.get('request_id')
        task_id = request.GET.get('task_id')
        task_ids = None
        if task_id:
            task_ids = [task_id]
        else:
            if request_id and request_id in request.session:
                task_ids = request.session[request_id]
        filter_kwargs = dict(name='changesetapplies.tasks.apply_changeset')
        if task_ids:
            filter_kwargs.update(dict(task_id__in=task_ids))
        task_states = djcelery_models.TaskState.objects.filter(
            **filter_kwargs)
        task_state_list = []
        for task_state in task_states:
            ar = AsyncResult(task_state.task_id)
            result = ar.result

            if result and isinstance(result, dict) and 'message' in result:
                show_message = True
            else:
                show_message = False

            changeset_id = None
            server = None
            changeset_detail_applies = []
            if result:
                changeset_id = result.get('changeset_id')
                server_id = result.get('server_id')
                changeset_detail_apply_ids = result.get(
                    'changeset_detail_apply_ids')
                if server_id:
                    server = servers_models.Server.objects.get(pk=server_id)
                if changeset_detail_apply_ids:
                    for id in changeset_detail_apply_ids:
                        try:
                            changeset_detail_applies.append(
                                models.ChangesetDetailApply.objects.get(pk=id))
                        except:
                            pass

            task_state_list.append(dict(
                task_id=task_state.task_id,
                tstamp=djcelery_humanize.naturaldate(task_state.tstamp),
                state=task_state.state,
                result=result,
                show_message=show_message,
                changeset_id=changeset_id,
                server=server,
                changeset_detail_applies=changeset_detail_applies
            ))

        data['html'] = render_to_string(
            template, locals(), context_instance=RequestContext(request))

        data_json = json.dumps(data)

    except Exception, e:
        msg = 'ERROR %s: %s' % (type(e), e)
        log.exception(msg)
        data = dict(error=msg, html='')
        data_json = json.dumps(data)
Example #3
0
def created_at(instance):
    return """<div title="%s">%s</div>""" % (
                escape(str(instance.created_at)),
                escape(naturaldate(instance.created_at)))