Skip to content

Instantly share code, notes, and snippets.

View craigderington's full-sized avatar
🚀
Automate Everything

Craig Derington craigderington

🚀
Automate Everything
View GitHub Profile
@craigderington
craigderington / partition.py
Last active March 27, 2016 01:07
Break a list into 'n' pieces. The last list may be larger than the rest if the list doesn't break cleanly.
#!/usr/bin/python
def partition(thelist, n):
try:
n = int(n)
list(thelist)
except (ValueError, TypeError):
return [thelist]
p = len(thelist) / n
return [thelist[p*i:p*(i+1)] for i in range(n - 1)] + [thelist[p*(i+1):]]
@craigderington
craigderington / partition_horizontal.py
Last active September 1, 2016 22:04
Breaks a list into 'n' pieces horizontally.
#!/usr/bin/python
def partition_horizontal(thelist, n):
"""
break a list into 'n' pieces horizontally
"""
try:
n = int(n)
thelist = list(thelist)
@craigderington
craigderington / views.py
Last active November 29, 2016 13:59
CustomerListView - Generic List View with Django Tables
from django.views.generic import ListView
from braces.views import LoginRequiredMixin, GroupRequiredMixin
from django_tables2 import RequestConfig
from .models import Customer
from .tables import CustomerTable
class CustomerListView(LoginRequiredMixin, GroupRequiredMixin, ListView):
model = Customer
template_name = 'customer_list.html'
@craigderington
craigderington / tables.py
Last active March 29, 2016 15:19
Django Tables2 - Create Customer Data Table
import django_tables2 as tables
from django_tables2.utils import A
from .models import Customer
class CustomerTable(tables.Table):
account_number = tables.LinkColumn('customer-detail', args=[A('pk')])
customer_first_name = tables.LinkColumn('customer-detail', args=[A('pk')])
customer_last_name = tables.LinkColumn('customer-detail', args=[A('pk')])
customer_email = tables.LinkColumn('customer-detail', args=[A('pk')])
@craigderington
craigderington / utils.py
Last active April 1, 2016 22:37
Django Filters - PagedFilteredTableView
from django_tables2 import SingleTableView
from django_tables2.config import RequestConfig
class PagedFilteredTableView(SingleTableView):
filter_class = None
formhelper_class = None
context_filter_name = 'filter'
def get_queryset(self, **kwargs):
@craigderington
craigderington / filters.py
Last active April 1, 2016 22:38
Django Filters - FilterSet
# filters.py
import django_filters
from .models import Customer
class CustomerListFilter(django_filters.FilterSet):
class Meta:
model = Customer
fields = ['account_number', 'customer_first_name', 'customer_last_name',
@craigderington
craigderington / views.py
Last active April 1, 2016 14:22
Generic Customer List View - Search & Filter QuerySet
from django.views.generic import ListView
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.query_utils import Q
from django_tables2 import RequestConfig
from braces.views import LoginRequiredMixin, GroupRequiredMixin
from .tables import CustomerTable
from .filters import CustomerListFilter
from .utils import PagedFilteredTableView
from .models import Customer
from .forms import CustomerListFormHelper
@craigderington
craigderington / views.py
Created April 4, 2016 13:14
Django Generic Detail View
from django.views.generic import DetailView
from .models import Customer
class CustomerDetailView(LoginRequiredMixin, DetailView):
def _allowed_methods(self):
return [m.upper() for m in self.http_method_names if hasattr(self, m)]
@classonlymethod
def as_view(cls, **initkwargs):
"""
{% extends "base.html" %}
{% load bootstrap3 %}
{% load querystring from django_tables2 %}
{% load title from django_tables2 %}
{% load trans blocktrans from i18n %}
{% block "content" %}
{% if table.page %}
@craigderington
craigderington / models.py
Last active November 29, 2016 13:58
Customer Model
# models.py
from django.db import models
class Customer(models.Model):
company = models.ForeignKey('Company', null=False)
customer_first_name = models.CharField(null=False, blank=False, max_length=50)
customer_last_name = models.CharField(null=False, blank=False, max_length=50)
customer_email = models.CharField(null=False, blank=False, max_length=80)