This file contains 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
''' | |
requires langdetect, available on pypi (pip install langdetect) | |
https://github.com/Mimino666/langdetect | |
''' | |
import langdetect | |
def split_by_language(content, delimiter='\n', joiner='\n', | |
languages=None, fail_silently=True): | |
''' |
This file contains 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
''' | |
Adds access to previous versions to a model's file field. | |
Requires django-storages, and you must either be using S3Boto3Storage | |
by default, or you must specify it as the storage class in the field | |
definition. | |
class SampleModel(models.Model): | |
file = S3FileField(upload_to='path/to/dir') |
This file contains 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
import uuid | |
import datetime | |
import itertools | |
import re | |
import inflection # requires python library inflection (pip install inflection) | |
class ContextPlaceholder: | |
counter = itertools.count() | |
DEFAULT_VALUES = { |
This file contains 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 contextlib import contextmanager | |
from django.dispatch.dispatcher import Signal | |
class SignalTestMixin(object): | |
''' | |
Add this as a mixin to any Django TestCase | |
that needs to add testing for signals. | |
This file contains 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 function can be used to process text that may or may not be base64 encoded | |
# In general, if you use base64 decode on normal text, you end up with random bytes | |
# that don't resemble a standard encoding. This function makes use of the chardet | |
# library to recognize decoded text that matches an expected encoding. | |
# Note that it tends to fail for very short inputs. | |
import binascii | |
import base64 |
This file contains 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
window.CASLogin = window.CASLogin || (function () { | |
var action = "{{ action|safe }}", | |
ticket = {% if ticket %}"{{ ticket.ticket }}"{% else %}null{% endif %}, | |
logged_in = {{ logged_in|lower|default:"false" }}, | |
username = {% if request.user.is_authenticated %}"{{ request.user.username }}"{% else %}null{% endif %}, | |
email = {% if email %}"{{ email }}"{% else %}null{% endif %}; | |
function authenticate() { | |
if (!logged_in) { | |
window.location.href = action; |
This file contains 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
(function() { | |
var monthInputs = document.querySelectorAll('input[type="month"]'), | |
checkDateInput = document.createElement('input'), | |
dateSupported = false, | |
months = [], | |
lang = document.documentElement.lang || navigator.language, | |
DEFAULT_SPAN = 5; | |
if (monthInputs[0].type === 'month') { | |
// browser supports month input; no need for polyfill |
This file contains 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 file provides a base Command class that handles sending errors via mail_admins | |
# Make sure to adjust the LOGGING variable in settings | |
# Credit to Abdulla Diab: https://mpcabd.xyz/make-django-management-commands-send-errors-email-admins/ | |
import sys | |
import logging | |
from django.core.management import BaseCommand | |
logger = logging.getLogger('management.command') |
This file contains 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
0 22 * * 1,2,3,4,5 /path/to/git_changes.sh /path/to/projects/ # Sends 5 PM EST if server time is UTC | |
0 21 * * 5 /path/to/git_changes.sh -w /path/to/projects # Sends Friday @ 4PM EST if server time is UTC |
This file contains 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 get_dotted_value(obj, attr=None): | |
"""Access values from an object using dot notation. | |
Can be attributes, keys, or indices. | |
>>> class SampleObject(): | |
... def __repr__(self): | |
... return repr(sorted(self.__dict__.items())) | |
... | |
>>> zoo = SampleObject() |
NewerOlder