Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Automate Everything

Craig Derington craigderington

🚀
Automate Everything
  • Python Development Systems, LLC.
  • Sanford, FL
View GitHub Profile
@craigderington
craigderington / partition.py
Last active Mar 27, 2016
Break a list into 'n' pieces. The last list may be larger than the rest if the list doesn't break cleanly.
View partition.py
#!/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 Sep 1, 2016
Breaks a list into 'n' pieces horizontally.
View partition_horizontal.py
#!/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 Nov 29, 2016
CustomerListView - Generic List View with Django Tables
View views.py
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 Mar 29, 2016
Django Tables2 - Create Customer Data Table
View tables.py
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 Apr 1, 2016
Django Filters - PagedFilteredTableView
View utils.py
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 Apr 1, 2016
Django Filters - FilterSet
View filters.py
# 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 Apr 1, 2016
Generic Customer List View - Search & Filter QuerySet
View views.py
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 Apr 4, 2016
Django Generic Detail View
View views.py
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):
"""
View customer_list.html
{% 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 Nov 29, 2016
Customer Model
View models.py
# 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)