Skip to content

Instantly share code, notes, and snippets.

View agritheory's full-sized avatar

Tyler Matteson agritheory

View GitHub Profile
@agritheory
agritheory / token_auth_example.py
Last active March 4, 2020 01:14
Frappe Token Authorization Example
from requests import request
from base64 import b64encode
api_secret = get_decrypted_password("User", "example_username", "api_secret") # this is an internal frappe method
token = frappe.get_value("User", "example_username", "api_key") # this is an internal frappe method
url = "https://" + subdomain + ".some_domain.com"
headers = {
"Content-Type": "application/json",
"Authorization": "Basic "
+ b64encode(bytes(token + ":" + api_secret, "ascii")).decode("ascii"),
@agritheory
agritheory / Python Async Decorator.py
Created February 15, 2020 02:27 — forked from Integralist/Python Async Decorator.py
[Python Async Decorator] #python #asyncio #decorator
import asyncio
from functools import wraps
def dec(fn):
@wraps(fn)
async def wrapper(*args, **kwargs):
print(fn, args, kwargs) # <function foo at 0x10952d598> () {}
await asyncio.sleep(5)
@agritheory
agritheory / atom_extensions.md
Last active February 7, 2020 15:47
Atom Extensions
@agritheory
agritheory / README-Template.md
Created January 19, 2020 01:55 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@agritheory
agritheory / hooks.py
Created November 27, 2019 13:08
Basic document hook (snippets)
doc_events = {
"Sales Order": {
"on_submit": "my_custom_app.workflows.make_stock_entry_on_so_submit"
},
"on_cancel": "my_custom_app.workflows.cancel_stock_entry_on_so_cancel"
}
}
@agritheory
agritheory / gist:6c6117f9a7b9d3c15a99e4c0f22a8024
Created November 18, 2019 16:13
Patch a document method in Frappe
#this can be done in a either a document hook or
import types
import rounding_error_monkey_patch
doc = frappe.new_doc("Journal Entry")
# monkey patch validate_invoices method to bypass (incorrect) rounding error
doc.validate_invoices = types.MethodType(rounding_error_monkey_patch, doc)
return doc
@agritheory
agritheory / graphql_ariadne.py
Last active November 25, 2019 21:52
Quart/ Ariadne example
"""
# graphql query to run in the playground
{
hello
}
"""
from ariadne import QueryType, graphql, make_executable_schema
from ariadne.constants import PLAYGROUND_HTML
from quart import Quart, request, jsonify
@agritheory
agritheory / hook_workflow_action.py
Last active January 16, 2021 11:51
Hook onto the workflow action API
# Overriding Whitelisted Methods (in my_app/hooks.py)
# ------------------------------
override_whitelisted_methods = {
"frappe.model.workflow.apply_workflow": "my_app.workflows.apply_workflow"
}
# in my_app/workflows.py
import frappe
from frappe.model.workflow import apply_workflow as model_apply_workflow
import requests
from base64 import b64encode
import json
def push_po(doc, method):
# additional business logic or other validations
# prune document fields and turn into json
payload = json.loads({"docfield": doc})
api_key, api_secret = frappe.get_value("User", frappe.session.user, ["api_key", "api_secret"])
@agritheory
agritheory / so_print.py
Created January 30, 2019 01:21
Sales Order Print Template
{%- macro add_header(page_num, max_pages, doc, letter_head, no_letterhead) -%}
{% if letter_head and not no_letterhead %}
<div class="letter-head">{{ letter_head }}</div>
{% endif %}
{%- if doc.meta.is_submittable and doc.docstatus==0-%}
<div class="alert alert-info text-center">
<h4 style="margin: 0px;">{{ _("DRAFT") }}</h4></div>
{%- endif -%}
{%- if doc.meta.is_submittable and doc.docstatus==2-%}
<div class="alert alert-danger text-center">