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',
+ )
margin: 3em auto;
}
+main {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+}
+
h1 {
font-size: 12vw;
text-align: center;
font-size: 12pt;
width: 100%;
}
+
+footer {
+ margin: 1rem 0 0 0;
+ text-align: center;
+}
+
+footer section:not(:first-child) {
+ margin-top: 1em;
+}
--- /dev/null
+{% 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>© {% now "Y" %} David Kerkeslager</section>
+ <section>All code for this site is released under the AGPL version 3.</section>
+ </footer>
+ </body>
+</html>
+{% 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 %}
-{% 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 %}
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
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