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 / dashboard.py
Created July 28, 2011 21:17
Example dashboard module
class PersonalModule(modules.LinkList):
title = _('Welcome,')
draggable = False
deletable = False
collapsible = False
template = 'ecms_dashboard/modules/personal.html'
def init_with_context(self, context):
super(PersonalModule, self).init_with_context(context)
@vdboor
vdboor / mixins.py
Created October 31, 2012 08:52
Django view initialization ordering issues
class BaseViewMixin(object):
def dispatch(self, request, *args, **kwargs):
# Set earlier to let get_loggedin_user() and get_context_user() work.
# They run before the get() and post() methods are called.
self.request = request
self.args = args
self.kwargs = kwargs
# Run checks before entering the view.
@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.
*
@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 / 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 / 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 / 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 / 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 / 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 / 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