Contextual date improvements
This commit is contained in:
		
							parent
							
								
									2b003b8fa9
								
							
						
					
					
						commit
						a6a508899a
					
				@ -5,16 +5,14 @@ from django.utils import timezone
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DateRangeMixin:
 | 
			
		||||
    def get_start_date(self, use_default=True):
 | 
			
		||||
    def get_start_date(self):
 | 
			
		||||
        if self.request.GET.get("startDate") != None:
 | 
			
		||||
            found_time = timezone.datetime.strptime(
 | 
			
		||||
                self.request.GET.get("startDate"), "%Y-%m-%d"
 | 
			
		||||
            )
 | 
			
		||||
            return timezone.make_aware(datetime.combine(found_time, time.min))
 | 
			
		||||
        elif use_default == True:
 | 
			
		||||
            return timezone.now() - timezone.timedelta(days=30)
 | 
			
		||||
        else:
 | 
			
		||||
            return None
 | 
			
		||||
            return timezone.now() - timezone.timedelta(days=30)
 | 
			
		||||
 | 
			
		||||
    def get_end_date(self):
 | 
			
		||||
        if self.request.GET.get("endDate") != None:
 | 
			
		||||
@ -29,6 +27,4 @@ class DateRangeMixin:
 | 
			
		||||
        data = super().get_context_data(**kwargs)
 | 
			
		||||
        data["start_date"] = self.get_start_date()
 | 
			
		||||
        data["end_date"] = self.get_end_date()
 | 
			
		||||
        start_date = self.get_start_date(False)
 | 
			
		||||
        data["date_query_params"] = f"?startDate={start_date.strftime('%Y-%m-%d')}&endDate={self.get_end_date().strftime('%Y-%m-%d')}" if start_date is not None else ""
 | 
			
		||||
        return data
 | 
			
		||||
 | 
			
		||||
@ -41,10 +41,8 @@
 | 
			
		||||
        <p class="ml-2 mb-1 supra font-medium text-gray-500 pointer-events-none">Services</p>
 | 
			
		||||
 | 
			
		||||
        {% for service in user.owning_services.all %}
 | 
			
		||||
        {% url 'dashboard:service' service.uuid as url %}
 | 
			
		||||
        {% with url_full=date_query_params|default:''|prepend_string:url %}
 | 
			
		||||
        {% include 'dashboard/includes/sidebar_portal.html' with label=service.name|truncatechars:16 url=url_full icon=service.link|iconify %}
 | 
			
		||||
        {% endwith %}
 | 
			
		||||
        {% contextual_url 'dashboard:service' service.uuid as url %}
 | 
			
		||||
        {% include 'dashboard/includes/sidebar_portal.html' with label=service.name|truncatechars:16 url=url icon=service.link|iconify %}
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
 | 
			
		||||
        {% endif %}
 | 
			
		||||
