This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
register = template.Library() | |
from templatetag_sugar.register import tag | |
from templatetag_sugar.parser import Constant, Variable, Name | |
from .utils import get_next_or_previous | |
""" | |
Efficient and generic get next/previous tags for the Django template language, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os.path import splitext | |
from django.core.exceptions import ValidationError | |
from django.utils.translation import ugettext_lazy as _ | |
from django.template.defaultfilters import filesizeformat | |
class FileValidator(object): | |
""" | |
Validator for files, checking the size, extension and mimetype. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sanitize_log_data(secret, data=None, leave_characters=4): | |
""" | |
Clean private/secret data from log statements and other data. | |
Assumes data and secret are strings. Replaces all but the first | |
`leave_characters` of `secret`, as found in `data`, with '*'. | |
If no data is given, all but the first `leave_characters` of secret | |
are simply replaced and returned. | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
oldfile = open('freeze_old.txt') | |
newfile = open('freeze_new.txt') | |
packages = {} | |
def splitpackage(line): | |
result = line.split('==') | |
if not len(result) == 2: | |
result = line.split('@') | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core.exceptions import ValidationError | |
... | |
def default_validator(value): | |
""" Make sure only one instance of MyModel has default=True """ | |
if value and MyModel.objects.filter(default=True).exists(): | |
raise ValidationError('A default has already been defined.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
# Import the PROJECT_MODEL string from the current app's settings | |
from .settings import PROJECT_MODEL | |
class Task(models.Model): | |
""" | |
Instead of explicitly giving the model for the FK as a Python object or | |
explicitly stating a string in the form '<app>.<modelname>', we'll use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Open files without extenions and guess mimetype through python-magic. | |
""" | |
# Fix extensions | |
(root, ext) = path.splitext(full_path) | |
if ext == '': | |
import magic, mimetype |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.o | |
*.pyc | |
.DS\_Store | |
build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Counting the amount of projects by distinct tag in a queryset | |
Project.objects.values('tags__name').annotate(projects=Count('id')).order_by() | |
""" | |
For filtering, this leads to the following design pattern: | |
1. Given a set of filters, apply each filter except for the current one. | |
2. Run `qs.values(<filtered_field>).annotate(projects=Count('id')).order_by()` to determine available options and counts. | |
3. Return to the client (either through AJAX or through a form) and render appropriately. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script transcodes a single MP4 video to WebM and Theora and | |
# creates a still for the video after 2 seconds | |
# Inspiration: http://johndyer.name/ffmpeg-settings-for-html5-codecs-h264mp4-theoraogg-vp8webm/ | |
INFILE=$1 | |
FORMAT=$2 | |
BASENAME="${INFILE%.*}" | |
VIDEO_BITRATE="1500k" |