Skip to content

Instantly share code, notes, and snippets.

@scupython
scupython / decorators.py
Created November 23, 2012 05:02 — forked from jsocol/decorators.py
A better @permission_required decorator for Django
try:
from functools import wraps
except ImportError:
from django.utls.functional import wraps
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseForbidden, HttpResponseRedirect
from django.template import loader, RequestContext, TemplateDoesNotExist
from django.utils.decorators import available_attrs
data:text/html, <style type="text/css">#e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div id="e"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("e");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
@app.route('/users/<username>', methods = ['GET', 'POST', 'PATCH', 'DELETE'])
@requires_auth
def api_user(username):
current_user = User.get(request.authorization.username)
app.logger.debug("req.auth.username = %s", request.authorization.username)
# Only "SELF" can access/modify user info
if current_user.username != username:
return unauthorized()
callback: function(opts, event) {
if (opts.primary) {
function getAddHostTag(cb) {
console.log(cb);
cb('before ajax');
return $.ajax({
url: "/api/addhosttag/",
dataType: 'json',
data: {hostid: m.id, tagname: tagname},
type: "POST",
@scupython
scupython / nagios_config
Created July 15, 2013 15:08
wrapper for add nagios config file,
def check_config_reload_or_del(del_func):
def wrap(add_func):
def wrapped_f(*args, **kwargs):
# check config before add config file
if check_nagios_config():
if add_func(*args, **kwargs):
# check config after add config file
if check_nagios_config():
if reload_nagios():
return (True,
@scupython
scupython / get_host_info.py
Created July 16, 2013 06:18
get all host info which belong to a game of which game type is elex
# -*- coding: utf-8 -*-
from dbase import db_session
from gametype import GameType
from game import Game
from app import App
from apphostmap import AppHostMap
from host import Host
from user import User
from currency import Currency
@scupython
scupython / get_softlayer_info.py
Created July 16, 2013 06:22
fetch info from softlayer api
# -*- coding: utf-8 -*- #
import SoftLayer.API
from models.host import Host
from models.dbase import db_session as session
api_username = 'XXXXX_NAME'
api_key = 'XXXXXXX_KEY'
client = SoftLayer.API.Client('SoftLayer_Account', None, api_username, api_key)
hs = client.getHardware()
server_ids = [h['id'] for h in hs]
@scupython
scupython / load_nagios.py
Last active December 19, 2015 23:29
load nagios config to new system
import simplejson as json
from flask_application.models.nagcmd import NagCMD
from flask_application.models.dbase import db_session
from flask_application.models.host import Host
from flask_application.models.app import App
from flask_application.models.hostmonitem import HostMonItem
with open('nagiosconfig_orig', 'r') as f:
fc = json.load(f)
ef = open('host_err.txt', 'w')
from flask_application.elexlib.nagconf import NagConf
from flask_application.models.dbase import db_session
from flask_application.models.hostmonitem import HostMonItem
import time
nc = NagConf()
hms = db_session.query(HostMonItem).all()
for hm in hms:
time.sleep(0.2)
print 'sleeping'
added, msg = nc.add_service(
@scupython
scupython / get_nagios.py
Last active December 20, 2015 01:39
get nagios configure from nagiosql
import simplejson as json
import MySQLdb
from MySQLdb.cursors import DictCursor
import sqlalchemy.pool as pool
qServiceItem = ['id', 'config_name', 'service_description', 'check_command', 'max_check_attempts', 'check_interval', 'retry_interval']
def get_conn():
c = MySQLdb.connect("localhost", "nagiosql",
"db@xingcloud", "XXXXXX",