Add the ability to redirect to the resulting link, some styling
authorDavid Kerkeslager <kerkeslager@gmail.com>
Thu, 2 Sep 2021 17:40:28 +0000 (13:40 -0400)
committerDavid Kerkeslager <kerkeslager@gmail.com>
Thu, 2 Sep 2021 17:44:03 +0000 (13:44 -0400)
src/bigly/serializers.py
src/bigly/static/bigly/styles.css
src/bigly/templates/bigly/_base.html [new file with mode: 0644]
src/bigly/templates/bigly/index.html
src/bigly/templates/bigly/link_info.html
src/bigly/views.py

index 8a9a5c4..a601f10 100644 (file)
@@ -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',
+    )
index b1bc6cf..3a373d9 100644 (file)
@@ -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 (file)
index 0000000..13ccecd
--- /dev/null
@@ -0,0 +1,26 @@
+{% load static %}
+<!doctype html>
+<html lang='en'>
+  <head>
+    <title>bigly</title>
+
+    <meta charset='utf-8'/>
+    <meta name='viewport' content='width=device-width, initial-scale=1'/>
+    <meta name='description' content='A tool for unshortening links that have been shortened with a link shortener.'/>
+    <meta name='author' content='David Kerkeslager'/>
+
+    <link rel='stylesheet' href='{% static "bigly/styles.css" %}'/>
+  </head>
+
+  <body>
+    <main>
+      {% block body %}
+      {% endblock %}
+    </main>
+
+    <footer>
+      <section>&copy; {% now "Y" %} David Kerkeslager</section>
+      <section>All code for this site is released under the AGPL version 3.</section>
+    </footer>
+  </body>
+</html>
index 17bf9f7..960aaed 100644 (file)
@@ -1,30 +1,26 @@
+{% extends 'bigly/_base.html' %}
 {% load static %}
-<!doctype html>
-<html lang='en'>
-  <head>
-    <title>bigly</title>
 
-    <meta charset='utf-8'/>
-    <meta name='viewport' content='width=device-width, initial-scale=1'/>
-    <meta name='description' content='A tool for unshortening links that have been shortened with a link shortener.'/>
-    <meta name='author' content='David Kerkeslager'/>
+{% block body %}
+<h1>bigly</h1>
+<h2>make links big again</h2>
 
-    <link rel='stylesheet' href='{% static "bigly/styles.css" %}'/>
-  </head>
+<form action='{% url "embiggen" %}' method='get'>
+  <input type='url' name='link' autofocus></input>
+  <label for='link'>Link</label>
+  <span>
+    <input type='checkbox' name='remove_utm' checked></input>
+    <label for='remove_utm'>Remove UTM Parameters</label>
+  </span>
+  <span>
+    <input type='radio' name='handler' value='display' checked></input>
+    <label for='display'>Display link information</label>
+    <input type='radio' name='handler' value='redirect'></input>
+    <label for='redirect'>Redirect to link</label>
+  </span>
+  <input type='submit' value='Embiggen'></input>
+</form>
 
-  <body>
-    <h1>bigly</h1>
-    <h2>make links big again</h2>
-
-    <form action='{% url "embiggen" %}' method='get'>
-      <input type='url' name='link' autofocus></input>
-      <label for='link'>Link</label>
-      <input type='checkbox' name='remove_utm' checked></input>
-      <label for='remove_utm'>Remove UTM Parameters</label>
-      <input type='submit' value='Embiggen'></input>
-    </form>
-
-    <script src='{% static "bigly/scripts.js" %}'></script>
-  </body>
-</html>
+<script src='{% static "bigly/scripts.js" %}'></script>
+{% endblock %}
 
index bdd5265..4eb7d1f 100644 (file)
@@ -1,23 +1,20 @@
-{% load static %}
-<!doctype html>
-<html lang='en'>
-  <head>
-    <title>bigly</title>
+{% extends 'bigly/_base.html' %}
 
-    <meta charset='utf-8'/>
-    <meta name='viewport' content='width=device-width, initial-scale=1'/>
-    <meta name='description' content='A tool for unshortening links that have been shortened with a link shortener.'/>
-    <meta name='author' content='David Kerkeslager'/>
-
-    <link rel='stylesheet' href='{% static "bigly/styles.css" %}'/>
-  </head>
-
-  <body>
-    <table>
-      <tr>
-        <td>Link:</td>
-        <td><a href='{{ link }}'>{{ link }}</a></td>
-      </tr>
-    </table>
-  </body>
-</html>
+{% block body %}
+<table>
+  <tr>
+    <td>Status:</td>
+    {% if 200 <= status and status < 300 %}
+      <td style='color: green;'>{{ status }}</td>
+    {% elif 400 <= status and status < 600 %}
+      <td style='color: red;'>{{ status }}</td>
+    {% else %}
+      <td>{{ status }}</td>
+    {% endif %}
+  </tr>
+  <tr>
+    <td>Link:</td>
+    <td><a href='{{ link }}'>{{ link }}</a></td>
+  </tr>
+</table>
+{% endblock %}
index f88753a..4a3317c 100644 (file)
@@ -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