Skip to content

Instantly share code, notes, and snippets.

View mariocesar's full-sized avatar

Mario-César mariocesar

View GitHub Profile
@mariocesar
mariocesar / models.py
Last active December 12, 2015 00:29
Conveniently register model methods as constraints, that will check before saving the model
from functools import wraps
from django.db import models
from django.core.exceptions import ValidationError
class ConstraintModel(models.Model):
class Meta:
abstract = True
def _fill_constraints_register(self):
@mariocesar
mariocesar / ticket_function.sql
Last active December 12, 2015 00:48
PostgreSQL, ticketing system to use numbered sequences
-- returns and update the next value for a named ticket.
CREATE OR REPLACE FUNCTION get_ticket(ticket_name varchar(50)) RETURNS INTEGER AS $$
DECLARE
ticket INTEGER; --
BEGIN
IF EXISTS (SELECT * FROM ticketing_ticket WHERE name = ticket_name FOR UPDATE) THEN
UPDATE ticketing_ticket SET currval = currval + incval
WHERE name = ticket_name RETURNING currval INTO ticket; --
RETURN ticket; --
@mariocesar
mariocesar / grub.txt
Last active December 12, 2015 05:58
Boot multiple live-cds from a USB Stick
## Boot multiple live-cds from a USB
##
# First install grub on the usb, for example:
#
# $ sudo grub-install --no-floppy --force --root-directory=/media/E310-C779 /dev/sdc1
# Get some of the isos you want to boot
#
# $ ls
@mariocesar
mariocesar / storage.py
Last active November 6, 2024 16:57
Django custom storage backend that applies optimizations for png and jpg images
from django.contrib.staticfiles.storage import CachedFilesMixin, StaticFilesStorage
from pipeline.storage import PipelineMixin
from functools import wraps
import shutil
import re
import subprocess
def coroutine(func):
@wraps(func)
@mariocesar
mariocesar / _runserver.py
Last active December 12, 2015 09:19
Conditionaly register a management command in Django. In this example if compass is available on the system, it will load a custom version of runserver to run the compass watch process and the runserver command at the same time.
import os
import subprocess
from optparse import make_option
from django.core.management.commands.runserver import BaseRunserverCommand
from django.conf import settings
class Command(BaseRunserverCommand):
option_list = BaseRunserverCommand.option_list + (
@mariocesar
mariocesar / website.yml
Created February 20, 2013 00:12
An idea in progress. #jekyll #pelican. A more convenient way to make static sites
---
site_name: C1.com.bo
description: C1
keywords: design, bolivia
build_dir: htdocs
apps:
blog:
handler: constructor.handlers.BlogHandler
@mariocesar
mariocesar / changegroup.sh
Created February 27, 2013 02:55
Mercurial Hook, update the WD if the commit has a a new node for the 'release' branch
#!/usr/bin/env bash
hg diff --stat -r $HG_NODE -r tip
BRANCH=$(hg log --template '{branches}' -r $HG_NODE)
if [ "$BRANCH" == "release" ] ; then
hg update --clean release >&2
fi
@mariocesar
mariocesar / admin.py
Last active December 14, 2015 16:38
A generic view to create a web resource, suitable to use with Backbone #django #backbone #generic-views
from project import api
from .resources import UserResource
api.resources.register(UserResource)
@mariocesar
mariocesar / notes.sh
Created March 17, 2013 04:58
Ubuntu Touch tools for 12.04
# As http://developer.ubuntu.com/get-started/gomobile/ says
sudo add-apt-repository ppa:canonical-qt5-edgers/qt5-proper
sudo add-apt-repository ppa:ubuntu-sdk-team/ppa
sudo apt-get update
sudo apt-get install ubuntu-sdk notepad-qml
# On ubuntu 12.04 qmake 3 is set as the default alternative for qmake,
# I was no able to start correctly the qt-designer, so I search the
# qmake
@mariocesar
mariocesar / string_helpers.js
Last active December 18, 2015 00:18
#javascript #jquery #trim I get tired of removing the blank spaces everywhere, ... If I just realize months ago that I can do this !
// Load after jquery.js
// Instead of $('#my_input').val(), do $('#my_input').trimmed_val()
(function() {
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, '');};
String.prototype.ltrim = function(){return this.replace(/^\s+/,'');};
String.prototype.rtrim = function(){return this.replace(/\s+$/,'');};