Skip to content

Instantly share code, notes, and snippets.

View vitorfs's full-sized avatar

Vitor Freitas vitorfs

View GitHub Profile
vitorfs /
Last active November 12, 2019 01:14
Collect page views from Google Analytics
import argparse
from apiclient.discovery import build
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from web.api.models import Article, PageView
vitorfs /
Created November 9, 2018 20:13
Serving Django Media Files During Development
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# Project url patterns...
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
vitorfs /
Created October 17, 2018 12:53
Markdown -> HTML Compiler
import argparse
import os
import pathlib
import shutil
import sys
import markdown
exclude_dirs = {'_build'}
exclude_files = {''}
vitorfs /
Created November 7, 2017 12:57
Stop and remove all Docker containers and images
docker stop $(docker ps -a -q)
docker rm -f $(docker ps -a -q)
docker rmi -f $(docker images -q)
vitorfs /
Created October 30, 2017 09:27
Generate secret key
from django.utils.crypto import get_random_string
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
random_string = get_random_string(50, chars)
from django import template
register = template.Library()
def fieldtype(field):
return field.field.widget.__class__.__name__
vitorfs /
Created September 25, 2017 15:08
cloudflare-like sign up
from django import forms
from django.contrib.auth.models import User
class SignUpForm(forms.ModelForm):
username = models.CharField(
label='Email address',
vitorfs /
Created March 21, 2017 14:13
Disqus Python API Latest Comments
from django.conf import settings
from django.utils.dateparse import parse_datetime
from disqusapi import DisqusAPI
from .utils import smart_safe_excerpt
def get_latest_comments(limit=5):
disqus = DisqusAPI(settings.DISQUS_SECRET_KEY, settings.DISQUS_PUBLIC_KEY)
"name":"Hacker News",
vitorfs /
Last active June 25, 2022 14:51
Handling GitHub Webhooks Using Django
import hmac
from hashlib import sha1
from django.conf import settings
from django.http import HttpResponse, HttpResponseForbidden, HttpResponseServerError
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
from django.utils.encoding import force_bytes
import requests