This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class PersonManager(models.Manager): | |
def get_by_natural_key(self, first_name, last_name): | |
return self.get(first_name=first_name, last_name=last_name) | |
class Person(models.Model): | |
first_name = models.CharField(max_length=100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tempfile | |
from django.contrib import admin | |
from django.core import serializers | |
from django.http import HttpResponse | |
from example_app.models import * | |
@admin.action(description="Export Model") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
def print_cube(num): | |
""" | |
function to print cube of given num | |
""" | |
print("Cube: {}".format(num * num * num)) | |
def print_square(num): | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from multiprocessing import Process | |
def print_func(continent='Asia'): | |
print('The name of continent is : ', continent) | |
if __name__ == "__main__": # confirms that the code is under main function | |
names = ['America', 'Europe', 'Africa'] | |
procs = [] | |
proc = Process(target=print_func) # instantiating without any argument |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from flask import ( | |
Flask, | |
request, | |
render_template, | |
jsonify, | |
url_for | |
) | |
from make_celery import make_celery | |
import time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserSerializer(serializers.ModelSerializer): | |
def validate_password(self, value): | |
if value.isalnum(): | |
raise serializers.ValidationError('password must have atleast one special character.') | |
return value | |
def validate(self, data): | |
if data['first_name'] == data['last_name']: | |
raise serializers.ValidationError("first_name and last_name shouldn't be same.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Custom django checks. | |
H001: Field has no verbose name. | |
H002: Verbose name should use gettext. | |
H003: Words in verbose name must be all upper case or all lower case. | |
H004: Help text should use gettext. | |
H005: Model must define class Meta. | |
H006: Model has no verbose name. | |
H007: Model has no verbose name plural. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://code.djangoproject.com/ticket/17209 | |
import urlparse | |
from django.conf import settings | |
from django.core.urlresolvers import reverse_lazy | |
from django.http import HttpResponseRedirect, QueryDict | |
from django.utils.decorators import method_decorator | |
from django.utils.http import base36_to_int | |
from django.utils.translation import ugettext as _ | |
from django.views import generic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import make_response | |
from reportlab.pdfgen import canvas | |
# ... | |
@app.route('/pdf') | |
def pdf(): | |
import cStringIO | |
output = cStringIO.StringIO() |