Skip to content

Instantly share code, notes, and snippets.

Michael Bylstra mbylstra

Block or report user

Report or block mbylstra

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@mbylstra
mbylstra / scratchpad.py
Last active Mar 5, 2019
Django scratchpad file. A standalone Django script with the django environment loaded.Place in the root of your Django project. Assumes your settings.py is in a folder named 'main'.
View scratchpad.py
import os
import sys
project_dir = os.path.realpath(__file__)
sys.path.append(project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
import django
from django.conf import settings
django.setup()
@mbylstra
mbylstra / djr_simple_api_view
Last active Dec 19, 2015
If you just want to use Django Rest Framework for authentication and just want to convert a dictionary to json.
View djr_simple_api_view
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
class SimpleView(APIView):
renderer_classes = (JSONRenderer,)
permission_classes = (IsAuthenticated,)
def get(self, request):
content = {'testing': 123}
@mbylstra
mbylstra / django_template_string_interp.py
Last active Dec 19, 2015
using Django templates for fancy string interpolation
View django_template_string_interp.py
t = Template("The first stooge in the list is {{ stooges.0 }}.")
c = Context({"stooges": ["Larry", "Curly", "Moe"]})
t.render(c)
#or if you want to render from an existing html template
from django.template.loader import render_to_string
rendered = render_to_string('my_template.html', context)
@mbylstra
mbylstra / sort_git_branches_by_last_commit
Last active Dec 19, 2015
sort git branches by last commit
View sort_git_branches_by_last_commit
git for-each-ref --sort=-committerdate refs/heads/
#http://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit
@mbylstra
mbylstra / python_source_code_unicode
Last active Dec 19, 2015
allow unicode in python source code
View python_source_code_unicode
# -*- coding: utf-8 -*-
@mbylstra
mbylstra / django_runserver_debug_false
Created Jul 11, 2013
If you want to test logging with DEBUG=True on your local Django dev server, you need to do this.
View django_runserver_debug_false
manage.py runserver --insecure
#http://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail
View safe_print.py
#modified version off http://code.activestate.com/recipes/576602-safe-print/
def sprint(*args, **kwargs):
"""Safely print the given string.
If you want to see the code points for unprintable characters then you
can use `errors="xmlcharrefreplace"`.
"""
errors = kwargs.get('errors', 'replace')
@mbylstra
mbylstra / py_datetime_to_mysql.py
Last active Dec 23, 2015
convert a python date to a mysql date string
View py_datetime_to_mysql.py
from django.db import connection
from django.db.backends.mysql.base import DatabaseOperations
def py_datetime_to_mysql(d):
db_ops = DatabaseOperations(connection)
return db_ops.value_to_db_datetime(d)
#eg:
from django.db import connection
py_datetime_to_mysql(datetime.now())
@mbylstra
mbylstra / disable_mysql_foreign_key_checks
Created Sep 18, 2013
temporarily disable mysql foreign key checks, so you can do something dodgy:
View disable_mysql_foreign_key_checks
SET foreign_key_checks = 0;
DELETE FROM users where id > 45;
SET foreign_key_checks = 1;
@mbylstra
mbylstra / create_utf8_mysql_db
Last active Dec 23, 2015
create a utf8 mysql database
View create_utf8_mysql_db
CREATE DATABASE some_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
You can’t perform that action at this time.