Skip to content

Instantly share code, notes, and snippets.

View Alir3z4's full-sized avatar
💭
Rock'nRolla

Alireza Savand Alir3z4

💭
Rock'nRolla
View GitHub Profile
@Alir3z4
Alir3z4 / decorator.py
Created June 1, 2013 16:45
Decorator to check whether user is super user or not If user is not a super-user, it will raise PermissionDenied or 403 Forbidden.
from functools import wraps
from django.core.exceptions import PermissionDenied
def superuser_required(method):
"""
Decorator to check whether user is super user or not
If user is not a super-user, it will raise PermissionDenied or
403 Forbidden.
"""

CockroachDB + Django ORM

cockroachdb/sqlalchemy-cockroachdb#14

TODO

  1. fork django/contrib/postgres driver
  2. patch blocking postgres compatibility issues
  3. review with cockroachdb team to see if any issues can be fixed by cockroach
  4. review with django team
  5. merge adapter as separate cockroachdb adapter
  6. write docs, any admin work, etc.
@dmutende
dmutende / oauth2_graphql_readme.txt
Last active January 16, 2020 16:35
Warpping GraphQL endpoints with Django OAuth2 Toolkit's ProtectedResourceView to secure them using OAuth2.0
- create a .py file with 2 classes (OAuth2ProtectedResourceMixin and OAuth2ProtectedGraph). see 'utils.py' file
- then in your Django's 'urls.py', wrap the graphql endpoint. see 'urls.py.
@kylefox
kylefox / post_compile.sh
Last active October 23, 2020 08:42
Run Django database migrations after deploy to Heroku. This file must live at `bin/post_compile` within your root project directory.
# !/usr/bin/env bash
# File path should be ./bin/post_compile
# (.sh extension added in Gist just to enable shell syntax highlighting.
# https://discussion.heroku.com/t/django-automaticlly-run-syncdb-and-migrations-after-heroku-deploy-with-a-buildpack-or-otherwise/466/7
echo "=> Performing database migrations..."
python manage.py migrate
@Alir3z4
Alir3z4 / middleware.py
Last active December 16, 2020 13:01
When running on AWS Elastic Beanstalk, we suffer an issue where HTTPS requests arriving at the load balancer are propagated to the individual hosts as HTTP requests. If the host issues a redirect it issues it using the same scheme as its incoming request (HTTP) when it should use HTTPS. This issue isn't unique to AWS EB, it's discussed in the co…
from django.http import HttpResponsePermanentRedirect
class SecureRequestPropagationMiddleware(object):
"""
When running on AWS Elastic Beanstalk, we suffer
an issue where HTTPS requests arriving at the load
balancer are propagated to the individual hosts as
HTTP requests. If the host issues a redirect it
issues it using the same scheme as its incoming
@Alir3z4
Alir3z4 / middleware.py
Created July 19, 2013 10:54
She's a Rock'nRoll Ruby! -- Simple IP blocker django middleware.
from django.conf import settings
from django import http
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip
@Alir3z4
Alir3z4 / gist:5498409
Created May 1, 2013 21:10
direct_to_template to TemplateView django1.3 to newer ;)
find: direct_to_template, \{'template': ('[a-z/.\w-]+')}
replace: TemplateView.as_view(template_name=$1)
def utf8ify(value):
if not value: return
# remove some characters altogether
value = re.sub(r'[?.,;/:"\'\|!@#~`+=$%^&\\*()\[\]{}<>]','',value, re.UNICODE)
return value
def utf8ify_slugify(value):
value = utf8ify(value)
value = value.replace(" ", "-")
@Alir3z4
Alir3z4 / forms.py
Created November 30, 2012 00:03
Override the init to pass the current user to the model form.
class Formio(ModelForm):
def __init__(self, user=None, *args, **kwrags):
"""
Override the init to pass the current user
to the model form.
Then the current user will be passed to the form at
initializing time in views or any other situation where
``request`` is available.
"""
# server {
# listen 80;
# server_name exampl.io www.exampl.io;
# access_log /srv/http/exampl.io/orgia/logs/access.log;
# error_log /srv/http/exampl.io/orgia/logs/error.log;
#
# #ssl on;
# #ssl_session_timeout 5m;
# #ssl_certificate /etc/ssl/certs/www.exampl.io.crt;
# #ssl_certificate_key /etc/ssl/private/www.exampl.io.key;