Skip to content

Instantly share code, notes, and snippets.

@brianlittmann
brianlittmann / aws_uploader.rb
Last active May 26, 2016 17:38
Save image versions with Refile with streaming as a fallback
class AwsUploader
CLIENT = Aws::S3::Client.new
BUCKET = Rails.configuration.x.aws["output_bucket"]
def self.upload(file, key)
CLIENT.put_object({
bucket: BUCKET,
key: key,
body: file,
content_length: file.size
@brianlittmann
brianlittmann / datetime_range_formatter.rb
Last active August 29, 2015 14:22
Display summary of Ruby datetimes as range or as single day.
def formatted_datetime_range(start_datetime, end_datetime)
start_date = start_datetime.to_date rescue nil
end_date = end_datetime.to_date rescue nil
return "" if start_date.nil?
is_all_day = (start_datetime == start_datetime.beginning_of_day)
is_this_year = (start_datetime.year == Time.zone.today.year)
if end_date && start_date != end_date
@brianlittmann
brianlittmann / restore_pg_table
Last active August 29, 2015 14:06
Replace data in a single PG table.
### Source
# shell:
pg_dump -U username --data-only --table=tablename dbname > tablename.sql
### Target
# psql:
delete from tablename *;
# shell:
psql -U username dbname < tablename.sql
@brianlittmann
brianlittmann / LoginRequiredMiddleware.py
Created July 15, 2013 15:20
Django middleware component that wraps the login_required decorator around all URL patterns be default, with exceptions. Can also require user to belong to a group ("admin" in this gist) or be adapted if using the Django admin app.
"""
Middleware component that wraps the login_required decorator around all URL patterns be default, with exceptions.
Define PUBLIC_URLS and ADMIN_URLS using regex in settings.py, where:
PUBLIC_URLS do not require user to be logged in.
ADMIN_URLS require user to be in admin group.
Source: http://stackoverflow.com/a/2164224/720054
"""
# settings.py
PUBLIC_URLS = (
@brianlittmann
brianlittmann / EmailBackend.py
Last active August 2, 2017 04:34
Remove Python's smtplib CRAM-MD5 authentication method if getting SMTPAuthenticationError: (535, 'Authentication failed') and you know credentials are correct.
# settings.py
EMAIL_BACKEND = 'backends.SMTPEmailBackend'
# backends.py
"""Custom SMTP email backend class"""
import smtplib
from django.core.mail.utils import DNS_NAME
from django.core.mail.backends.smtp import EmailBackend
class SMTPEmailBackend(EmailBackend):