Skip to content

Instantly share code, notes, and snippets.

@anhtran
anhtran / urlify.js
Created May 7, 2013 03:34
Add Vietnamese character map for urlify.js of Django Admin (generator of slug fields)
var LATIN_MAP = {
'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'AE', 'Ç':
'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I', 'Î': 'I',
'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O', 'Õ': 'O', 'Ö':
'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U', 'Ü': 'U', 'Ű': 'U',
'Ý': 'Y', 'Þ': 'TH', 'ß': 'ss', 'à':'a', 'á':'a', 'â': 'a', 'ã': 'a', 'ä':
'a', 'å': 'a', 'æ': 'ae', 'ç': 'c', 'è': 'e', 'é': 'e', 'ê': 'e', 'ë': 'e',
'ì': 'i', 'í': 'i', 'î': 'i', 'ï': 'i', 'ð': 'd', 'ñ': 'n', 'ò': 'o', 'ó':
'o', 'ô': 'o', 'õ': 'o', 'ö': 'o', 'ő': 'o', 'ø': 'o', 'ù': 'u', 'ú': 'u',
'û': 'u', 'ü': 'u', 'ű': 'u', 'ý': 'y', 'þ': 'th', 'ÿ': 'y'
@anhtran
anhtran / secure_url.py
Last active December 17, 2015 04:18
In some cases, you don't want to use the middleware. These tags will be useful to navigate users to HTTPS or not. I modified them from Django source files.
from django.template import Library, TemplateSyntaxError
from django.template.defaulttags import kwarg_re
from django.template.base import Node
from django.utils.encoding import smart_str
from django.conf import settings
from django.contrib.sites.models import Site
register = Library()
@anhtran
anhtran / animate_looper.css
Created May 13, 2013 09:42
How many times you want to repeat the effects from animate.css?
/*
USAGE: <div class="swing animated loop3"></div>
*/
.loop1 {
animation-iteration-count:1;
-moz-animation-iteration-count:1;
-webkit-animation-iteration-count:1;
}
.loop2 {
# -*- coding: utf-8 -*-
import Image
def resize_and_crop(img_path, modified_path, size, crop_type='top'):
"""
Resize and crop an image to fit the specified size.
args:
img_path: path for the image to resize.
@anhtran
anhtran / slugify_vietnamese.py
Last active December 20, 2015 19:38
Create slug for URL from string in Django. Fixing the issue with character "đ" in Vietnamse.
# -*- coding: utf-8 -*-
from django.utils.text import slugify
def slugify_vietnamese(value):
value = value.replace(u'đ', 'd').replace(u'Đ', 'D')
return slugify(value)
@anhtran
anhtran / recaptcha.css
Created August 9, 2013 17:57
Use this hack to reCaptcha. It should be clean and in style Bootstrap.
.recaptchatable {
line-height: inherit;
border: none !important;
}
.recaptchatable #recaptcha_image {
margin: auto;
border: none !important;
}
@anhtran
anhtran / build_uwsgi.sh
Created August 11, 2013 05:30
Shell script for installing uWSGI with nginx at servers of Webfaction. We also can modify a bit to use in a VPS/Cloud server. Try command `dos2unix` if get some errors.
#!/bin/bash
# Set environment variables
APPNAME=uwsgi_app # Name of the uWSGI Custom Application
APPPORT=12345 # Assigned port for the uWSGI Custom Application
PYTHON=python2.7 # Django python version
DJANGOAPP=django_app # Django application name
DJANGOPROJECT=my_project # Django project name
mkdir -p $HOME/webapps/$APPNAME/{bin,nginx,src,tmp}
@anhtran
anhtran / facebook_tags.py
Created August 16, 2013 22:08
Django template tags: check the request agent is Facebook.
from django import template
register = template.Library()
@register.assignment_tag(takes_context=True)
def is_facebook(context):
request = context.get('request')
a = request.META['HTTP_USER_AGENT']
f = ['facebookexternalhit/1.1 (+https://www.facebook.com/externalhit_uatext.php)',
@anhtran
anhtran / profile.js
Last active December 30, 2015 16:39
Seperate models file for Express App + Mongooes
var ProfileProvider = require('./profile_models').ProfileProvider;
var ProfileModel = new ProfileProvider();
exports.all_profiles = function(req, res) {
ProfileModel.findAll(function(profiles){
res.render('profiles', {
profile_list: profiles
});
});
};
@anhtran
anhtran / md_cheatsheet.md
Created April 26, 2014 06:46
Markdown Cheatsheet

This is intended as a quick reference and showcase. For more complete info, see John Gruber's original spec and the Github-flavored Markdown info page.

This cheatsheet is specifically Markdown Here's version of Github-flavored Markdown. This differs slightly in styling and syntax from what Github uses, so what you see below might vary a little from what you get in a Markdown Here email, but it should be pretty close.

You can play around with Markdown on our live demo page.

(If you're not a Markdown Here user, check out the Markdown Cheatsheet that is not specific to MDH. But, really, you should also use Markdown Here, because it's awesome. http://markdown-here.com)

Table of Contents

Headers