Skip to content

Instantly share code, notes, and snippets.

Brian Littmann brianlittmann

View GitHub Profile
@brianlittmann
brianlittmann / restore_pg_table
Last active Aug 29, 2015
Replace data in a single PG table.
View restore_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 / datetime_range_formatter.rb
Last active Aug 29, 2015
Display summary of Ruby datetimes as range or as single day.
View datetime_range_formatter.rb
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 / aws_uploader.rb
Last active May 26, 2016
Save image versions with Refile with streaming as a fallback
View aws_uploader.rb
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 / EmailBackend.py
Last active Aug 2, 2017
Remove Python's smtplib CRAM-MD5 authentication method if getting SMTPAuthenticationError: (535, 'Authentication failed') and you know credentials are correct.
View EmailBackend.py
# 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):
@brianlittmann
brianlittmann / LoginRequiredMiddleware.py
Created Jul 15, 2013
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.
View LoginRequiredMiddleware.py
"""
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 = (
You can’t perform that action at this time.