{% extends "base.html" %}
{% load cab_tags %}
{% load cache %}
{% load ratings_tags %}
{% load safe_markup %}
{% load url from future %}

{% block title %}{{ object.title }}{% endblock %}

{% block content_header %}{{ object.title }}{% endblock %}

{% block content %}
  {{ object.highlighted_code|safe }}
  <div>
    <h3>More like this</h3>
    {% cache 600 mlt object.pk %}
      <ol>
      {% for related in object|more_like_this:5 %}
      <li><a href="{{ related.object.get_absolute_url }}">{{ related.object.title }}</a> by <a href="{% url 'cab_author_snippets' username=related.object.author.username %}">{{ related.object.author.username }}</a>
        {{ related.object.pub_date|timesince }} ago
      </li>
      {% endfor %}
      </ol>
    {% endcache %}
  </div>
{% endblock %}

{% block sidebar %}
  {{ object.description_html|safe }}

  <dl>
    <dt>Author:</dt>
    <dd><a href="{% url 'cab_author_snippets' username=object.author.username %}">{{ object.author.username }}</a></dd>
    <dt>Posted:</dt>
    <dd>{{ object.pub_date|date:"F j, Y" }}</dd>
    <dt>Language:</dt>
    <dd><a href="{{ object.language.get_absolute_url }}">{{ object.language.name }}</a></dd>
    <dt>Version:</dt>
    <dd>{{ object.get_version }}</dd>
    <dt>Tags:</dt>
    <dd>{% for tag in object.tags.all %}<a href="{% url "cab_snippet_matches_tag" tag.slug %}">{{ tag.name }}</a> {% endfor %}</dd>
    <dt>Score:</dt>
    <dd>{{ object.rating_score }} (after {{ object.ratings.count }} ratings)</dd>
  </dl>

  <h3>Tools</h3>
  <ul>
    {% if user.is_authenticated %}
      {% ifequal user.id object.author.id %}
        <li><a href="{% url 'cab_snippet_edit' snippet_id=object.id %}">Edit this snippet</a></li>
      {% endifequal %}
      {% if object|is_bookmarked:request.user %}
        <li>This snippet is in <a href="{% url 'cab_user_bookmarks' %}">your bookmarks</a></li>
      {% else %}
        <li><a href="{% url 'cab_bookmark_add' snippet_id=object.id %}">Bookmark this snippet</a></li>
      {% endif %}
      {% with object|rating_score:request.user as rating_score %}
        {% if rating_score %}
          <li>You rated this snippet <strong>{% ifequal rating_score 1 %}useful{% else %}not useful{% endifequal %}</strong>. (<a href="{% url 'cab_snippet_rate' snippet_id=object.id %}?score=reset">Remove your vote</a>)</li>
        {% else %}
          {% url 'cab_snippet_rate' snippet_id=object.id as rate_url %}
          <li>Rate this snippet: <a href="{{ rate_url }}?score=up">useful</a> or <a href="{{ rate_url }}?score=down">not useful</a></li>
        {% endif %}
      {% endwith %}
    {% endif %}
    <li><a href="{% url 'cab_snippet_download' snippet_id=object.id %}" type="{{ object.language.mime_type }}">Download this snippet</a></li>
    <li><a href="#comments">Comment on this snippet</a></li>
    {% if user.is_authenticated %}
      <li>
        {% if not user|has_flagged:object %}
        <form method="post" class="flag_form" action="{% url "cab_snippet_flag" object.id %}">{% csrf_token %}
          {{ flag_form.flag }}
          <input type="submit" value="Flag" />
        </form>
        {% endif %}
      </li>
    {% endif %}
  </ul>
{% endblock %}

{% block extra_content %}
  <div id="comments">
    <h2>Comments</h2>
    {% load comments %}
    {% get_comment_list for object as comment_list %}
    <dl>
    {% for comment in comment_list %}
      <dt id="c{{ comment.id }}"><a href="{% url 'cab_author_snippets' username=comment.user.username %}">{{ comment.user.username }}</a> (on {{ comment.submit_date|date:"F j, Y" }}):</dt>
      <dd>{{ comment.comment|safe_markdown:"safe"|safe }}
      <p><a href="{{ comment.get_absolute_url }}">#</a></p></dd>
    {% endfor %}
    </dl>
    {% render_comment_form for object %}
  </div>
{% endblock %}
