Skip to content

Instantly share code, notes, and snippets.

View thiagoferreiraw's full-sized avatar

Thiago Ferreira thiagoferreiraw

View GitHub Profile
@thiagoferreiraw
thiagoferreiraw / create_promotional_image_pure_python.py
Created November 25, 2021 19:34
create_promotional_image_pure_python.py
def create_promotional_image():
with open("promo.png", "wb+") as output_file:
create_png_from_svg_template(
"coupon/test.svg",
{"_WIDTH": 400, "_HEIGHT": 200, "_PROMO_CODE": "ABCD1234"},
output_file,
)
@thiagoferreiraw
thiagoferreiraw / tests.py
Created November 25, 2021 19:22
united tests
import cairosvg
@mock.patch("cairosvg.svg2png", wraps=cairosvg.svg2png)
def test_create_png_from_svg_template(self, spy_svg2png):
with BytesIO() as output_file:
image.create_png_from_svg_template(
"utils/tests/svg_template.svg",
{"_WIDTH": 159, "_HEIGHT": 161, "_TEXT": "Hello World!"},
output_file,
)
@thiagoferreiraw
thiagoferreiraw / create_promotional_image.py
Last active November 25, 2021 19:16
create_promotional_image
import io
from .models import Coupon
from .utils import create_png_from_svg_template
def create_promotional_image(coupon_id):
coupon = Coupon.objects.get(id=coupon_id)
with io.BytesIO() as output_file:
create_png_from_svg_template(
"coupon/templates/promotional-template.svg",
@thiagoferreiraw
thiagoferreiraw / create_png_from_svg_template.py
Last active November 25, 2021 19:12
create_png_from_svg_template
import cairosvg
def create_png_from_svg_template(svg_path, replace_keys_dict, output_file):
"""
Create a png from a svg template, replacing keys in the svg with custom values.
Args:
svg_path (str): Path to the svg file
replace_keys_dict (dict): Keys to search/replace in the svg, e. g. {"code": 1}
output_file (file-like obj or str): file-like object or path
@thiagoferreiraw
thiagoferreiraw / locust.py
Created November 4, 2021 12:27
Python Locust
import lorem
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 3)
@task(5)
def index(self):
self.client.get("/tasks/")
@thiagoferreiraw
thiagoferreiraw / git-diff-apply.sh
Created September 9, 2021 19:45
Git Diff + git apply
# Escolha sua branch de origem, de onde os commits serão revertidos
git checkout main
# Usando o git log, encontre o hash do commit do ponto que você deseja retornar para.
git checkout f21306c
# Agora vamos fazer uma comparação entre a posição atual do histórico e a main.
# Nesse ponto precisamos confirmar se as mudanças exibidas aqui estão corretas ;)
# Se não estiverem, você provavelmente está no ponto errado do histórico (veja o git log)
git diff main
@thiagoferreiraw
thiagoferreiraw / git-revert.sh
Created September 9, 2021 19:36
Giit Revert
# Escolha sua branch de origem, de onde os commits serão revertidos
git checkout main
# Use o commando git revert com a opção "-n" pra gerar um único commit
# Os ids abaixo podem ser encontrados usando o `git log`
git revert -n f21306c..e330c04 # OLD_COMMIT..RECENT_COMMIT
# Nesse ponto, você já pode usar os comandos de status e diff pra conferir se a alteração está correta:
git status # mostra os nomes dos arquivos alterados
git diff # mostra as mudanças efetivas
@thiagoferreiraw
thiagoferreiraw / 1-views.py
Last active September 1, 2021 13:23
Medium - Metabase + Django - 3
class EmbeddedReportResource(PaginatorMixin, APIResource):
preparer = EMBEDDED_REPORT_LIST_PREPARER
paginate = True
page_size = 40
@property
def base_query(self):
return (
EmbeddedReport.objects.filter(active=True)
.select_related("engine")
@thiagoferreiraw
thiagoferreiraw / models-2.py
Last active November 8, 2021 11:49
Medium - Metabase + Django - 2
class EmbeddedReport(BaseModel, models.Model):
...
...
...
def get_report_url_for_business(self, business)
map_resource = {
"dashboard": {
"params": {"dashboard": int(self.reference_id)},
"url_path": "dashboard",
},
@thiagoferreiraw
thiagoferreiraw / models-1.py
Last active September 1, 2021 13:21
Medium - Metabase + Django
class ReportEngine(BaseModel, models.Model):
id = HashedAutoField(primary_key=True)
name = models.CharField(max_length=250)
type = models.CharField(
choices=(("metabase", "Metabase"),), default="metabase", max_length=50
)
base_url = models.URLField()
integration_api_key = models.CharField(max_length=250)
class EmbeddedReport(BaseModel, models.Model):