Skip to content

Instantly share code, notes, and snippets.

View dchaplinsky's full-sized avatar

Dmitry Chaplinsky dchaplinsky

View GitHub Profile
@dchaplinsky
dchaplinsky / signup_codes_for_social_auth.py
Created March 15, 2011 16:55
To make it possible to only register users who provided invite code and log in only users which was already registered
@render_to("account/signup.html")
def register(request, backend):
"""Start authentication process"""
code = request.GET.get("code")
signup_code = check_signup_code(code)
if signup_code:
request.session['signup_code'] = signup_code
return auth(request, backend)
@dchaplinsky
dchaplinsky / sass.py
Created September 8, 2011 11:05
Update to webassets SassFilter to make it also work as output filter
import os, subprocess
from webassets.filter import Filter
class SassFilter(Filter):
"""Converts `Sass <http://sass-lang.com/>`_ markup to real CSS.
"""
name = 'sass'
@dchaplinsky
dchaplinsky / views.py
Created September 16, 2011 22:30
social auth
SOCIAL_DETAILS = None
def soul_catcher(sender, uid, response, details, **kwargs):
global SOCIAL_DETAILS
SOCIAL_DETAILS = details
socialauth_not_registered.connect(soul_catcher, sender=None)
@render_to("account/signup.html")
def register(request, backend):
@dchaplinsky
dchaplinsky / pypistats.py
Created September 18, 2011 10:20
PyPI Download Stats (found here: http://www.codekoala.com/blog/2010/pypi-download-stats/, (c) by CodeKoala)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Calculates the total number of downloads that a particular PyPI package has
received across all versions tracked by PyPI
"""
from datetime import datetime
import locale
@dchaplinsky
dchaplinsky / avatars.py
Created September 28, 2011 18:12
Get avatars from social networks (facebook and google) with django-social-auth
from social_auth.backends.facebook import FacebookBackend
from social_auth.backends import google
def social_extra_values(sender, user, response, details, **kwargs):
result = False
if "id" in response:
from apps.photo.models import Photo
from urllib2 import urlopen, HTTPError
from django.template.defaultfilters import slugify
@dchaplinsky
dchaplinsky / dump_fixtures.py
Created November 8, 2011 15:08
Script for dumping only particular fields of particular django models to JSON
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
"../"))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
from django.core import serializers
from django.db.models import get_model
from django.template.defaultfilters import slugify
MODELS_TO_DUMP = (
@dchaplinsky
dchaplinsky / httpdlogs.py
Created February 27, 2012 23:36
sysinfo plugin for PyMunin to parse last records in nginx/apache logs and gather stats about pageload time and amount of visits generated by search engine robots
"""Implements HttpdLog Class for gathering useful info from nginx/apache access logs.
"""
import re
from datetime import timedelta, datetime
import subprocess
__author__ = "Dmitry Chaplinsky"
__copyright__ = "Copyright 2012, Dmitry Chaplinsky"
__credits__ = []
#!/usr/bin/env python
import sys
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, os.path.join(PROJECT_ROOT, "../"))
from twisted.internet import reactor, task
from scrapy.crawler import Crawler
@dchaplinsky
dchaplinsky / Blackboard.tmTheme
Created January 3, 2013 21:42
Fixed Blackboard.tmTheme for Sublime Text 2 (now with proper coloring for Diff). Matches diff colouring from TextMate Blackboard theme.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Blackboard</string>
<key>author</key>
<string>Domenico Carbotta</string>
<key>settings</key>
<array>
@dchaplinsky
dchaplinsky / exporters.py
Created March 7, 2013 12:59
Improvement for original XmlItemExporter of scrapy to enable saving of dicts to xml (so free-form data can be saved to XML too).
from scrapy.contrib.exporter import XmlItemExporter
class DictXmlItemExporter(XmlItemExporter):
def _export_xml_field(self, name, serialized_value):
self.xg.startElement(name, {})
if hasattr(serialized_value, '__iter__'):
if isinstance(serialized_value, dict):
for key in serialized_value:
self._export_xml_field(key, serialized_value[key])