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 / 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 / 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 / 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 / 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 / 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 / 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 / csv_output.php
Created October 4, 2012 12:08
CSV cross-platform generation (utf-16le)
header("Cache-Control: must-revalidate");
header("Pragma: must-revalidate");
header("Content-type: application/vnd.ms-excel; charset=URF-16LE");
header("Content-disposition: attachment; filename=file.csv");
echo chr( 0xFF ) . chr( 0xFE ) . mb_convert_encoding( $content, 'UTF-16LE', 'UTF-8' );
@xfenix
xfenix / transliterate.py
Created September 20, 2012 11:24
Transliterate functions
def translit(locallangstring):
conversion = {
u'\u0410' : 'A', u'\u0430' : 'a',
u'\u0411' : 'B', u'\u0431' : 'b',
u'\u0412' : 'V', u'\u0432' : 'v',
u'\u0413' : 'G', u'\u0433' : 'g',
u'\u0414' : 'D', u'\u0434' : 'd',
u'\u0415' : 'E', u'\u0435' : 'e',
u'\u0401' : 'Yo', u'\u0451' : 'yo',
u'\u0416' : 'Zh', u'\u0436' : 'zh',
@xfenix
xfenix / modalwindowimageheightfix.js
Created September 19, 2012 13:58
This script allow dynamically load content with images, that has no height attribute. Nyromodal version.
var secret = $('<div>').attr('id', 'secret-meter'),
loadNow = 0,
loadTotal = 0;
$('body').prepend(secret);
$('.dialog').click(function(){
var url = $(this)[0].href,
loadWindow = function() {
$.nmData(secret.html(), {
closeButton: '<a href="#" class="nyroModalClose nyroModalCloseButton nmReposition" title="close"></a>',
@xfenix
xfenix / base_site.html
Created September 5, 2012 14:22
Grappelli ckeditor simple integration with solved collapsable problem and django-filebrowser
{% extends "admin/base.html" %}
{% load i18n %}
{% block extrahead %}
<script type="text/javascript" src="/static/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
grp.jQuery(function(){
config = {
'toolbar': [
[