Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
#17 display header in column (#46)
Browse files Browse the repository at this point in the history
* added absolute templatetag

* added request url to stub edit page

* added str_to_list filter for template

* headers in column

* linted and added test for filter

* added change_form_template for every Admin and refactor templates

* added change_form_template attribute

* changed local host in .sh for dev

* str_to_list renamed to headers_to_list

* tranclate to copied

* corrected start-django.dev

* refactored headers_to_list: added json.loads

* fixed tests and linted

* added test for ivalid headers

* corrected test

* replaced spike-nail for pretty_body to filedset_log_entry template
  • Loading branch information
PetrS12 authored Dec 13, 2020
1 parent d780eec commit e5d9672
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 39 deletions.
1 change: 1 addition & 0 deletions http_stubs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def pretty_body(self, instance) -> str:
class HTTPStubAdmin(admin.ModelAdmin):
"""HTTP stub admin."""

change_form_template = 'admin/http_stubs/httpstub/change_form.html'
extra_buttons_style = 'background-color:#00b0ff;color:white'

actions = ['enable_action', 'disable_action']
Expand Down
26 changes: 25 additions & 1 deletion http_stubs/templatetags/stub_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from typing import Dict
import json
from html import unescape
from json import JSONDecodeError
from typing import AnyStr, Dict, List

from django import template
from django.template.defaultfilters import stringfilter

Url = str

Expand All @@ -16,3 +20,23 @@ def get_absolute_url_tag(context: Dict, url: Url) -> Url:
:returns: absolute url
"""
return context.get('request').build_absolute_uri(url)


@register.filter()
@stringfilter
def headers_to_list(headers: AnyStr) -> List:
"""Filter that creates a list from a string of headers(dict).
Return a list of the lines in the string representation of the request
headers.
:param headers: string representation of the request headers
:returns: list of individual headers string representation
"""
try:
headers = json.loads(unescape(headers).replace("'", '"'))
except (TypeError, JSONDecodeError):
return []
return [
f'{header}: {header_value}'
for header, header_value in headers.items()
]
16 changes: 14 additions & 2 deletions http_stubs/tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.http.request import HttpRequest

from http_stubs.templatetags.stub_tags import get_absolute_url_tag
from http_stubs.templatetags import stub_tags


class TestStubTags:
Expand All @@ -14,5 +14,17 @@ def test_absolute_url_tag(self):
'SERVER_PORT': '80',
}
relative_url = '/check/'
url = get_absolute_url_tag({'request': req}, relative_url)
url = stub_tags.get_absolute_url_tag({'request': req}, relative_url)
assert url == 'http://127.0.0.1/check/'

def test_headers_to_list_filter(self):
"""Check that the list of headers is returned."""
headers = '{"Content-Length": "2", "Accept": "text/html"}'
headers_list = stub_tags.headers_to_list(headers)
assert headers_list == ['Content-Length: 2', 'Accept: text/html']

def test_headers_to_list_filter_invalid(self):
"""Check that the list of headers is empty."""
headers = 'invalid headers format'
headers_list = stub_tags.headers_to_list(headers)
assert not headers_list
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% extends "admin/change_form.html" %}
{% block field_sets %}
{% for fieldset in adminform %}
{% include "admin/http_stubs/includes/fieldset.html" %}
{% include "admin/http_stubs/includes/fieldset_http_stub.html" %}
{% endfor %}
{% endblock %}
46 changes: 11 additions & 35 deletions templates/admin/http_stubs/includes/fieldset.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load stub_tags %}
<fieldset class="module aligned {{ fieldset.classes }}">
<legend></legend>
<legend></legend>
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}
<div class="description">{{ fieldset.description|safe }}</div>
Expand All @@ -16,19 +16,13 @@
{% else %}
{{ field.label_tag }}
{% if field.is_readonly %}
<div class="readonly">
{% if field.field.name == 'pretty_body' %}
<pre>{{ field.contents }}</pre>
{% elif field.field.name == 'body' %}
<div style="overflow:auto;width:85vw;">{{ field.contents }}</div>
{% else %}
{{ field.contents }}
{% endif %}
</div>
{% block readonly_field %}
<div class="readonly">{{ field.contents }}</div>
{% endblock %}
{% else %}
{{ field.field }}
{# added request url for path field(issue #35) #}
{% if field.field.name == 'path' %}<div class="readonly"><div id="full-path">{% absolute field.field.value %}</div><div id="path-copied-alert">Copied!</div></div>{% endif %}
{% block writable_field %}
{{ field.field }}
{% endblock %}
{% endif %}
{% endif %}
{% if field.field.help_text %}
Expand Down Expand Up @@ -201,32 +195,14 @@
}
}
ImagePreview.init({id:$("#imgBox img")});


// copy content to clipboard
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}

// copy full request path to clipboard
$("#full-path").on("click", function() {
copyToClipboard("#full-path");
$("#path-copied-alert").fadeIn("slow");
});
</script>

{% block javascripts %}
{% endblock %}

<style type="text/css">
#imgs img {
width: 24%;
}
#path-copied-alert {
display: none;
color: green;
padding-left: 170px;
}
</style>
{% block css %}
{% endblock %}
40 changes: 40 additions & 0 deletions templates/admin/http_stubs/includes/fieldset_http_stub.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% extends 'admin/http_stubs/includes/fieldset.html' %}
{% load stub_tags %}
{% block writable_field %}
{{ field.field }}
{# added request url for path field(issue #35) #}
{% if field.field.name == 'path' %}
<div class="readonly">
<div id="full-path">{% absolute field.field.value %}</div>
<div id="path-copied-alert">Copied!</div>
</div>
{% endif %}
{% endblock %}
{% block javascripts %}
<script type="text/javascript">
// copy content to clipboard
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}

// copy full request path to clipboard
$("#full-path").on("click", function() {
copyToClipboard("#full-path");
$("#path-copied-alert").fadeIn("slow");
});

</script>
{% endblock %}
{% block css %}
<style type="text/css">
#path-copied-alert {
display: none;
color: green;
padding-left: 170px;
}
</style>
{% endblock %}
19 changes: 19 additions & 0 deletions templates/admin/http_stubs/includes/fieldset_log_entry.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'admin/http_stubs/includes/fieldset.html' %}
{% load stub_tags %}
{% block readonly_field %}
{% if field.field.name == 'headers' %}
<ul class="readonly">
{% for header in field.contents|headers_to_list %}
<li>{{ header }}</li>
{% endfor %}</ul>
{% elif field.field.name == 'pretty_body' %}
<div class="readonly">
<pre>{{ field.contents }}</pre>
</div>
{% elif field.field.name == 'body' %}
<div class="readonly">
<div style="overflow:auto;width:85vw;">{{ field.contents }}</div>
</div>
{% else %}
<div class="readonly">{{ field.contents }}</div>{% endif %}
{% endblock %}
6 changes: 6 additions & 0 deletions templates/admin/http_stubs/logentry/change_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% extends "admin/change_form.html" %}
{% block field_sets %}
{% for fieldset in adminform %}
{% include "admin/http_stubs/includes/fieldset_log_entry.html" %}
{% endfor %}
{% endblock %}

0 comments on commit e5d9672

Please sign in to comment.