Skip to content

Instantly share code, notes, and snippets.

View xfenix's full-sized avatar
😏
All commits are lost

Denis xfenix

😏
All commits are lost
View GitHub Profile
@xfenix
xfenix / dump.sh
Created October 5, 2012 14:19
Django dump/restore deployment
Local:
./manage.py dumpdata all.json
Remote:
./manage.py syncdb
psql [DBNAME]
delete from auth_group_permissions; delete from auth_permission; delete from django_admin_log; delete from django_content_type;
@xfenix
xfenix / gist:3993825
Created November 1, 2012 14:09
Sticky footer
<!DOCTYPE html>
<head>
<title></title>
<style>
html {
position: relative;
min-height: 100%;
}
body {
margin: 0 0 100px; /* bottom = footer height */
@xfenix
xfenix / multiselect.py
Created December 3, 2012 09:10
Usage of FilteredSelectMultiple in custom models
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
def widget(model_reference, field, title, titlew):
class WidgetForm(forms.ModelForm):
vars()[field] = forms.ModelMultipleChoiceField(queryset=model_reference.objects.all(),
label=(title),
widget=FilteredSelectMultiple(
(titlew),
False,
@xfenix
xfenix / flatpages.py
Created December 11, 2012 15:21
Flat pages with selected default site
#admin.py
from django.contrib.sites.models import Site
class ExtendedFlatPageAdmin(FlatPageAdmin):
fieldsets = (
(None, {'fields': ('url', 'title', 'content', 'sites', )}),
)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
@xfenix
xfenix / output_minify.py
Created December 12, 2012 16:06
Django output html minification
import re
from django.utils.html import strip_spaces_between_tags
from django.conf import settings
RE_MULTISPACE = re.compile(r"\s{2,}")
RE_NEWLINE = re.compile(r"\n")
class MinifyHTMLMiddleware(object):
def process_response(self, request, response):
if 'text/html' in response['Content-Type'] and settings.COMPRESS_HTML and not re.match('^/admin/.*$', request.path):
@xfenix
xfenix / dict_to_object.py
Created December 12, 2012 18:01
Create object from dict
class DictToObject:
def __init__(self, data):
for name, value in data.iteritems():
setattr(self, name, self._wrap(value))
def _wrap(self, value):
if isinstance(value, (tuple, list, set, frozenset)):
return type(value)([self._wrap(v) for v in value])
else:
return Struct(value) if isinstance(value, dict) else value
@xfenix
xfenix / drupal_add_column_to_admincontent.php
Created December 14, 2012 12:45
Add column to admin/content for Drupal 7
<?
function MYMODULE_form_node_admin_content_alter(&$form, &$form_state, $form_id) {
$column_alias = 'my_cool_column';
$column_title = 'Header of my cool column';
// Load the nodes. This incurrs very little overhead as
// "$nodes = node_load_multiple($nids);" has already been run on these
// nids in node_admin_nodes(). The static cache will be used instead of
// another db query being invoked
$nodes = node_load_multiple(array_keys($form['admin']['nodes']['#options']));
@xfenix
xfenix / rupluralize.py
Last active March 5, 2018 04:25
Russian pluralize django template tag
# pluralize for russian language
# {{someval|rupluralize:"товар,товара,товаров"}}
@register.filter(is_safe = False)
@stringfilter
def rupluralize(value, arg):
bits = arg.split(u',')
try:
value = str( 0 if not value or value <= 0 else value )[-1:] # patched version
return bits[ 0 if value=='1' else (1 if value in '234' else 2) ]
except:
from django.utils.html import strip_tags, escape
from django.utils.safestring import mark_safe
"""
Strips all [X]HTML tags except the space seperated list of tags
from the output.
Usage: keeptags:"strong em ul li"
"""
def keep_tags(value, tags):
@xfenix
xfenix / bash_profile.sh
Last active December 16, 2015 08:09
.bash_profile django & git tweaks
# bash
alias rr=". ~/.bash_profile"
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias ......="cd ../../../../.."
alias c=clear
function freem() {