Skip to content

Instantly share code, notes, and snippets.

@michaeljohnbarr
michaeljohnbarr / All Migrations Applied.txt
Last active December 12, 2016 19:38
View for viewing applied/unapplied Django migrations (tested on Django 1.8)
admin
[X] 0001_initial
someapp
[X] 0001_initial
[X] 0002_auto_20150701_1359
[X] 0003_auto_20160427_0527
api
(no migrations)
auth
[X] 0001_initial
@michaeljohnbarr
michaeljohnbarr / lies.py
Created February 18, 2016 04:17
Double Negatives, Anyone?!
if not True:
print 'wow'
else:
print 'neat'
# neat
if not not True:
print 'wow'
else:
print 'neat'
@michaeljohnbarr
michaeljohnbarr / tests.py
Last active August 29, 2015 14:25
Insure all models registered in Django Admin
# =============================================================================
# IMPORTS
# =============================================================================
# Python
from __future__ import unicode_literals
from future_builtins import map, filter
# Django
from django.apps import apps
from django.contrib import admin
@michaeljohnbarr
michaeljohnbarr / encoding.py
Last active September 22, 2015 05:05
Dynamically detect file encoding
import io
from chardet import UniversalDetector
def get_file_encoding(file_path):
with io.open(file_path, 'rb') as f:
# If you're dealing with a large amount of text, you can call the Universal
# Encoding Detector library incrementally, and it will stop as soon as it
# is confident enough to report its results.
detector = UniversalDetector()
@michaeljohnbarr
michaeljohnbarr / fields.py
Created September 3, 2014 00:33
Beginnings of a jQuery DatePicker package
# ==============================================================================
# IMPORTS
# ==============================================================================
# Django
from django.forms.fields import DateField, DateTimeField
# App
from .widgets import DatePickerWidget
__all__ = ('DatePickerDateField', 'DatePickerDateTimeField')
@michaeljohnbarr
michaeljohnbarr / gist:6259405
Last active December 21, 2015 05:49
A Django Class-Based View Mixin for creating a context variable for view_type of "Create" or "Update." This is useful for sharing the same form template and updating instructions or buttons.
# forms.py
from django.forms import ModelForm
from .models import SomeModel
SomeModelForm(ModelForm):
model = SomeModel
# views.py