Skip to content

Instantly share code, notes, and snippets.

@abhijo89
abhijo89 / admin.py
Created February 1, 2018 05:40 — forked from mariocesar/admin.py
Django admin decorator to create a confirmation form action, like the default delete action works
from .models import Post, Category
from .decorators import action_form
class PostCategoryForm(forms.Form):
title = 'Update category for the selected posts'
category = forms.ModelChoiceField(queryset=Category.objects.all())
@admin.register(Post)
@abhijo89
abhijo89 / glaciervault.py
Last active August 29, 2015 14:27 — forked from tsileo/glaciervault.py
Wrapper for uploading/download archive to/from Amazon Glacier Vault
# encoding: utf-8
import os
import shelve
import boto.glacier
import boto
from boto.glacier.exceptions import UnexpectedHTTPResponseError
ACCESS_KEY_ID = "XXXXXXXXXXXXX"
SECRET_ACCESS_KEY = "XXXXXXXXXXX"
SHELVE_FILE = os.path.expanduser("~/.glaciervault.db")
# encoding: utf-8
from __future__ import unicode_literals
import os
import requests
import sys
def get_google_auth_session(username, password):
session = requests.Session()
@abhijo89
abhijo89 / filter.py
Created January 13, 2015 11:57
filter by dynamic field
def set_filter_operation_on_model(model_query_set_object, field, value, operator ):
if operator == FilterOperators.EQ:
kwargs = {'{0}__{1}'.format(field, 'exact'): value ,}
elif operator == FilterOperators.CONTAINS:
kwargs = {'{0}__{1}'.format(field, 'contains'): value ,}
elif operator == FilterOperators.IS_NOT:
kwargs = {'{0}__{1}'.format(field, 'exact') : value, }
return model_quesry_set_object.filter(~Q(**kwargs))
return model_quesry_set_object.filter(**kwargs)
def get_ip_address_from_request(request):
""" Makes the best attempt to get the client's real IP or return the loopback """
PRIVATE_IPS_PREFIX = ('10.', '172.', '192.', '127.')
ip_address = ''
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR', '')
if x_forwarded_for and ',' not in x_forwarded_for:
if not x_forwarded_for.startswith(PRIVATE_IPS_PREFIX) and is_valid_ip(x_forwarded_for):
ip_address = x_forwarded_for.strip()
else:
ips = [ip.strip() for ip in x_forwarded_for.split(',')]
@abhijo89
abhijo89 / show_hide_django_filters.js
Created December 13, 2011 09:16 — forked from empty/show_hide_django_filters.js
Simple jQuery script to show / hide Django's change list filters.
<script type="text/javascript">
jQuery(document).ready(function($){
$('<div id="show-filters" style="float: right;"><a href="#">Show Filters</a></p>').prependTo('div.actions');
$('#show-filters').hide();
$('#changelist-filter h2').html('<a style="color: white;" id="hide-filters" href="#">Filter &rarr;</a>');
$('#show-filters').click(function() {
$('#changelist-filter').show('fast');
$('#changelist').addClass('filtered');
$('#show-filters').hide();