Skip to content

Instantly share code, notes, and snippets.

@otov4its
otov4its / python_parse_hashtags.py
Last active July 12, 2016 13:50
This gist parses 99% twitter-like hashtags in unicode
# -*- coding: utf-8 -*-
import re
# Because 're' module do not support \p{L} unicode class
# need to use simplistic regex with \w instead \p{L}. But such regexp
# also match digits like #123, #003 and so on.
# So it need to further filter out digit-only tags.
# See https://regex101.com/r/cK5oJ0/
HASHTAG_EXP = r'(?:^|_|[^\w&/]+)(?:#|#)([\wÀ-ÖØ-öø-ÿ]+)'
HASHTAG_REGEX = re.compile(HASHTAG_EXP, re.UNICODE | re.IGNORECASE)
@otov4its
otov4its / django_csrf.js
Created October 7, 2016 07:23
django ajax CSRF
$(function() {
// This function gets cookie with a given name
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
@otov4its
otov4its / null_char_field.py
Last active January 16, 2017 15:48
Django NullCharField subclass of the CharField that allows empty strings to be stored as NULL in database
from django.db.models import CharField
from django.core.exceptions import ImproperlyConfigured
from django.utils.translation import ugettext_lazy as _
class NullCharField(CharField):
"""
Subclass of the CharField that allows
empty strings to be stored as NULL in database
"""