Skip to content

Instantly share code, notes, and snippets.

View vdboor's full-sized avatar

Diederik van der Boor vdboor

View GitHub Profile
@vdboor
vdboor / pgbouncer_user.py
Created April 7, 2016 13:08
Manage the `userlist.txt` for PgBouncer in ansible. Place this file in the `library` folder.
#!/usr/bin/env python
#
# (c) 2015, Edoburu, GPLv3+ licensed
#
# Based on https://github.com/ansible/ansible-modules-core/blob/devel/web_infrastructure/htpasswd.py
#
DOCUMENTATION = """
module: htpasswd
short_description: manage PgBouncer userlist.txt entries
description:
@vdboor
vdboor / date_trunc.py
Last active August 19, 2019 08:22
Allow to use DATE_TRUNC('month', "date_field") in PostgreSQL for Django 1.8+. I'm using this for django-oscar reports per month/week/year.
from django.db.models import Func, Value
class DateTrunc(Func):
"""
To support using DATE_TRUNC('text', "field") in SQL
Example::
order_totals = (orders
@vdboor
vdboor / parent_object.py
Last active September 29, 2019 03:11
A view mixin that allows fetching a parent object, and limiting the children for that. A typical example would be "users belonging to a customer" or "entries in a category".
"""
Parent object mixin, also available at: https://gist.github.com/vdboor/7f7865df748ed8949ba5
"""
from functools import lru_cache
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.db import models
from django.http import Http404
from django.urls import reverse
@vdboor
vdboor / create_update.py
Last active October 25, 2018 15:12
A CreateOrUpdateView for Django, to support both creating and updating an object in a single view
"""
An create+update view in a single class.
"""
from django.views.generic.detail import SingleObjectTemplateResponseMixin
from django.views.generic.edit import ModelFormMixin, ProcessFormView
class BaseCreateOrUpdateView(ModelFormMixin, ProcessFormView):
"""
Merging the logic of Django's
@vdboor
vdboor / fields.py
Last active February 18, 2016 09:46
USE https://github.com/edoburu/django-parler-rest/ NOW! THIS GIST IS DEPRECATED. It was a test to share code before making the package ----- Combining django-parler with django-rest-framework. Please tell me how that code is working for you, and if you have any improvements. The final version could be included into a 'contrib' folder of django-p…
from django.core.exceptions import ImproperlyConfigured
from rest_framework import serializers
from rest_framework.serializers import SortedDictWithMetadata
from .utils import create_translated_fields_serializer
class TranslatedFieldsField(serializers.WritableField):
"""
Exposing translated fields for a TranslatableModel in REST style.
"""
@vdboor
vdboor / catalogue | models.py
Last active February 25, 2021 12:29
A quick overview how to combine django-parler with django-oscar. This assumes you are familliar with the "Customising Oscar" topics (https://django-oscar.readthedocs.org/en/latest/topics/customisation.html). A screenshot of the integration was shown at DjangoConEU 2014 (http://django-fluent.org/blog/2014/05/lightning-talk-djangocon-eu-2014/)
from oscar.apps.catalogue.abstract_models import AbstractProduct,
from parler.models import TranslatableModel, TranslatedFields
class Product(AbstractProduct, TranslatableModel):
"""
Add translations to the product model.
"""
# Provide translated fields.
@vdboor
vdboor / postactivate
Created August 28, 2013 11:24
Switch to a directory when entering a virtualenv
#!/bin/bash
# This hook is run after this virtualenv is activated.
root="$HOME/Sites/example.org/"
# Change to root unless already there.
if [[ "`pwd`" != "$root"* ]]; then
cd "$root"
fi
@vdboor
vdboor / patch_admin_permissions_list.py
Last active March 29, 2021 11:10
Patch Django admin to hide useless default permissions.Import this file somewhere in an `__init__.py` or `admin.py` file.
"""
Hide permission in the Django admin which are irrelevant, and not used at all.
"""
from django.contrib import admin
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.contrib.auth.models import Group, User
class PermissionFilterMixin(object):
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
@vdboor
vdboor / start-django-fluent.sh
Last active December 17, 2015 10:59
Starting a django-fluent-project
mkdir my-website.com
cd my-website.com
django-admin.py startproject mywebsite . -e py,rst,example,gitignore --template=https://github.com/edoburu/django-project-template/archive/django-fluent.zip
@vdboor
vdboor / jquery.django-inlines.js
Last active November 5, 2019 21:12
jQuery plugin for dynamic Django admin inlines. This script gives more freedom over the HTML layout (in constrast to `inlines.js` from Django), but does require manual binding.
/**
* jQuery plugin for Django inlines
*
* - When a `.js-django-inlines` is present, it will automatically enable this script for it.
* - When `.js-add-form` is missing, an add button will be inserted manually.
* - Make sure a `.js-remove-form` element is present in the HTML.
*
* To customize the behavior, use different class names and manually call $formset.djangoInline().
* This can also be used to manually connect the 'add' and 'delete' buttons.
*