Skip to content

Instantly share code, notes, and snippets.

View vitorfs's full-sized avatar

Vitor Freitas vitorfs

View GitHub Profile
from django.db import transaction
@transaction.atomic
def create_user_view(request):
if request.method == 'POST':
user_form = UserCreationForm(request.POST)
profile_form = ProfileForm(request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.refresh_from_db() # This will load the Profile created by the Signal
@vitorfs
vitorfs / change_password.html
Created August 4, 2016 09:08
Django Change Password View
<form method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Save changes</button>
</form>
@vitorfs
vitorfs / example.html
Last active August 23, 2023 05:44
Startswith Template Filter
{% load startswith %}
<li{% if request.path|startswith:'/settings/' %} class="active"{% endif %}>
@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 / admin.py
Created October 11, 2016 17:46
Removing "sites" from Django's Flatpages
from django.contrib import admin
from django.contrib.flatpages.admin import FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from django.utils.translation import ugettext_lazy as _
class CustomFlatPageAdmin(FlatPageAdmin):
fieldsets = (
(None, {'fields': ('url', 'title', 'content',)}),
(_('Advanced options'), {
'classes': ('collapse',),
@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
@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)
@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 / 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 / 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'}