Skip to content

Instantly share code, notes, and snippets.

Avatar
🛠️
Building things

Fauzan Erich Emmerling femmerling

🛠️
Building things
View GitHub Profile
@femmerling
femmerling / parser.py
Created Jan 22, 2014
Have problems checking class variable type to ensure that value will be typecasted accordingly
View parser.py
def parser(passed_object, request_data):
for item in request_data.values:
if hasattr(passed_object, item) and request_data.values.get(item) != None:
inputval = request_data.values.get(item)
#TODO: check the corresponding class variable type before typecasting
setattr(passed_object, item, inputval)
return passed_object
@femmerling
femmerling / parse_helper.py
Created Oct 11, 2013
Function to simplify json parsing for SQLAlchemy model creation
View parse_helper.py
"""
I know that parsing json values for SQLAlchemy models is a pain in the ass.
The following two helpers will get you cleaner code and handles the json while converting it into code.
This is commonly use in Flask-SQLAlchemy applications. My latest backyard release already included these functions
"""
import json
# this import is just an example of an SQLAlchemy module, it can be any model that you wish to use
from app.models import User
@femmerling
femmerling / app.py
Created Sep 9, 2013
Always error and class instance always created twice
View app.py
from flask import Flask, jsonify
from oauth1.authorize import Oauth1
from oauth1.errors.oauth import Oauth1Errors
from oauth1.store.sql import Oauth1StoreSQLAlchemy
BASE_URL = "http://localhost:5000/"
auth = None
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:somepassword@127.0.0.1:3306/oauth" # Change this to a valid URI
@femmerling
femmerling / ftpserver.py
Created Jun 5, 2013
Simple python ftp server with authentication
View ftpserver.py
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
authorizer = DummyAuthorizer()
authorizer.add_user("user", "password", "/home/user", perm="elradfmw")
authorizer.add_anonymous("/home/nobody")
handler = FTPHandler
handler.authorizer = authorizer
@femmerling
femmerling / owncloudUserCreateHack.php
Created Apr 3, 2013
Put this file in your OwnCloud root folder to have a create new user API. All you need to do is have a POST request to <host>/owncloud/owncloudUserCreateHack.php with username and password parameters and your user is created. Remember that this works under SQLite3. Adjust to MySQL or PostgreSQL or whatever engine query you're using.
View owncloudUserCreateHack.php
<?php
require_once '3rdparty/phpass/PasswordHash.php';
class MyDB extends SQLite3
{
function __construct()
{
$this->open('data/owncloud.db');
}
@femmerling
femmerling / authenticate.py
Last active Jun 13, 2022
I have to create user authentication using python-ldap. After googling around and trying out stuffs, this is the final code for you to use. Please remember to adjust the user_dn, and base_dn accordingly to the format used in your LDAP server.
View authenticate.py
# to be able to import ldap run pip install python-ldap
import ldap
if __name__ == "__main__":
ldap_server="x.x.x.x"
username = "someuser"
password= "somepassword"
# the following is the user_dn format provided by the ldap server
user_dn = "uid="+username+",ou=someou,dc=somedc,dc=local"
@femmerling
femmerling / main.py
Created Feb 28, 2013
fixed home routing
View main.py
# home root controller
# @app.route('/')
# def index():
# # define your controller here
# return render_template('welcome.html')
@app.route('/') #Link
def home_control():
# add your controller here
return render_template('home.html')
@femmerling
femmerling / base.html
Last active Dec 14, 2015
base.html fix
View base.html
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>My Website</title>
<meta name="description" content="">
@femmerling
femmerling / main.py
Last active Dec 14, 2015
Editing contact/add routing
View main.py
@app.route('/contact/')
def contact_add_controller():
#this is the controller to add new model entries
return render_template('contact_add.html', title = "Add New Entry")
@app.route('/contact/create/',methods=['POST','GET'])
def contact_create_data_controller():
# this is the contact data create handler
contact_name = request.values.get('contact_name')
contact_email = request.values.get('contact_email')
@femmerling
femmerling / main.py
Created Feb 28, 2013
Edit contacts routing
View main.py
@app.route('/contact/view-all')
def contact_view_controller():
#this is the controller to view all data in the model
contact_list = Contact.query.all()
if contact_list:
contact_entries = [contact.dto() for contact in contact_list]
else:
contact_entries = None