Skip to content

Instantly share code, notes, and snippets.

@kofic
kofic / customizing_django_admin.md
Last active March 16, 2019 21:16
Customizing Django Admin

Initial Model and Admin code

Let's say I have a simple Content model.py that looks like this:

# Content model
class Content(models.Model):
    TYPE_CHOICES = (
        ('text', 'Text'),
        ('html', 'HTML'),
 ('image', 'Image'),
@kofic
kofic / post.md
Created February 21, 2019 19:00 — forked from fxn/post.md
GeoPlanet data with ancestor chain cache imported in 10 minutes

GeoPlanet data with ancestor chain cache imported in 10 minutes

Yahoo! provides its GeoPlanet data as three separate TSV files, available for download here.

That's a database with some 17 million records:

  • 5.7 million records: locations (aka places).
  • 2.2 million records: alternative names for each place (aka aliases).
  • 9.6 million records: matrix of neighbourhoods per place (aka adjacencies).

Introduction to Django

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

https://www.djangoproject.com/

So lets use Django to build a simple website - shall we.

Create a new project in PyCharm

Let's say you have a Django app called sections - your templatetag will be in the following folder:


 sections/
      __init__.py
      models.py
      ...
      templatetags/
          __init__.py

SQLite3 Electron windows integration

This guide is based on the very informative discussion in this article: Using node_sqlite3 with Electron

Install sqlite3

npm install sqlite3 --save
@kofic
kofic / tag_info.py
Created August 27, 2016 20:25 — forked from voldmar/tag_info.py
Django command for getting info about template tags and filters within your project
# coding: utf-8
'''Django command for getting info about template tags and filters within your project
It suports two output formats: human readable (with extra doc) and vim quickfix format
Usage: ./manage.py tag_info tag_or_filter_name
'''
import os
import sys
@kofic
kofic / m2m.py
Created March 1, 2016 22:49 — forked from alexgleason/m2m.py
Many to many relationships in Wagtail
from django.db import models
from wagtail.wagtailsnippets.models import register_snippet
from wagtail.wagtailcore.models import Page
from wagtail.wagtailadmin.edit_handlers import InlinePanel
@register_snippet
class Category(models.Model):
"""
class LanguageMiddleware(object):
"""
Detect the user's browser language settings and activate the language.
If the default language is not supported, try secondary options. If none of the
user's languages are supported, then do nothing.
"""
def is_supported_language(self, language_code):
supported_languages = dict(settings.LANGUAGES).keys()
return language_code in supported_languages
function nestCollection(model, attributeName, nestedCollection) {
//setup nested references
for (var i = 0; i < nestedCollection.length; i++) {
model.attributes[attributeName][i] = nestedCollection.at(i).attributes;
}
//create empty arrays if none
nestedCollection.bind('add', function (initiative) {
if (!model.get(attributeName)) {
model.attributes[attributeName] = [];