Skip to content

Instantly share code, notes, and snippets.

View psychok7's full-sized avatar

Nuno Khan psychok7

View GitHub Profile
@psychok7
psychok7 / docker_commands.sh
Last active May 13, 2018 20:51
Docker commands
Source: https://vimeo.com/133154447
Source: https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/
# if you run into problems when creating try docker-machine regenerate-certs dev
docker-machine create --driver virtualbox dev
# run commands in your created VM
eval "$(docker-machine env dev)"
# List available machines and check their Ip's
docker-machine ls
@psychok7
psychok7 / gist:36d7e0b78abcb95a9741
Created March 2, 2015 21:26
Django Rest Framework + Django Hvad Usage Example
class MissionSerializer(TranslatableModelSerializer):
mission = serializers.CharField(source='mission')
class Meta:
model = Mission
class MissionViewSet(viewsets.ModelViewSet):
queryset = Mission.objects.language().all()
serializer_class = MissionSerializer
authentication_classes = (NoAuthentication,)
@psychok7
psychok7 / gist:50f00e2621450254c3da
Created March 2, 2015 21:19
"Glue" for Django Rest Framework + Django Hvad
# hvad compatibility for rest_framework - JHA
class TranslatableModelSerializerOptions(serializers.ModelSerializerOptions):
def __init__(self, meta):
super(TranslatableModelSerializerOptions, self).__init__(meta)
# We need this ugly hack as ModelSerializer hardcodes a read_only_fields check
self.translated_read_only_fields = getattr(meta, 'translated_read_only_fields', ())
self.translated_write_only_fields = getattr(meta, 'translated_write_only_fields', ())
class HyperlinkedTranslatableModelSerializerOptions(serializers.HyperlinkedModelSerializerOptions):
@psychok7
psychok7 / gist:2016cffe6322801ca876
Last active August 29, 2015 14:06
Django Amazon S3 Settings
DEFAULT_FILE_STORAGE = 'app.s3utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'app.s3utils.StaticRootS3BotoStorage'
AWS_ACCESS_KEY_ID = 'XXXXXXXXXXX'
AWS_SECRET_ACCESS_KEY = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY'
AWS_STORAGE_BUCKET_NAME = 'app-bucket'
AWS_S3_SECURE_URLS = False # use http instead of https
AWS_QUERYSTRING_AUTH = False # we dont have any private files
S3_URL = 'http://s3-eu-west-1.amazonaws.com/%s' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = '/media/'
@psychok7
psychok7 / gist:dc04a308a2cb10544b79
Created September 7, 2014 18:15
CORS CONFIGURATION
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
@psychok7
psychok7 / check_ip.py
Last active December 29, 2015 11:59
Utility to check changes of Dynamic IP addresses. It emails the user every 6 hours if the IP changes in that period. This is better then having a domain because you computer is a bit more secure since only you know the IP to access it.
import subprocess
import time
import smtplib
import string
# Author Nuno Khan
# Utility to check dynamic ip addresses
def save_new_Ip(p):
ip=""
@psychok7
psychok7 / strip_html.py
Last active December 18, 2015 02:59
Django StripHtml Mixin with regular expressions to help avoid XSS
import re
class StripHtmlMixin(object):
#based on http://stackoverflow.com/a/3398894/977622
def strip_html(self, data):
p = re.compile(r'<.*?>')
return p.sub('', data)
@psychok7
psychok7 / periodictask.py
Last active December 17, 2015 10:19
Using celery periodic tasks with Django
SETTINGS.PY:
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
'add-every-30-seconds': {
'task': 'yourapp.tasks.teste',
'schedule': timedelta(seconds=5),
'args': (16, 16)
},
@psychok7
psychok7 / gist:5568291
Last active December 17, 2015 06:49
Python Post to REST API using Basic Authentication
import urllib2, json
def post():
def basic_authorization(user, password):
s = user + ":" + password
return "Basic " + s.encode("base64").rstrip()
data = json.dumps({'username':'david2', 'email':'david@gmail.com' , 'password':'mypassword'})
@psychok7
psychok7 / gist:5029496
Last active December 14, 2015 04:39
Download Zipfile and extract
import urllib2 ,argparse, shutil, urlparse , os , zipfile, os.path
from zipfile import ZipFile as zip
parser = argparse.ArgumentParser(description="Download and Unzip")
parser.add_argument('url', help='The action to take (e.g. install, remove, etc.)')
args = parser.parse_args()
url = args.url
def extractAll(zipName):
z = zip(zipName)