This file contains hidden or 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
""" | |
Sometimes in your Django model you want to raise a ``ValidationError`` in the ``save`` method, for | |
some reason. | |
This exception is not managed by Django Rest Framework because it occurs after its validation | |
process. So at the end, you'll have a 500. | |
Correcting this is as simple as overriding the exception handler, by converting the Django | |
``ValidationError`` to a DRF one. | |
""" | |
from django.core.exceptions import ValidationError as DjangoValidationError |
This file contains hidden or 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.contrib.gis.geos import GEOSGeometry | |
# sample data | |
geom_1 = GEOSGeometry('POLYGON((-71.8 42.1,-70.6 42.1,-70.5 41.2,-71.8 41.2,-71.8 42.1))') | |
geom_2 = GEOSGeometry('POLYGON((-71.12 42.23,-71.48 42.34,-71.52 42.55,-71.12 42.23))') | |
geom_3 = GEOSGeometry('POLYGON((-73.12 42.23,-71.48 42.34,-71.52 42.55,-73.12 42.23))') | |
polygons = [geom_1, geom_2, geom_3] | |
# get first polygon | |
polygon_union = polygons[0] |
This file contains hidden or 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
""" | |
Fix for some issues with the original code from Heroku: | |
https://devcenter.heroku.com/articles/s3-upload-python | |
This example is also designed for use with Django, not Flask as in the original. | |
""" | |
import base64 | |
import hashlib | |
import hmac |
This file contains hidden or 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
# Generate df manually. | |
df = pd.DataFrame({ | |
'A': ['apple', 'orange', 'pear', 'peach', 'black berry', 'grape', 'mandarine', 'watermelon', 'melon', 'pineapple', 'fig', 'banana', 'lemon', 'kiwi'], | |
'B': [21, 15, 3, 28, 9, 46, 28, 34, 47, 4, 38, 9, 5, 3], | |
}) |
This file contains hidden or 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
{ | |
"container_commands": { | |
"celery_configure": { | |
"command": "mv /tmp/100_celery_worker.sh /opt/elasticbeanstalk/hooks/appdeploy/post && chmod 774 /opt/elasticbeanstalk/hooks/appdeploy/post/100_celery_worker.sh", | |
"leader_only": true | |
} | |
} | |
} |
This file contains hidden or 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
files: | |
"/tmp/100_celery_worker.sh": | |
mode: "000755" | |
owner: root | |
group: root | |
content: | | |
#!/bin/sh | |
# for_log_and_pid | |
mkdir -p /var/log/celery/ /var/run/celery/ |
This file contains hidden or 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 Flask | |
from flask_restful import Api, Resource, reqparse | |
app = Flask(__name__) | |
api = Api(app) | |
transactions = [ | |
{ |
This file contains hidden or 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
""" | |
This should be a separate file, i usually install it on the maind django project folder. | |
""" | |
from threading import local | |
from django.utils.deprecation import MiddlewareMixin | |
_user = local() |
This file contains hidden or 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
""" | |
Django ORM Optimization Tips | |
Caveats: | |
* Only use optimizations that obfuscate the code if you need to. | |
* Not all of these tips are hard and fast rules. | |
* Use your judgement to determine what improvements are appropriate for your code. | |
""" | |
# --------------------------------------------------------------------------- |
This file contains hidden or 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
def add_q_object(self, keyword, q_object, query_parm): | |
for key in keyword: | |
kwargs = {query_parm: key.strip()} | |
q_object.add(Q(**kwargs), q_object.OR) | |
def filter_applicants(self, _queryset=None): | |
my_applicants = _queryset |