@ -62,7 +60,7 @@
 | 
			
		||||
        <p class="ml-2 mb-1 supra font-medium text-gray-500 pointer-events-none">Collaborations</p>
 | 
			
		||||
 | 
			
		||||
        {% for service in user.collaborating_services.all %}
 | 
			
		||||
        {% url 'dashboard:service' service.uuid as url %}
 | 
			
		||||
        {% contextual_url 'dashboard:service' service.uuid as url %}
 | 
			
		||||
        {% include 'dashboard/includes/sidebar_portal.html' with label=service.name|truncatechars:20 url=url %}
 | 
			
		||||
        {% endfor %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -2,7 +2,7 @@
 | 
			
		||||
    <input type="hidden" name="startDate" value="{{start_date.isoformat}}" id="startDate">
 | 
			
		||||
    <input type="hidden" name="endDate" value="{{end_date.isoformat}}" id="endDate">
 | 
			
		||||
</form>
 | 
			
		||||
<input type="input" id="rangePicker" placeholder="Date range" class="input ~neutral bg-neutral-000 cursor-pointer" readonly>
 | 
			
		||||
<input type="input" id="rangePicker" placeholder="Date range" class="input ~neutral bg-neutral-000 cursor-pointer w-auto" readonly>
 | 
			
		||||
<style>
 | 
			
		||||
    :root {
 | 
			
		||||
        --litepickerMonthButtonHover: var(--color-urge);
 | 
			
		||||
@ -18,7 +18,7 @@
 | 
			
		||||
    var picker = new Litepicker({
 | 
			
		||||
        element: document.getElementById('rangePicker'),
 | 
			
		||||
        singleMode: false,
 | 
			
		||||
        format: 'MMM D, YYYY',
 | 
			
		||||
        format: "MMM DD 'YY",
 | 
			
		||||
        maxDate: new Date(),
 | 
			
		||||
        startDate: Date.parse(document.getElementById("startDate").getAttribute("value")),
 | 
			
		||||
        endDate: Date.parse(document.getElementById("endDate").getAttribute("value")),
 | 
			
		||||
 | 
			
		||||
@ -1,6 +1,6 @@
 | 
			
		||||
{% load humanize helpers %}
 | 
			
		||||
 | 
			
		||||
<a class="card ~neutral !low service mb-6 p-0" href="{% url 'dashboard:service' object.uuid %}{{date_query_params}}">
 | 
			
		||||
<a class="card ~neutral !low service mb-6 p-0" href="{% contextual_url 'dashboard:service' object.uuid %}">
 | 
			
		||||
    {% with stats=object.stats %}
 | 
			
		||||
    <div class="p-4 md:flex justify-between">
 | 
			
		||||
        <div class="flex items-center mb-4 md:mb-0">
 | 
			
		||||
 | 
			
		||||
@ -12,7 +12,7 @@
 | 
			
		||||
        {% for session in object_list %}
 | 
			
		||||
        <tr>
 | 
			
		||||
            <td>
 | 
			
		||||
                <a href="{% url 'dashboard:service_session' object.pk session.pk %}{{date_query_params}}"
 | 
			
		||||
                <a href="{% contextual_url 'dashboard:service_session' object.pk session.pk %}"
 | 
			
		||||
                    class="font-medium text-urge-700">
 | 
			
		||||
                    {{session.start_time|date:"M j Y, g:i a"|capfirst}}
 | 
			
		||||
                    {% if session.is_currently_active %}
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@
 | 
			
		||||
<div class="mr-2">{% include 'dashboard/includes/date_range.html' %}</div>
 | 
			
		||||
{% has_perm 'core.change_service' user object as can_update %}
 | 
			
		||||
{% if can_update %}
 | 
			
		||||
<a href="{% url 'dashboard:service_update' service.uuid %}" class="button field bg-neutral-000 w-auto">Manage →</a>
 | 
			
		||||
<a href="{% contextual_url 'dashboard:service_update' service.uuid %}" class="button field bg-neutral-000 w-auto">Manage →</a>
 | 
			
		||||
{% endif %}
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
@ -235,7 +235,7 @@
 | 
			
		||||
<div class="card ~neutral !low limited-height py-2">
 | 
			
		||||
    {% include 'dashboard/includes/session_list.html' %}
 | 
			
		||||
    <hr class="sep h-8">
 | 
			
		||||
    <a href="{% url 'dashboard:service_session_list' service.uuid %}{{date_query_params}}" class="button ~neutral w-auto mb-2">View more
 | 
			
		||||
    <a href="{% contextual_url 'dashboard:service_session_list' service.uuid %}" class="button ~neutral w-auto mb-2">View more
 | 
			
		||||
        sessions
 | 
			
		||||
        →</a>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
{% block head_title %}{{object.name}} Session{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block service_actions %}
 | 
			
		||||
<a href="{% url 'dashboard:service' object.uuid %}" class="button field bg-neutral-000 w-auto">Analytics →</a>
 | 
			
		||||
<a href="{% contextual_url 'dashboard:service' object.uuid %}" class="button field bg-neutral-000 w-auto">Analytics →</a>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block service_content %}
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@
 | 
			
		||||
 | 
			
		||||
{% block service_actions %}
 | 
			
		||||
<div class="mr-2">{% include 'dashboard/includes/date_range.html' %}</div>
 | 
			
		||||
<a href="{% url 'dashboard:service' object.uuid %}{{date_query_params}}" class="button field ~neutral bg-neutral-000 w-auto">Analytics →</a>
 | 
			
		||||
<a href="{% contextual_url 'dashboard:service' object.uuid %}" class="button field ~neutral bg-neutral-000 w-auto">Analytics →</a>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
{% block service_content %}
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@
 | 
			
		||||
 | 
			
		||||
{% block content %}
 | 
			
		||||
<div class="md:flex justify-between items-center" id="heading">
 | 
			
		||||
    <a class="flex items-center mb-4 md:mb-0" href="{% url 'dashboard:service' object.uuid %}{{date_query_params}}">
 | 
			
		||||
    <a class="flex items-center mb-4 md:mb-0" href="{% contextual_url 'dashboard:service' object.uuid %}">
 | 
			
		||||
        <h3 class="heading leading-none mr-4">
 | 
			
		||||
            {{object.link|iconify}}
 | 
			
		||||
            {{object.name}}
 | 
			
		||||
 | 
			
		||||
@ -1,5 +1,6 @@
 | 
			
		||||
from datetime import timedelta
 | 
			
		||||
from urllib.parse import urlparse
 | 
			
		||||
import urllib
 | 
			
		||||
 | 
			
		||||
import flag
 | 
			
		||||
import pycountry
 | 
			
		||||
@ -8,6 +9,7 @@ from django.conf import settings
 | 
			
		||||
from django.utils import timezone
 | 
			
		||||
from django.utils.html import escape
 | 
			
		||||
from django.utils.safestring import SafeString
 | 
			
		||||
from django.template.defaulttags import url as url_tag
 | 
			
		||||
 | 
			
		||||
register = template.Library()
 | 
			
		||||
 | 
			
		||||
@ -185,6 +187,42 @@ def urldisplay(url):
 | 
			
		||||
    else:
 | 
			
		||||
        return url
 | 
			
		||||
 | 
			
		||||
@register.filter
 | 
			
		||||
def prepend_string(arg1, arg2):
 | 
			
		||||
    return f"{str(arg2)}{str(arg1)}"
 | 
			
		||||
class ContextualURLNode(template.Node):
 | 
			
		||||
    """Extension of the Django URLNode to support including contextual parameters in URL outputs. In other words, URLs generated will keep the start and end date parameters."""
 | 
			
		||||
 | 
			
		||||
    CONTEXT_PARAMS = ["startDate", "endDate"]
 | 
			
		||||
 | 
			
		||||
    def __init__(self, urlnode):
 | 
			
		||||
        self.urlnode = urlnode
 | 
			
		||||
 | 
			
		||||
    def __repr__(self):
 | 
			
		||||
        return self.urlnode.__repr__()
 | 
			
		||||
 | 
			
		||||
    def render(self, context):
 | 
			
		||||
        url = self.urlnode.render(context)
 | 
			
		||||
        if self.urlnode.asvar:
 | 
			
		||||
            url = context[self.urlnode.asvar]
 | 
			
		||||
 | 
			
		||||
        url_parts = list(urlparse(url))
 | 
			
		||||
        query = dict(urllib.parse.parse_qsl(url_parts[4]))
 | 
			
		||||
 | 
			
		||||
        query.update({
 | 
			
		||||
            param: context.request.GET.get(param) for param in self.CONTEXT_PARAMS if param in context.request.GET and param not in query
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
        url_parts[4] = urllib.parse.urlencode(query)
 | 
			
		||||
 | 
			
		||||
        url_final = urllib.parse.urlunparse(url_parts)
 | 
			
		||||
 | 
			
		||||
        if self.urlnode.asvar:
 | 
			
		||||
            context[self.urlnode.asvar] = url_final
 | 
			
		||||
            return ''
 | 
			
		||||
        else:
 | 
			
		||||
            return url_final
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register.tag
 | 
			
		||||
def contextual_url(*args, **kwargs):
 | 
			
		||||
    urlnode = url_tag(*args, **kwargs)
 | 
			
		||||
    return ContextualURLNode(urlnode)
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user