Skip to content

Instantly share code, notes, and snippets.

@shihweilo
shihweilo / django_upload_image_from_url_to_imagefield.py
Last active September 8, 2020 09:25
Django upload image from URL to ImageField
# reference: https://goodcode.io/articles/django-download-url-to-file-field/
from tempfile import TemporaryFile
from os.path import basename
from urllib.parse import urlsplit
from django.core.files import File
import requests
class Foo(models.Model):
/**
* Sanitizer which filters a set of whitelisted tags, attributes and css.
* For now, the whitelist is small but can be easily extended.
*
* @param bool whether to escape or strip undesirable content.
* @param map of allowed tag-attribute-attribute-parsers.
* @param array of allowed css elements.
* @param array of allowed url scheme
*/
function HtmlWhitelistedSanitizer(escape, tags, css, urls) {
@shihweilo
shihweilo / metaclasses.py
Created March 1, 2016 16:47
Django: Useful default manager override
# -*- coding: utf-8 -*-
# copied from django-cms
# https://github.com/divio/django-cms/blob/develop/cms/models/metaclasses.py
from cms.publisher.manager import PublisherManager
from django.db.models.base import ModelBase
class PageMetaClass(ModelBase):
@shihweilo
shihweilo / shopify_webhook.py
Last active August 29, 2015 14:14
Django view decorator for the HMAC verification of a Shopify Webhook.
# modified from https://gist.github.com/gavinballard/8513270
from functools import wraps
import hashlib
import base64
import hmac
import json
from django.conf import settings