Skip to content

Instantly share code, notes, and snippets.

View dz0's full-sized avatar

Jurgis Pralgauskis dz0

  • 11:13 (UTC +02:00)
View GitHub Profile
@dz0
dz0 / GroupPermissionObjects.py
Last active June 25, 2019 14:06
constructed by `GroupObjectsPermission` as example
########### choose multiple objects (Bookable) for selected group+permission
from django.contrib.auth.models import Permission
## guardian/forms.py
from django import forms
from django.utils.translation import ugettext as _
from guardian.shortcuts import assign_perm, remove_perm, get_objects_for_group
class BasePermissionObjectsForm(forms.Form):
@dz0
dz0 / DRF validate on serialization read_only.py
Last active December 1, 2018 21:35
DRF validate None on BooleanField serialization
from rest_framework.serializers import Serializer, BooleanField
# BooleanField.FALSE_VALUES.add(None) # does not help
class S(Serializer):
bf = BooleanField(required=False, read_only=True)
class example:
bf = None # I expect it to become **False**
@dz0
dz0 / drf_optimize.py
Created November 29, 2018 09:23 — forked from jackton1/drf_optimize.py
MetaClass to Improve DRF ModelViewsSet query.
from django.db import ProgrammingError, models
from django.db.models.constants import LOOKUP_SEP
from django.db.models.query import normalize_prefetch_lookups
from rest_framework import serializers
from rest_framework.utils import model_meta
class OptimizeRelatedModelViewSetMetaclass(type):
"""
This metaclass optimizes the queryset using `prefetch_related` and `select_related`.
@dz0
dz0 / DRF Serializer validation stack.md
Last active March 14, 2019 11:03
DRF Serializer validation stack.md

Serializer:

  • is_valid(self, raise_exception=False) # usually called before self.save(..)
    • run_validation(self, data=empty)
      • validate_empty_values(self, data)
      • to_internal_value(self, data)
        • each DRF Field:
          • run_validation(self, data=empty)
            • validate_empty_values(self, data)
            • to_internal_value(self, data)
  • run_validators(self, value)
@dz0
dz0 / code_quotes.txt
Created April 26, 2018 02:33 — forked from OnesimusUnbound/quote.txt
Programming Quotes
[T]he difference between a bad programmer and a
good one is whether he considers his code or his
data structures more important. Bad programmers
worry about the code. Good programmers worry about
data structures and their relationships.
-- Linus Torvalds
~~~
Clarity and brevity sometimes are at odds.
When they are, I choose clarity.
-- Jacob Kaplan-Moss
/*
* Game of Life v1.0
* https://en.wikipedia.org/wiki/Conway's_Game_of_Life
*
* Author: Chionsas
* Licence: PUBLIC DOMAIN, https://unlicense.org/UNLICENSE
*/
/*
* Užduotis: pabaigti funkcijas getAliveNeighbourCount ir (panaudojant ją) generateNextTick (pagal išvardintas taisykles).
final int cols = 3;
final int rows = 3;
//int[][] M = new int[cols][rows];
/*
{
{0, 0, 0 } ,
{0, 0, 0 } ,
{0, 0, 0 }
}
*/
a, b = None, None
def __init__(a_, b_):
global a, b
a = a_
b = b_
def add():
return a+b
int X[ ]={0, -40, 35, 10, -35, 90};
int Y[ ]={0, -50, 30, -20, 26, -10};
int n=6;
int[] eile = new int[n]; // lankymo masyvas/eilė -- pradžioj tuščia
int eil_n = 0;
// boolean[] ar_lankytas = new boolean[n]; // gali praversti patogesniam pažymėjimui, ar punktas jau peržiūrėtas
int kur_pele=-1;
void draw(){
/*
// reik surasti ir pakeisti buvusį užpildo nustatymo kodą:
int alpha = 255 - ligos_poveikis_alphai(i); // 255 yra pilnai spalvotas
if (i==0) fill(250, 250, 0, alpha);
else fill(100, alpha); // uzpildas
// prie info:
text(alpha, X[i], Y[i]+15);