Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View shangxiao's full-sized avatar

David Sanders shangxiao

View GitHub Profile
@shangxiao
shangxiao / authenticated_or_404.py
Created May 14, 2016 13:20
Django view decorator for returning 404 if not authenticated
from functools import update_wrapper
from django.http import Http404
class authenticated_or_404(object):
def __init__(self, func):
self.func = func
update_wrapper(self, func)
[tool.poetry]
name = "poetry-test"
version = "0.1.0"
description = ""
authors = ["Some User <some@user.com>"]
[tool.poetry.dependencies]
python = "^3.6"
[tool.poetry.dev-dependencies]
@shangxiao
shangxiao / results
Created July 3, 2019 06:16
Pytest not raising import exceptions for regular classes?
shangxiao ~/projects/test-pytest $ pytest
========================================================= test session starts =========================================================
platform darwin -- Python 3.6.2, pytest-5.0.0, py-1.8.0, pluggy-0.12.0
rootdir: /Users/shangxiao/projects/test-pytest
collected 1 item
test_foo.py F [100%]
============================================================== FAILURES ===============================================================
________________________________________________________ FooTestCase.test_foo _________________________________________________________
@shangxiao
shangxiao / django-cte.md
Last active May 17, 2018 02:00
CTE's in Django
@shangxiao
shangxiao / debug_middleware.py
Created September 11, 2017 11:08
FunkyBob's debug middleware
import time
from django.conf import settings
from django.db import connection
class DebugMiddleware:
def __init__(self):
import logging
self.log = logging.getLogger(__name__)
Relay Notes
===========
- Define a schema, that is used for _both_ server side endpoint setup & frontend graphql compilation
- node only for now, some graphql ports
- Relay has a strict specification of how a graphql schema is set up: https://facebook.github.io/relay/docs/graphql-relay-specification.html
- tree children are called "plural links" (see _writePluralLink())
@shangxiao
shangxiao / faq.md
Last active July 24, 2016 22:24
django faq

Frequently asked questions:

  • Why shouldn't i use mysql? / mongo?
  • Should I use jinja, I read it's faster?
  • Should I use sqlalchemy, I read it's superior?
  • How do i make this thing happen in parallel/not wait for xxx to finish?
  • Why does django ask for a default? (But my table has no data in it wtf django!?!)
    • FunkyBob | I think, perhaps, it's time I wrote a blog post on "why does a migration adding a field need a default?"
  • Why do I need to commit my migrations? Why can't I just run makemigrations on the server?
  • migrations are source, makemigrations is a helper which takes you part way
  • i've seen people accidentally lose their migrations on production…  oops now what?
@shangxiao
shangxiao / gotcha.py
Created July 31, 2015 06:44
Django is_authenticated gotcha
def some_view(request):
if request.user.is_authenticated:
do_something_private()
class ChoiceField(Field):
...
def to_representation(self, value):
if data == '' and self.allow_blank:
return ''
try:
return self.choice_strings_to_values[six.text_type(value)]
except KeyError:
return value