Skip to content

Instantly share code, notes, and snippets.

@Julian-Nash
Julian-Nash / flask_mongodb_bcrypt_user_login.py
Last active August 30, 2018 00:13
Simple user login route & password hashing/checking functions for flask using mongoengine and bcrypt
# mongoengine class
class User(DynamicDocument):
date_created = DateTimeField(default=datetime.datetime.utcnow)
username = StringField(unique=True)
password = BinaryField(required=True)
email = EmailField(unique=True)
admin = BooleanField(default=False)
data_sets = DictField()
# Hashing & password checking functions
@Julian-Nash
Julian-Nash / graph_temps.py
Created June 6, 2018 18:20
parsing temperature logger data from raspberry pi
import matplotlib.pyplot as plt
data_points = {}
with open("test_data.txt", "r") as f:
for line in f:
date, temp = line.split("|")
temp = temp.strip()
data_points.update({date: temp})
@Julian-Nash
Julian-Nash / resize.sh
Last active April 28, 2018 19:24
Python 3. Convert bulk images from any file type to PNG with progress bar and save in new directory
#!/bin/bash
arg=$1
red='\033[1;31m'
yellow='\033[1;33m'
cyan='\033[1;36m'
purple='\033[1;35m'
nl='\n'
nc='\033[0m'
@Julian-Nash
Julian-Nash / flask_session_wrapper.py
Last active August 30, 2018 00:06
Flask session wrapper - Control view access with this decorator
def in_session(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("LOGGED_IN") == False:
return redirect(url_for('login'))
return f(*args, **kwargs)
return decorated_function
# Usage --------------------------------------------------------
@Julian-Nash
Julian-Nash / validate_form.js
Created April 11, 2018 21:15
Form validation & fetch POST request using materialize.css & Javascript
// trim polyfill to clear whitespace
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
};
}
var submit_btn = document.getElementById("acc_form_btn");
submit_btn.addEventListener("click", function() {
@Julian-Nash
Julian-Nash / user_management.py
Last active August 19, 2021 21:50
Query MongoDB for account (Mongoengine) / Bcrypt password hashing / Hashed password validation
def has_account(email):
email = email.strip()
user_query = User.objects(email=email)
status = False
for user in user_query:
if user.email == email:
status = True
else:
status = False
return status
@Julian-Nash
Julian-Nash / send_gmail.py
Last active April 9, 2018 23:39
Python 3 gmail API example
# Follow guide on https://developers.google.com/gmail/api/quickstart/python to obtain credentials
import httplib2
import os
import oauth2client
from oauth2client import client, tools
import base64
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from apiclient import errors, discovery
@Julian-Nash
Julian-Nash / fetch_post.js
Last active April 3, 2018 12:55
Get form data on click and send json obj to server in fetch post. ES5 & ES6
// ES6
document.getElementById("post_btn").addEventListener("click", (e) => {
e.preventDefault();
let user_credentials = {
first_name: document.getElementById("first_name").value,
last_name: document.getElementById("last_name").value,
};
fetch("http://127.0.0.1:5000/testing_fetch", {
method: "POST",
@Julian-Nash
Julian-Nash / sessionStorageForm.js
Created March 9, 2018 17:53
Save and populate a form with sessionStorage data from a previously submitted form using jQuery
// Get the previous form data from sessionStorage
var previousForm = JSON.parse(sessionStorage.getItem("formData"));
// Iterate through the form fields and polulate the form with sessionStorage values
for (let item in previousForm) {
$(`#${item}`).val(`${previousForm[item]}`);
}
$("#submit").click(function(){
// Build sessionStorage object on form submit click
var formData = {
@Julian-Nash
Julian-Nash / cprint.py
Last active December 26, 2022 00:25
Print colored objects in the console depending on their type. Useful for development & debugging requests.
import click
import json
def cprint(x=None):
"""
Pass any object into cprint to have it printed to the console in color!
json = yellow (json is pretty printed by default)
Python collections [list, dict, tuple] = green
Integers & floats = magenta