This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_picture_path_is_correct(self): | |
obj = self.create_obj() | |
abs_path = os.path.join(settings.TEST_MEDIA_ASSETS_FOLDER, 'profile_picture_1280.png') | |
obj.picture.save('filename.png', ImageFile(open(abs_path, 'rb'))) # this line reads image and store it as binary | |
obj.save() | |
self.assertTrue(os.path.isfile(obj.picture.path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import typing | |
from abc import ABC | |
_th_base = typing.TypeVar('_th_base', bound='BaseKlass') | |
class BaseKlass(ABC): | |
def __init__(self, selected_topic): | |
self.selected_topics = selected_topics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test import TestCase | |
from path.to.abstract.model import SomeAbstractModel | |
class SampleModel(SomeAbstractModel): | |
class Meta: | |
# app_label is needed in cases where tests are in folder of project to be able to find this model | |
app_label = 'app.name.of.where.abstract.model.is.defined' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.forms import BaseModelFormSet | |
from app.companies.models import CompanyBranchPaymentOptions | |
class PaymentsBaseFormSet(BaseModelFormSet): | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.__full_id_queryset = CompanyBranchPaymentOptions.objects.get_full_queryset() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
value_list = [ | |
{ | |
'name': 'a', | |
'value': '1', | |
}, | |
{ | |
'name': 'a', | |
'value': '2', | |
}, | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bug described here - https://code.djangoproject.com/ticket/33813 | |
---- | |
# Definitions | |
# models | |
class ReviewsModelManager(models.Manager): | |
def get_queryset(self): | |
qs = super().get_queryset() | |
qs = qs.filter(published=True) | |
return qs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import time | |
logger = logging.getLogger('MyLogger') | |
def measure_run_time(func): | |
def wrapper(*args, **kwargs): | |
total_start_time = time.time_ns() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decorator(func): | |
"""This is a decorator function. It takes some function as a parameter, 'wraps it' in code | |
and RETURNs wrapped function | |
This is a way to add functionality without changing base code""" | |
def wrap(text='this is base function'): | |
print('this is pre-function') | |
func(text) | |
print('this is post func') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""getters, setters and property decorators examples | |
Used to get and set protected variables(starts with _) | |
using property makes all code call getter and setter functions when rear and write do this class protected variable""" | |
class AddTesterToString: | |
def __init__(self, input_string): | |
self.input_string = input_string | |
# def getter function - gets _protected variable |
NewerOlder