From 59c710f97ad48c2b57ac8711ee30cbe7438810e8 Mon Sep 17 00:00:00 2001 From: David Kerkeslager Date: Thu, 2 Sep 2021 13:40:28 -0400 Subject: [PATCH 1/1] Add the ability to redirect to the resulting link, some styling --- src/bigly/serializers.py | 22 ++++++++++++ src/bigly/static/bigly/styles.css | 15 ++++++++ src/bigly/templates/bigly/_base.html | 26 ++++++++++++++ src/bigly/templates/bigly/index.html | 46 +++++++++++------------- src/bigly/templates/bigly/link_info.html | 41 ++++++++++----------- src/bigly/views.py | 16 +++++---- 6 files changed, 113 insertions(+), 53 deletions(-) create mode 100644 src/bigly/templates/bigly/_base.html diff --git a/src/bigly/serializers.py b/src/bigly/serializers.py index 8a9a5c4..a601f10 100644 --- a/src/bigly/serializers.py +++ b/src/bigly/serializers.py @@ -1,5 +1,27 @@ from rest_framework import serializers +class ChoiceField(serializers.CharField): + def __init__(self, *args, **kwargs): + sentinel = object() + self._choices = kwargs.pop('choices', sentinel) + if self._choices is sentinel: + raise Exception('Choices is required') + else: + self._choices = set(self._choices) + super().__init__(self, *args, **kwargs) + + def to_internal_value(self, data): + if data not in choices: + raise serializers.ValidationError('{} not in choices {}'.format( + data, + self._choices, + )) + return data + class FollowRedirectsSerializer(serializers.Serializer): link = serializers.URLField(required=True) remove_utm = serializers.BooleanField(required=False) + handler = serializers.ChoiceField( + choices=['display', 'redirect'], + default='display', + ) diff --git a/src/bigly/static/bigly/styles.css b/src/bigly/static/bigly/styles.css index b1bc6cf..3a373d9 100644 --- a/src/bigly/static/bigly/styles.css +++ b/src/bigly/static/bigly/styles.css @@ -22,6 +22,12 @@ body { margin: 3em auto; } +main { + display: flex; + flex-direction: column; + align-items: center; +} + h1 { font-size: 12vw; text-align: center; @@ -59,3 +65,12 @@ form label { font-size: 12pt; width: 100%; } + +footer { + margin: 1rem 0 0 0; + text-align: center; +} + +footer section:not(:first-child) { + margin-top: 1em; +} diff --git a/src/bigly/templates/bigly/_base.html b/src/bigly/templates/bigly/_base.html new file mode 100644 index 0000000..13ccecd --- /dev/null +++ b/src/bigly/templates/bigly/_base.html @@ -0,0 +1,26 @@ +{% load static %} + + + + bigly + + + + + + + + + + +
+ {% block body %} + {% endblock %} +
+ + + + diff --git a/src/bigly/templates/bigly/index.html b/src/bigly/templates/bigly/index.html index 17bf9f7..960aaed 100644 --- a/src/bigly/templates/bigly/index.html +++ b/src/bigly/templates/bigly/index.html @@ -1,30 +1,26 @@ +{% extends 'bigly/_base.html' %} {% load static %} - - - - bigly - - - - +{% block body %} +

bigly

+

make links big again

- - +
+ + + + + + + + + + + + + +
- -

bigly

-

make links big again

- -
- - - - - -
- - - - + +{% endblock %} diff --git a/src/bigly/templates/bigly/link_info.html b/src/bigly/templates/bigly/link_info.html index bdd5265..4eb7d1f 100644 --- a/src/bigly/templates/bigly/link_info.html +++ b/src/bigly/templates/bigly/link_info.html @@ -1,23 +1,20 @@ -{% load static %} - - - - bigly +{% extends 'bigly/_base.html' %} - - - - - - - - - - - - - - -
Link:{{ link }}
- - +{% block body %} + + + + {% if 200 <= status and status < 300 %} + + {% elif 400 <= status and status < 600 %} + + {% else %} + + {% endif %} + + + + + +
Status:{{ status }}{{ status }}{{ status }}
Link:{{ link }}
+{% endblock %} diff --git a/src/bigly/views.py b/src/bigly/views.py index f88753a..4a3317c 100644 --- a/src/bigly/views.py +++ b/src/bigly/views.py @@ -1,6 +1,6 @@ from urllib.parse import urlparse, urlunparse, parse_qs -from django.shortcuts import render +from django.shortcuts import redirect, render from django.views.generic.base import TemplateView from rest_framework import status, viewsets @@ -78,11 +78,15 @@ def embiggen(request): remove_utm = serializer.data['remove_utm'], ) - return render( - request, - 'bigly/link_info.html', - result, - ) + if serializer.data['handler'] == 'redirect': + return redirect(result['link']) + + else: + return render( + request, + 'bigly/link_info.html', + result, + ) class FollowRedirectsViewSet(viewsets.ViewSet): serializer_class = serializers.FollowRedirectsSerializer -- 2.20.1