Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View vitorfs's full-sized avatar

Vitor Freitas vitorfs

View GitHub Profile
@vitorfs
vitorfs / google_analytics.py
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
vitorfs / urls.py
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
vitorfs / mdcompiler.py
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 = {'README.md'}
@vitorfs
vitorfs / docker_cleanup.sh
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
vitorfs / secret.py
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)
print(random_string)
from django import template
register = template.Library()
@register.filter('fieldtype')
def fieldtype(field):
return field.field.widget.__class__.__name__
@vitorfs
vitorfs / forms.py
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(
widget=forms.EmailInput(),
label='Email address',
required=True,
max_length=150
)
@vitorfs
vitorfs / disqus.py
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)
[
{
"fields":{
"status":"G",
"name":"Hacker News",
"url":"https://news.ycombinator.com/news",
"last_run":"2017-02-22T02:11:16.279Z",
"story_url":"https://news.ycombinator.com/item?id=",
"slug":"hn"
},
@vitorfs
vitorfs / github_webhooks.py
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