Skip to content

Instantly share code, notes, and snippets.

View ao5357's full-sized avatar
🙂
Probably near my computer, probably something with emails

Brad Czerniak ao5357

🙂
Probably near my computer, probably something with emails
View GitHub Profile
@ao5357
ao5357 / NyplIpCheckerAdminForm.php
Created May 4, 2021 11:53
Stash a complex AJAX form
<?php
namespace Drupal\nypl_ip_checker\Form;
/**
* @file
* Contains \Drupal\contact_info\Form\NyplIpCheckerAdminForm.
*/
use Drupal\Core\Config\ConfigFactoryInterface;
@ao5357
ao5357 / a11y--skip-hide.css
Created April 27, 2021 13:31
Skip navigation link for assistive technology and keyboard nav
.a11y--skip-hide {
background: #444;
color: #fff;
position: relative;
text-align: center;
}
.a11y--skip-hide a {
color: #fff;
display: block;
height: 1px;
@ao5357
ao5357 / AttributeCriteria.py
Created April 11, 2021 20:02
Python dataclass allowing dict initialization
@dataclass
class AttributeCriteria:
attribute_type: LocationAttributeType
display_value: str
operators: List[CriteriaOperator]
choices: List[AttributeCriteriaChoice]
def __post_init__(self):
for idx, operator in enumerate(self.operators):
if isinstance(operator, str):
@ao5357
ao5357 / init_dict.py
Created April 11, 2021 19:57
Python dataclasses do not support nested dataclass instantiation out of the box
init_dict = dict(
attribute_key=REGION_KEY,
display_value="Regions",
operators=[EQ_OPERATOR, IN_OPERATOR],
choices=[dict(value=42, display_value="West Coast"), ...]
)
attribute_criteria_example = AttributeCriteria(**init_dict)
# attribute_criteria.choices[0] is a dict not a AttributeCriteriaChoice as we would hope!
# This is true for a dataclass as well as this example of a list of dataclasses
@ao5357
ao5357 / ExampleModel.py
Created April 11, 2021 19:55
Python usage with a Django SQL backed model:
class ExampleModel(models.Model):
...
json_attributes = DataclassJSONField(
default=None, blank=True, null=True, dataclass_cls=AttributeCriteriaFilters
)
model = ExampleModel.objects.first()
assert dataclasses.is_dataclass(model.json_attributes)) is True
@ao5357
ao5357 / DataclassJSON.py
Created April 11, 2021 19:54
Python prevent overriding serialization
class DataclassJSONEncoder(JSONEncoder):
""" JSON encoder for JSON mysql fields to handle encoding dataclasses and enum classes """
def default(self, o):
if is_dataclass(o):
return dataclasses.asdict(o)
elif isinstance(o, Enum):
return o.value
return super().default(o)
class DataclassJSONField(JSONField):
"""
@ao5357
ao5357 / AttributeCriteria.py
Created April 11, 2021 19:45
Python explicit structured dataclasses
@dataclass
class AttributeCriteriaChoice:
value: Type[Any]
display_value: str
@dataclass
class AttributeCriteria:
attribute_key: str
display_value: str
operators: List[str]
@ao5357
ao5357 / atribute_criteria.py
Created April 11, 2021 19:44
Python native dictionary implementation
attribute_criteria = dict(
attribute_key=REGION_KEY,
display_value="Regions",
operators=[EQ_OPERATOR, IN_OPERATOR],
choices=[dict(value=42, display_value="West Coast"), ...]
)
@ao5357
ao5357 / .lando.yml
Created April 6, 2021 19:08
Build assets for local petrol
name: petrol
recipe: drupal7
config:
php: '5.6'
via: nginx:1.17
webroot: .
database: mysql:5.7
drush: '^7'
xdebug: true
@ao5357
ao5357 / view--migrate_subscription_databases.php
Last active September 7, 2021 17:15
Migrate D8 XML Views Data Export
$view = new view();
$view->name = 'd8_migrations';
$view->description = 'Views Data Exports for migrating to Drupal 8';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'D8 Migrate';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */