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
ffmpeg -itsoffset -4 -i "https://scontent.xx.fbcdn.net/v/t50.12441-16/61340994_435915640302058_n.mp4?_nc_cat=106&_nc_ht=scontent.xx&oh=19858915f178f4854a789438b3d9d24b&oe=5D5B3C92" -vcodec mjpeg -vframes 1 -an -f rawvideo test.jpg |
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
# Sorry for the silly example | |
def is_platypus(self): | |
if self.is_mammal(): | |
if self.has_fur(): | |
if self.has_beak(): | |
if self.has_tail(): | |
if self.can_swim(): | |
# It's a platypus! | |
return True | |
# Not a platypus |
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 is_platypus(self): | |
# Not a platypus for everything below | |
if not self.is_mammal(): | |
return False | |
if not self.has_fur(): | |
return False | |
if not self.has_beak(): | |
return False | |
if not self.has_tail(): | |
return False |
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 func_not_guarded(self, param): | |
if param == 'something': | |
self.counter += 1 | |
if self.counter > 10: | |
self.reached_ten() | |
else: | |
if self.counter < 5: | |
self.has_not_reached_5() | |
else: | |
self.has_not_reached_5() |
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 copy_queryset_without_annotations(original_qs): | |
# Create a new queryset based on the same model | |
optimized_qs = original_qs.model.objects.all() | |
# Copy the filters | |
optimized_qs.query.where = original_qs.query.where | |
optimized_qs.query.where_class = original_qs.query.where_class | |
return optimized_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
class PaginatorWithOptimizedCount(Paginator): | |
@cached_property | |
def count(self): | |
try: | |
# First usage of optimization function | |
return copy_queryset_without_annotations(self.object_list).count() | |
except (AttributeError, TypeError): | |
return len(self.object_list) | |
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
class Article(models.Model): | |
class Status(models.TextChoices): | |
DRAFT = '0', "Draft" | |
PUBLISHED = '1', "Published" | |
REMOVED = '2', "Removed" | |
status = models.CharField(choices=Status.choices, max_length=1) | |
class StatusFilter(admin.SimpleListFilter): |
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
class StatusFilter(admin.SimpleListFilter): | |
# Change your lookups function and include the class method | |
def lookups(self, request, model_admin): | |
return self._lookups() | |
@classmethod | |
def _lookups(cls): | |
# Static choices that will be used in the forms | |
return Article.Status.choices |
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
class BaseFilterForm(forms.Form): | |
def __init__(self, *args, **kwargs): | |
# Receive the filter instances from the view | |
filters = kwargs.pop('filters', []) | |
super(BaseFilterForm, self).__init__(*args, **kwargs) | |
for f in filters: | |
# Create dynamic form fields from the filter | |
self.fields[f.parameter_name] = \ | |
forms.ChoiceField(label=f.title, choices=[('', "Select")] + f._lookups(), required=False) | |
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
class BaseFilterView: | |
# List of admin filters | |
filters = [StatusFilter] | |
def get_form_kwargs(self): | |
form_kwargs = super().get_form_kwargs() | |
# Pass the filters to the form | |
form_kwargs['filters'] = self.filters | |
return form_kwargs |
OlderNewer