Skip to content

Instantly share code, notes, and snippets.

View BenAtWide's full-sized avatar
🌇
Skiff

Ben Edwards BenAtWide

🌇
Skiff
View GitHub Profile
@BenAtWide
BenAtWide / DraftsCredential.js
Created August 25, 2021 05:10
Use Drafts Credential object to ask a user for a secret the first time it is used, and store it securely in the app. This makes it easier to share scripts without requiring embedded secrets in the code.
// Use the Drafts Credential function to store the API key
var credential = Credential.create("Mem.ai", "The mem.ai web API. Generate your key in the Flows section of the mem app.");
credential.addTextField("apikey", "API Key");
credential.authorize();
// Retrieve the credential and add to the request headers
var headers = {
"Content-Type": "application/json",
"Authorization": "ApiAccessToken " + credential.getValue("apikey")
@BenAtWide
BenAtWide / list_perms.py
Created March 2, 2020 14:22
List all Django permissions in an app, as a management command.
from django.contrib import auth
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = 'Get a list of all permissions available in the system.'
def handle(self, *args, **options):
permissions = set()
@BenAtWide
BenAtWide / docker.fish
Last active February 14, 2020 15:29
Handy docker commands in fish
# get rid of docker containers that exited with status 1
docker rm (docker ps -a -q --filter 'exited=1')
# connect to a running container, run bash cos probably no fish :-(
docker container exec -it <container name or id> bash
# or, to just run a command
docker exec -i <container name or id> sh -c "command arg args"
# Connect to an image, and have a poke around...
docker run --rm -it --entrypoint=/bin/bash <image name>
# Install compose
from django.db import migrations
def combine_names(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
Person = apps.get_model('yourappname', 'Person')
for person in Person.objects.all():
person.name = '%s %s' % (person.first_name, person.last_name)
person.save()
content_type = ContentType.objects.get(app_label='', model='')
#get all permssions for this model
perms = Permission.objects.filter(content_type=content_type)
group = Group.objects.get(name='')
for p in perms:
group.permissions.add(perms)
#!/usr/bin/python
import subprocess
def asrun(ascript):
"Run the given AppleScript and return the standard output and error."
osa = subprocess.Popen(['osascript', '-'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
@BenAtWide
BenAtWide / python-anywhere.wsgi
Created March 24, 2017 12:17
basic wsgi file for PythonAnywhere
# This file contains the WSGI configuration required to serve up your
# web application at http://simplicat.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# +++++++++++ GENERAL DEBUGGING TIPS +++++++++++
# getting imports and sys.path right can be fiddly!
# We've tried to collect some general tips here:
# https://www.pythonanywhere.com/wiki/DebuggingImportError
@BenAtWide
BenAtWide / django-render-email.py
Last active March 15, 2017 11:24
Send an email generated from a template.
from django.core.mail import send_mail
from django.template.loader import render_to_string
def send_rendered_email(recipients=[]):
# send an email to a list of recipients - NB it's a list!
ctx = {'var': 'val'} # context dict
text_body = render_to_string('path/to/template.txt', ctx)
html_body = render_to_string('path/to/template.html', ctx)
if send_mail("My subject", text_body, from_email, recipients, html_message=html_body):
return "Mail sent"
@BenAtWide
BenAtWide / export_issues.py
Created September 16, 2016 10:21 — forked from reberhardt7/export_issues.py
Github Issues Export: This script exports all issues from a repository, along with comments and events, into a JSON file. It also produces a Markdown file that can be used to easily view the issues.
"""
This script uses Github's API V3 with Basic Authentication to export issues from
a repository. The script saves a json file with all of the information from the
API for issues, comments, and events (on the issues), downloads all of the
images attached to issues, and generates a markdown file that can be rendered
into a basic HTML page crudely mimicking Github's issue page. If the gfm module
is available, the script will go ahead and render it itself.
In the end, you'll be left with a folder containing a raw .json file (which you
can use to extract information for your needs, or to import it somewhere else),
#!/usr/bin/env python
#
# Very simple Python script to dump all emails in an IMAP folder to files.
# This code is released into the public domain.
#
# RKI Nov 2013
#
import sys
import imaplib
import getpass