{% extends "admin/base_site.html" %}

{% load static django_rq %}

{% block title %}Job {{ job.id }} {{ block.super }}{% endblock %}

{% block extrastyle %}
    {{ block.super }}
    <style>
        .data {
            display: inline-block;
            float: left;
            width: 80%;
            font-size: 12px;
            padding-top: 3px;
        }
    </style>
    <link href="{% static 'admin/css/forms.css' %}" type="text/css" rel="stylesheet">
{% endblock %}

{% block breadcrumbs %}
    <div class="breadcrumbs">
        <a href="{% url 'admin:index' %}">Home</a> &rsaquo;
        <a href="{% url 'rq_home' %}">Django RQ</a> &rsaquo;
        <a href = "{% url 'rq_jobs' queue_index %}">{{ queue.name }}</a> &rsaquo;
        <a href = "{% url 'rq_job_detail' queue_index job.id %}">{{ job.id }}</a>
    </div>
{% endblock %}

{% block content_title %}<h1>Job Info</h1>{% endblock %}

{% block content %}

<div id="content-main">

    <fieldset class="module aligned ">

        <div class="form-row">
            <div>
                <label class="required">ID:</label>
                <div class="data">{{ job.id }}</div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Queue:</label>
                <div class="data">{{ job.origin }}</div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Timeout:</label>
                <div class="data">{{ job.timeout }}</div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Result TTL:</label>
                <div class="data">{{ job.result_ttl }}</div>
            </div>
        </div>

        {% if job.created_at %}
        <div class="form-row">
            <div>
                <label class="required">Created:</label>
                <div class="data">{{ job.created_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
            </div>
        </div>
        {% endif %}

        {% if job.enqueued_at %}
        <div class="form-row">
            <div>
                <label class="required">Queued:</label>
                <div class="data">{{ job.enqueued_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
            </div>
        </div>
        {% endif %}

        {% if job.started_at %}
        <div class="form-row">
            <div>
                <label class="required">Started:</label>
                <div class="data">{{ job.started_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
            </div>
        </div>
        {% endif %}

        {% if job.ended_at %}
        <div class="form-row">
            <div>
                <label class="required">Ended:</label>
                <div class="data">{{ job.ended_at|to_localtime|date:"Y-m-d, H:i:s" }}</div>
            </div>
        </div>
        {% endif %}

        <div class="form-row">
            <div>
                <label class="required">Status:</label>
                <div class="data">{{ job.get_status }}</div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Callable:</label>
                <div class="data">{{ job|show_func_name }}</div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Meta:</label>
                <div class="data">{{ job.meta }}</div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Args:</label>
                <div class="data">
                    {% if data_is_valid %}
                        {% if job.args %}
                          <ul>
                              {% for arg in job.args %}
                                  <li>{{ arg|force_escape }}</li>
                              {% endfor %}
                          </ul>
                        {% endif %}
                    {% else %}
                        Unpickling Error
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Kwargs:</label>
                <div class="data">
                    {% if data_is_valid %}
                        {% if job.kwargs %}
                            <ul>
                                {% for key, value in job.kwargs.items %}
                                    <li>{{ key }}: {{ value|force_escape }}</li>
                                {% endfor %}
                            </ul>
                        {% endif %}
                    {% else %}
                        Unpickling Error
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Depends On:</label>
                <div class="data">
                    {% if dependency_id %}
                    <a href = "{% url 'rq_job_detail' queue_index dependency_id %}">
                        {{ dependency_id }}
                    </a>
                    {% endif %}
                </div>
            </div>
        </div>


        <div class="form-row">
            <div>
                <label class="required">Exception:</label>
                <div class="data"><pre>{% if job.exc_info %}{{ job.exc_info|linebreaks }}{% endif %}</pre></div>
            </div>
        </div>

        <div class="form-row">
            <div>
                <label class="required">Result:</label>
                <div class="data">{{ job.result }}</div>
            </div>
        </div>

    </fieldset>

    <div class="submit-row">
        <p class="deletelink-box"><a href="delete/" class="deletelink">Delete</a></p>
        {% if job.is_failed %}
            <form method = 'POST' action = "{% url 'rq_requeue_job' queue_index job.id %}">
                {% csrf_token %}
                <input type="submit" value="Requeue" class="default" name="requeue">
            </form>
        {% endif %}
        {% if not job.is_queued and not job.is_failed %}
            <form method = 'POST' action = "{% url 'rq_enqueue_job' queue_index job.id %}">
                {% csrf_token %}
                <input type="submit" value="Enqueue" class="default" name="Enqueue">
            </form>
        {% endif %}
    </div>

</div>

{% endblock %}
