summaryrefslogtreecommitdiff
path: root/docs/url_shortener.rst
blob: 47b29c5410793d08c1ef0676b53a664f65b8fbeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
=============
URL Shortener
=============

.. module:: gstudio.url_shortener

.. versionadded:: 0.9

The URL shortening has becoming a big deal of the Internet especially for
transfering long URLs.

And so many URL shortening services exist, each with his own features.

Originally Gstudio provided a only way to generate short URLs for your
entries, and you needed to install :ref:`django-bitly<gstudio-bitly>`.

One way it's not bad, but it's not enough.

First of all Gstudio now provides his own short URLs for the entries,
exemple:

  http://mydomain.com/blog/1/

Of course the URL is short (and can be shorter) but if you have a long
domain, the URL can be not so short, exemple:

  http://mysuperverylongdomain.com/blog/1/ (40 characters !)

But now you can easily change this behavior and use your favorite URL
shortener service by writing a backend shortening your URLs.

.. _writing-url-shortener:

Writing your own URL shortener backend
======================================

Writing a backend for using your custom URL shortener is simple as
possible, you only needs to follows 4 rules.

#. In a new Python file write a function named **backend** taking an
   :class:`~gstudio.models.Entry` instance in parameters.

#. The **backend** function should returns an URL including the protocol
   and the domain.

#. If the **backend** requires initial configuration you must raise a
   :exc:`~django.core.exceptions.ImproperlyConfigured` exception if the
   configuration is not valid. The error will be displayed in the console.

#. Register your backend to be used in your project with this setting: ::

    GSTUDIO_URL_SHORTENER_BACKEND = 'path.to.your.url.shortener.module'

Here the source code of the default backend. ::

    from django.contrib.sites.models import Site
    from django.core.urlresolvers import reverse
    from gstudio.settings import PROTOCOL

    def backend(entry):
        return '%s://%s%s' % (PROTOCOL, Site.objects.get_current().domain,
                              reverse('gstudio_entry_shortlink', args=[entry.pk]))

For a more examples take a look in this folder: :file:`gstudio/url_shortener/backends/`.