Skip to content

Instantly share code, notes, and snippets.

// File: openzeppelin-solidity/contracts/token/ERC20/IERC20.sol
pragma solidity ^0.5.2;
/**
* @dev Interface of the ERC20 standard as defined in the EIP. Does not include
* the optional functions; to access them see `ERC20Detailed`.
*/
interface IERC20 {
@Ingco
Ingco / example_interfaces.py
Last active January 12, 2020 10:03
interface inheritance
#!/usr/bin/env python3.7
from abc import ABC, abstractmethod
from decimal import Decimal
from typing import NamedTuple, List, Tuple
# STUBS
def _(val):
@Ingco
Ingco / example.py
Created February 4, 2020 12:02
question
def make_readonly_field_for_instance(
parent_obj, obj, template_path: str = "", extra_context: dict = None
) -> callable:
"""
Dynamically creating new read-only fields for ModelAdmin classes
:param parent_obj: Models instance from admin class
:param obj: The object for which we are creating the field
:param template_path: Template for rendering
:param extra_context: dynamic context
:return: callable
order = (
Order.objects
.select_related("patient", "client")
.prefetch_related(
"orderstep_order__orderstepmisc_orderstep__misc",
"orderstep_order__orderstepjob_orderstep"
)
.annotate(orderstep_order__row_number=number_subquery)
.filter(pk=pk)
# models.py
class ProductionOrder(models.Model):
step = models.OneToOneField(
"order.OrderStep",
on_delete=models.DO_NOTHING,
verbose_name=_("Step"),
related_name="po_productionorder",
)
@Ingco
Ingco / code.py
Last active March 16, 2020 08:21
def get_jobs_queryset(self) -> QuerySet:
"""
Get QuerySet from production.Job (for subquery in ProductionOrderJob queryset)
:return: QuerySet
"""
return apps.get_model("production.Job").objects.filter(
(
Q(version_end_date__gt=OuterRef("production_order__created_at"))
| Q(version_end_date__isnull=True)
def when_conditions(when_year, when_month, then_val: Any = 1):
return When(
Q(
created_at__month=when_month,
created_at__year=when_year,
finish_at__isnull=True,
)
| Q(
finish_at__month=when_month,
finish_at__year=when_year,
@Ingco
Ingco / code.py
Last active March 20, 2020 07:27
# Запрос:
company_subquery = (
Order.objects_clear.annotate(
company_pk=F("client__company_id"),
company=Coalesce(
F("client__company__alias"), F("client__company__name")
),
)
)
company_name_subquery = company_subquery.values("company")[:1]
@Ingco
Ingco / code.py
Last active March 25, 2020 11:09
when__critical_status = When(
Q(orderstep_order__deadline__isnull=False)
& Q(orderstep_order__deadline__gte=timezone.now())
& Q(orderstep_order__deadline__lte=timezone.now() + timedelta(hours=4)),
then=Val("critical")
)
when__need_attention_status = When(
Q(orderstep_order__deadline__isnull=False)
& Q(orderstep_order__deadline__gte=timezone.now() + timedelta(hours=4))
# forms.py
class UserForm(forms.ModelForm):
"""A form for users. Includes password field."""
password = forms.CharField(
label="Password", widget=forms.PasswordInput(), required=False
)
email = MultiEmailField(
widget=MultiEmailWidget(), required=False, help_text=help_text_for_email
)