Skip to content

Instantly share code, notes, and snippets.

@synotna
synotna / gist:174fa662f0d171baa8ba
Created February 27, 2015 14:31
Combine Django Q objects
def combine_q_or(q_objects=None):
if q_objects is None:
q_objects = []
if len(q_objects):
return reduce(OR, q_objects)
return Q()
@synotna
synotna / .README.md
Last active November 22, 2015 13:39
How to use .gitgnore for Dockerfile because .dockerignore sucks

Want to exclude patterns of files at any folder depth?

Pulling your hair out because .dockerignore (Go filepath.Match) sucks?

Don't waste time with .dockerignore

Make use of tar's pattern matching together with Docker's tar support and save your sanity!

@synotna
synotna / 0guide.md
Last active August 29, 2015 14:22
How to use environment variable files with IDEA with remote interpreter

Want to use environment variables for your IDEA project?

  • Want you use .env files like you do for all other tools?
  • Don't want to include hacks in your project code for loading environment variables just for development?
  • Don't want to use the nightmare environment variables features of IDEA?
  • Want to easily be able to switch environments without losing your sanity?

Here's how!

  • Create a user, recommend per project and store project files in under user's homedir rather than your own
  • otherwise you need to add it & the original user to each others groups, and keep file permissions in mind
@synotna
synotna / gist:eaccd2586f5d3eda0f68
Created June 12, 2015 15:38
django bulk_update_or_create work in progress
def bulk_update_or_create(self, channels, *key_fields):
if len(key_fields) == 1:
key_field = key_fields[0]
key_to_object_mapping = {channel[key_field]: channel for channel in channels}
lookup = {'{}__in'.format(key_field): list(key_to_object_mapping)}
existing = self.filter(**lookup)
existing_keys = existing.values_list(key_field, flat=True)
to_create = [self.model(**channel) for channel in channels
if channel[key_field] not in existing_keys]
self.bulk_create(to_create, batch_size=1000)
@synotna
synotna / FieldPermissionsMixin
Created July 15, 2015 13:01
Field permissions mixin for Django Rest Framework
class FieldPermissionsMixin(object):
"""
A Serializer mixin for controlling which fields are included based on user permissions
Usage:
class MySerializer(FieldPermissionsMixin, serializers.ModelSerializer):
class Meta:
model = MyModel
field_permissions = {
'field': ['app.permission'],

Keybase proof

I hereby claim:

  • I am synotna on github.
  • I am synotna (https://keybase.io/synotna) on keybase.
  • I have a public key ASCfx3g3WgnIUJbeYEuYrvOfqZ0fVqrTIBLEuUMznytxHQo

To claim this, I am signing this object: