This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
issue-77: Fix absolute request url in HttpStubAdmin with regex(#77) #78
Merged
Merged
Changes from 20 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
495c7e9
added absolute templatetag
PetrS12 38225e4
added request url to stub edit page
PetrS12 8779577
added str_to_list filter for template
PetrS12 11d0672
headers in column
PetrS12 b061787
Merge pull request #3 from Uma-Tech/develop
PetrS12 b79e1a3
Merge branch 'develop' into #17-display_header_in_column
PetrS12 629041a
linted and added test for filter
PetrS12 f91a730
added change_form_template for every Admin and refactor templates
PetrS12 ddc7213
added change_form_template attribute
PetrS12 5be013d
changed local host in .sh for dev
PetrS12 867ae5e
str_to_list renamed to headers_to_list
PetrS12 4d8413c
Merge branch 'develop' of https://github.com/Uma-Tech/parrot into dev…
PetrS12 1ad82e3
Merge branch 'develop' into #17-display_header_in_column
PetrS12 27bfe76
tranclate to copied
PetrS12 5427684
corrected start-django.dev
PetrS12 4a9c169
refactored headers_to_list: added json.loads
PetrS12 61ab187
fixed tests and linted
PetrS12 184b24c
added test for ivalid headers
PetrS12 ab1aeae
corrected test
PetrS12 832fa68
Merge branch 'develop' of https://github.com/Uma-Tech/parrot into dev…
PetrS12 3f50a7a
fixed absolute url for stub admin page
PetrS12 2ab94a7
corrected name of function and doctring
PetrS12 c31ea9c
linted
PetrS12 ff604a9
renamed get_absolute_url to absolute_url
PetrS12 ca1945b
Merge branch 'develop' of https://github.com/Uma-Tech/parrot into dev…
PetrS12 9d21fa5
Merge branch 'develop' into issue-77
PetrS12 e0bd386
Merge branch 'develop' of https://github.com/Uma-Tech/parrot into dev…
PetrS12 1af158d
Merge branch 'develop' of https://github.com/Uma-Tech/parrot into dev…
PetrS12 71b07f7
Merge branch 'develop' into issue-77
PetrS12 cbc8aee
linted
PetrS12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,52 @@ | ||
from typing import Dict | ||
import json | ||
from html import unescape | ||
from json import JSONDecodeError | ||
from typing import AnyStr, Dict, List | ||
from urllib import parse | ||
|
||
from django import template | ||
from django.contrib.admin.helpers import Fieldset | ||
from django.template.defaultfilters import stringfilter | ||
|
||
Url = str | ||
|
||
register = template.Library() | ||
|
||
|
||
@register.simple_tag(takes_context=True, name='absolute') | ||
def get_absolute_url_tag(context: Dict, url: Url) -> Url: | ||
def get_absolute_url_tag(context: Dict, url: Url, fieldset: Fieldset) -> Url: | ||
"""Tag that returns an absolute url. | ||
|
||
:param context: context of request | ||
:param url: relative url | ||
:param fieldset: Fieldset of Django | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Such description is opaque to the reader. It is obvious that it would be a django fieldset, but reader would need information why is fieldset needed, when it should be passed. Correct description should be along these lines:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed. Thanks! |
||
:returns: absolute url | ||
""" | ||
return context.get('request').build_absolute_uri(url) | ||
request = context['request'] | ||
form = fieldset.form | ||
if form.initial.get('regex_path'): | ||
return parse.urlunparse( | ||
[request.scheme, request.get_host(), '', '', '', '']) | ||
if not url.startswith('/'): | ||
url = f'/{url}' | ||
return 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() | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
templates/admin/http_stubs/change_form.html → ...dmin/http_stubs/httpstub/change_form.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
templates/admin/http_stubs/includes/fieldset_http_stub.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 fieldset %}</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 %} |
11 changes: 11 additions & 0 deletions
11
templates/admin/http_stubs/includes/fieldset_log_entry.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{% 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> | ||
{% else %} | ||
<div class="readonly">{{ field.contents }}</div>{% endif %} | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
get_absolute_url_tag
seems too verbose. It consumes 25% of our line length allowance. I suggest one of the variants:get_absolute_url
absolute_url_tag
absolute_url
That are just as readable but more concise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed. Thanks!