Skip to content

Instantly share code, notes, and snippets.

View rg3915's full-sized avatar
🏠
Working from home

Regis Santos rg3915

🏠
Working from home
View GitHub Profile
@rg3915
rg3915 / drf_utils.py
Created March 20, 2021 23:00 — forked from twidi/drf_utils.py
Make Django Rest Framework correctly handle Django ValidationError raised in the save method of a model
"""
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
@rg3915
rg3915 / unions
Created March 6, 2021 12:52 — forked from djq/unions
How to union a list of geometries in GeoDjango
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]
@rg3915
rg3915 / upload.py
Created May 26, 2020 01:22 — forked from RyanBalfanz/upload.py
[Heroku] Direct to S3 File Uploads in Python
"""
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
@rg3915
rg3915 / df_manual.py
Last active January 22, 2020 16:39 — forked from brianckeegan/diverse_df.py
Generates a random pandas DataFrame containing categories, timestamps, integers, and random floats. Useful for debugging or answering StackOverflow questions.
# 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],
})
@rg3915
rg3915 / call_celery_post_deploy_aws.config
Created August 27, 2019 15:05 — forked from bahiamartins/call_celery_post_deploy_aws.config
call celery in AWS Elastic Beanstalk via post deploy. Process of Daemonization using Supervisord
{
"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
}
}
}
@rg3915
rg3915 / celery_daemon.config
Created August 27, 2019 15:04 — forked from bahiamartins/celery_daemon.config
Daemonization process of Celery and Celery Beat using Supervisord in AWS Elastic Beanstalk
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/
@rg3915
rg3915 / api-restful.py
Created July 2, 2019 20:22
Flask api Rest
from flask import Flask
from flask_restful import Api, Resource, reqparse
app = Flask(__name__)
api = Api(app)
transactions = [
{
@rg3915
rg3915 / example_current_user.py
Last active April 29, 2025 17:37 — forked from vinidmpereira/example_current_user.py
get_current_user middleware. current user
"""
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()
@rg3915
rg3915 / django_orm_optimization_cheat_sheet.py
Last active August 26, 2023 15:51 — forked from levidyrek/django_orm_optimization_cheat_sheet.py
Django ORM optimization cheat sheet
"""
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.
"""
# ---------------------------------------------------------------------------
@rg3915
rg3915 / filter_applicants.py
Created September 22, 2018 00:11 — forked from olivx/filter_applicants.py
filter_applicants multiples elements separator per ', ' example with Q() object
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