Skip to content

Instantly share code, notes, and snippets.

View douglasmiranda's full-sized avatar
👽

Douglas Miranda douglasmiranda

👽
  • Earth, Brazil
View GitHub Profile
@douglasmiranda
douglasmiranda / forms.py
Created September 10, 2024 02:29
Django BaseInlineFormset custom attributes to DELETE button
class ScheduleInlineFormSet(BaseInlineFormSet):
def add_fields(self, form, index):
super().add_fields(form, index)
# customizing for bootstrap needs
if "DELETE" in form.fields:
form.fields["DELETE"] = forms.BooleanField(
label="Excluir",
widget=forms.CheckboxInput(attrs={"class": "btn-check"}),
required=False,
)
@douglasmiranda
douglasmiranda / forms.py
Created September 10, 2024 02:26
Django forms.MultiWidget dict example
# this dict can be accessed in get_context()
{
"widget": {
"name": "publishing_dates",
"is_hidden": False,
"required": False,
"value": "[datetime.date(2024, 9, 5), datetime.date(2024, 9, 6), datetime.date(2024, 9, 7), datetime.date(2024, 9, 8)]",
"attrs": {
"class": "form-control",
"placeholder": "Data de publicação",
@douglasmiranda
douglasmiranda / nolabelsuffix.py
Created August 29, 2024 00:06 — forked from jleeothon/nolabelsuffix.py
Change label suffix for Django forms
# have all your forms extend this mixin
class NoLabelSuffixMixin:
def __init__(self, *args, **kwargs):
if 'label_suffix' not in kwargs:
kwargs['label_suffix'] = ''
super(NoLabelSuffixMixin, self).__init__(*args, **kwargs)
@douglasmiranda
douglasmiranda / extract_date_from_string.py
Created August 21, 2024 02:25
python - extract date from string (and return the format of matching)
from dataclasses import dataclass
from datetime import datetime
# This is basically a draft
# it works, it's just very simplistic
# looks for a single occurrence; match; return
# The main goal it's just parse the date and have control of
# the format I matched.
# Example: read autocomplete field and trigger a search when
# the search term match at least the dd/mm (%d/%m) format.
@douglasmiranda
douglasmiranda / warp.md
Last active August 18, 2024 03:23
Cloudflare Warp useful links
@douglasmiranda
douglasmiranda / templatetags.py
Created August 16, 2024 01:05 — forked from benbacardi/templatetags.py
Django query_transform templatetag
from django import template
register = template.Library()
@register.simple_tag(takes_context=True)
def query_transform(context, **kwargs):
'''
Returns the URL-encoded querystring for the current page,
updating the params with the key/value pairs passed to the tag.
@douglasmiranda
douglasmiranda / README.md
Created August 15, 2024 01:26 — forked from shrmnk/README.md
Setting up Cloudflared DoH client on Ubuntu Server with resolved
  1. Install cloudflared
  2. Create /etc/systemd.system/cloudflared-proxy-dns.service with contents:
[Unit]
Description=DNS over HTTPS (DoH) proxy client
Wants=network-online.target nss-lookup.target
Before=nss-lookup.target

[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE
@douglasmiranda
douglasmiranda / README.md
Last active August 5, 2024 01:50
Django - Postgres - Accent-insensitive queries in Portuguese language for full-text search.
@douglasmiranda
douglasmiranda / menu.html
Created August 5, 2024 00:59
Django (Bootstrap) Active Menu
Sure, you could make a Template Tag. But if it's just a single main menu in the entire website.. why not just:
(Not really limited to bootstrap, just an example)
<ul class="navbar-nav">
{% with request.resolver_match.url_name as url_name %}
<li class="nav-item">
<a class="nav-link{% if url_name == "home" %} active{% endif %}" href="{% url "app:home" %}">Home</a>
</li>
<li class="nav-item">
<a class="nav-link{% if url_name == "news" %} active{% endif %}" href="{% url "app:news" %}">News</a>
@douglasmiranda
douglasmiranda / fields.py
Created July 9, 2024 01:53
Django (postgres) ArrayField Multiple Choices as checkboxes.
# https://gist.github.com/danni/f55c4ce19598b2b345ef
from django import forms
from django.contrib.postgres.fields import ArrayField
class MultipleChoiceArrayField(ArrayField):
def formfield(self, **kwargs):
defaults = {
"form_class": forms.MultipleChoiceField,
"choices": self.base_field.choices,