Skip to content

Instantly share code, notes, and snippets.

View FZambia's full-sized avatar

Alexander Emelin FZambia

View GitHub Profile
@FZambia
FZambia / enum_choices.py
Last active December 30, 2015 00:09
create choices in django using enums
"""
usage:
Event.objects.create(type=Event.TYPES.CHAT_ADDED)
"""
def enum(*sequential, **named):
enums = dict(zip(sequential, range(len(sequential))), **named)
reverse = dict((value, key) for key, value in enums.iteritems())
enums['reverse_mapping'] = reverse
#
# Slightly tighter CORS config for nginx
#
# A modification of https://gist.github.com/1064640/ to include a white-list of URLs
#
# Despite the W3C guidance suggesting that a list of origins can be passed as part of
# Access-Control-Allow-Origin headers, several browsers (well, at least Firefox)
# don't seem to play nicely with this.
#
// Example
package main;
import (
"jsonhelper"
"fmt"
)
func main(){
jsonValue := []interface {}{
@FZambia
FZambia / main.go
Last active August 29, 2015 13:57
Example how to override Martini logger using Martini fork
package main
import (
"log"
"os"
"github.com/FZambia/martini"
)
func main() {
@FZambia
FZambia / web.py
Created April 18, 2014 13:24
minimal django
# to run:
# python web.py runserver
from django.conf import settings
from django.conf.urls import patterns
from django.http import HttpResponse
if not settings.configured:
settings.configure(
DEBUG=True,
ROOT_URLCONF='web',
@FZambia
FZambia / admin.py
Last active August 29, 2015 14:00
django custom user
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.utils.translation import ugettext_lazy as _
from users.forms import UserChangeForm, UserCreationForm
from users.models import User
class UserAdmin(BaseUserAdmin):
fieldsets = (
@FZambia
FZambia / storage.py
Created April 28, 2014 09:04
webdav storage backend for django
# coding: utf-8
from urlparse import urljoin
from django.conf import settings
from django.core.files.storage import Storage
import logging
from urlparse import urlparse
import requests
@FZambia
FZambia / tunnel.md
Last active May 18, 2016 09:44
making local port available through port on another machine via SSH tunneling
  1. Add into /etc/ssh/sshd_config on remote machine (remote.example.com):
GatewayPorts yes
  1. Restart sshd:
/etc/init.d/sshd restart 
# Copyright (c) 2014 Andrey Vlasovskikh
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
@FZambia
FZambia / image_download.py
Last active October 10, 2020 07:02
Download image from url for Django
import logging
import requests
from PIL import Image
import requests.exceptions
from StringIO import StringIO
from django.core.files.base import ContentFile
from django.core.files.storage import FileSystemStorage
logger = logging.getLogger('uploads')