Skip to content

Instantly share code, notes, and snippets.

@mazlum
mazlum / validators.py
Last active December 30, 2015 20:16
File extension validation validator for Django framework.
from django.core.exceptions import ValidationError
import os
def validate_file_extension(value):
if not os.path.splitext(str(value))[1] in [".pdf"]:
raise ValidationError("Your file must be a pdf.")
@mazlum
mazlum / django_base64_file.py
Created April 24, 2016 12:56
Django Create File Model from base64 data
from django.core.files.base import ContentFile
#...
base64_data = ""
model_file = ContentFile(base64.b64decode(base64_data), "filename")
ModelObject.objects.create(file_field=model_file)
#...
def tc_sum(i, k):
return int(i)+int(k)
def tc_checksum(tc_no):
if len(tc_no) < 11 or int(tc_no[10]) % 2 != 0 or int(tc_no[0]) == 0:
return False
single = reduce(tc_sum, [val for i, val in enumerate(tc_no[:9]) if i%2==0]) * 7
double = reduce(tc_sum, [val for i, val in enumerate(tc_no[:8]) if i%2!=0])
if not (single - double) % 10 == int(tc_no[9]):
@mazlum
mazlum / nginx.conf
Created February 11, 2017 13:38 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
from functools import wraps
import json
from django.http.response import JsonResponse
from django.utils.decorators import available_attrs
def custom_decorator(view_func):
def wrapped_view(request, *args, **kwargs):
if request.method == "POST":
try:
@mazlum
mazlum / aa.es6_for_everyone.js
Last active April 3, 2018 08:50
ES6 for Everyone notes from Wesbos's tutorial. http://wesbos.com/es6-for-everyone/
console.log('Es6 for everyone tuturial from Wesbos');
.container {
display: flex;
background: url("headercover.jpg") bottom center no-repeat;
background-size:cover;
flex-direction: column;
justify-content: center;
padding: 40px;
align-items: center;
height: 50vh;
}
class ContentTypesConfig(AppConfig):
name = 'django.contrib.contenttypes'
verbose_name = _("Content Types")
def ready(self):
pre_migrate.connect(inject_rename_contenttypes_operations, sender=self)
post_migrate.connect(create_contenttypes)
checks.register(check_generic_foreign_keys, checks.Tags.models)
checks.register(check_model_name_lengths, checks.Tags.models)
# views.py
def product_list(request):
f = ProductFilter(request.GET, queryset=Product.objects.all())
return render(request, 'my_app/template.html', {'filter': f})
# urls.py
from django.conf.urls import url
from django_filters.views import FilterView
from myapp.models import Product
from sys import getsizeof
import json
import timeit
j = '''{
"first_name": "Mazlum",
"last_name": "Ağar"
}'''
# Memory