Django neo-pastebin¶
Pastebin module for Django.
Django neo-pastebin, also known as NPB, is a text storage module for the Django framework. It is designed to be easily integrated into your website and customizable.
Navigation¶
Quickstart¶
You can install Django neo-pastebin directly from PyPI:
pip install django-npb
Integrate with Django¶
Add django-npb to your INSTALLED_APPS:
INSTALLED_APPS = [
[skiped],
'npb.apps.NpbConfig',
]
Then add the URLs:
urlpatterns = [
[skiped],
path('paste/', include('npb.urls', namespace='npb')),
]
If you uses internationalization, you are encouraged to add the path your i18n_patterns instead.
Configure¶
To configure NPB once it is set up, see Configuration options.
Configuration options¶
The following configuration variables may be set in your project’s settings.py file.
Common settings¶
NPB_ALLOW_ANONYMOUS¶
Define whether or not anonymous users are allowed to post. Default is “False”.
NPB_DEFAULT_EXPIRATION¶
Set which expiration duration is selected by default. The value is a pytimeparse duration which is set in NPB_EXPIRATION_LENGTH. Default is the first NPB_EXPIRATION_LENGTH value.
NPB_DEFAULT_EXPIRATION = '1d'
NPB_DEFAULT_EXPOSURE¶
Default exposure for new pastes. Must be either “public”, “unlisted” or “private”. Default is “public”.
NPB_EDIT_TOLERANCE¶
Set the tolerance time during which a paste can be edited without being labeled as so. Set to a negative value, it always shows the edit time. Set to a positive value, it represents the number of seconds after the creation time during which edits are not shown. Zero causes an undefined behavior. Default is 1.
NPB_EXPIRATION_LENGTH¶
Set the allowed expiration duration. It is a list of tuples where the first element if a pytimeparse duration (16 characters maximum) and the second one is the name to be displayed.
NPB_EXPIRATION_LENGTH = [
('15m', _('15 minutes')),
('1h', _('1 hour')),
('1d', _('1 day')),
('1w', _('1 week')),
('30d', _('30 days')),
('365d', _('365 days')),
('never', _('never')),
]
NPB_LOGIN_REDIRECT¶
If “NPB_ALLOW_ANONYMOUS” is “False”, define whether or not anonymous users should be redirected to the login page (“True”) or get a permission denied error (“False”). Default is “True”.
NPB_PASTES_LIST_MAX¶
Maximum number of posts displayed in the “recent posts” list. Set it to zero if you want to disable the list. Default is 5.
NPB_REPORT_CATEGORIES¶
Set the list of reasons a paste can be reported to an administrator. It is a list of tuples where the first element if a short identifier (16 characters maximum) and the second one is the name to be displayed.
NPB_REPORT_CATEGORIES = [
('illicit', _('illicit content')),
('explicit', _('explicit content')),
('copyright', _('copyright infrigment')),
('private', _('private information exposure')),
('other', _('other')),
]
NPB_TAB_SIZE¶
Set the number of spaces needed to represent a tabulation. Default is 4.
NPB_X_FORWARDED_FOR¶
Defines how NPB must use the X-Forwarded-For HTTP header. The value must be one of the following:
- “none”: Do not use the header at all.
- “first”: Use the first IP listed as the end-user’s one.
- “last”: Use the last IP listed as the end-user’s one.
Default is “none”.
Customize¶
Add fields to the forms¶
There is many reason to use a custom form. The most obvious one is to add your favorite captcha engine. Fortunately, it is very simple to add such custom fields.
First, you need to build the form itself by overloading one of the default one. Base it upon npb.forms.PasteForm or npb.forms.ReportForm depending on which form you want to extend.
from django.utils.translation import gettext_lazy as _
from npb.forms import PasteForm, ReportForm
from django import forms
class MyCustomForm(PasteForm):
my_field = forms.CharField(
label=_('My field'),
max_length=128,
)
Once your form is ready, pass it to the view.
from django.urls import path
from npb import views as npb_views
from .forms import MyCustomForm
urlpatterns = [
path(
'',
npb_views.CreatePasteView.as_view(form_class=MyCustomForm),
name='index'
),
]
Editing the style¶
The simplest and more powerful way to edit the style is to override templates. The following templates are open to overriding:
npb/base.html¶
This template contains the HTML page’s structure. It defines the following empty blocks that will be extended by other templates:
headers
: A place to add extra HTML headers.title
: The page’s title.content
: The page’s content.
It also displays Django’s messages and, if activated, recent pastes.
npb/paste_detail.html¶
This template is used to render a specific paste. For this purpose, it uses the paste
variable. Please refer to the Past
object in order to list its available properties.
npb/paste_form.html¶
This template is where the new past form is rendered. It uses the form
variable which is by default a npb.PasteForm
object.
npb/report_form.html¶
Same as the new paste form but for a new report. The default associated form object is a npb.ReportForm
.
recent_pastes¶
In your templates, you can load recent_pastes
to have access to the following tags:
recent_pastes_activated
: True if the recent pastes list is activated, False otherwise.recent_pastes
: Return an iterator over the most recent pastes.