Skip to content

Instantly share code, notes, and snippets.

View stavinsky's full-sized avatar

Anton Stavinsky stavinsky

View GitHub Profile
@stavinsky
stavinsky / pre_start
Created February 1, 2015 18:47
django openshift static files
#!/usr/bin/env bash
cd $OPENSHIFT_REPO_DIR/app
python manage.py migrate
mkdir -p $OPENSHIFT_DATA_DIR/static
ln -s $OPENSHIFT_DATA_DIR/static $OPENSHIFT_REPO_DIR/wsgi/static
python manage.py collectstatic --noinput
@stavinsky
stavinsky / app.yaml
Created October 7, 2014 13:00
cherrypy app engine example
application: app
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
@stavinsky
stavinsky / root.py
Created October 5, 2014 14:38
cherrypy if-modified-since example
import time
import cherrypy
now = [2014, 10, 4, 20, 45, 41, 5, 277, 0]
class Root():
def index(self):
cherrypy.response.headers['Last-Modified'] = cherrypy.lib.httputil.HTTPDate(time.mktime(tuple(now)))
cherrypy.lib.cptools.validate_since()
@stavinsky
stavinsky / js
Created October 2, 2014 10:05
cherrypy json jquery
//this is simple way to send almost any form data to cherrypy controller
$(function () {
$( "#save" ).click(function(){
var $form = $(this).closest('form');
$.ajax({
url: "/rest/save",
data: formDataToJSON($form),
type: 'POST',
contentType: 'application/json',
dataType: 'json',
@stavinsky
stavinsky / models.py
Last active August 29, 2015 14:07
sqlalchemy load data from json and export to json
##this is my way to import and export data to json
## actually not json, but dict. But you can convert json to dict very simple way:
## https://docs.python.org/2/library/json.html
class BaseExtend(object):
def to_dict(self):
d = dict()
for c in self.__table__.columns:
if isinstance(getattr(self, c.name), datetime):
d[c.name] = str(getattr(self, c.name)).split('.')[0]
else:
@stavinsky
stavinsky / gist:12234ba03be54308bbb4
Created October 1, 2014 07:21
CherryPy jinja2 decorator
# -*- coding: utf-8 -*-
"""
using:
class Root(BaseController):
''' Cherrypy Root class
'''
@jinja_template('main_page.html')
def index(self, lang=''):
@stavinsky
stavinsky / app.py
Created July 10, 2014 18:14
standalone cherrypy app for openshift
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cherrypy
## this have to be here to work on OpenShift environment
try:
zvirtenv = os.path.join(os.environ['OPENSHIFT_PYTHON_DIR'],
'virtenv', 'bin', 'activate_this.py')
execfile(zvirtenv, dict(__file__=zvirtenv))
except KeyError: