Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 04:31 (UTC +03:00)
View GitHub Profile
@LowerDeez
LowerDeez / list.jinja
Last active May 7, 2019 17:03
Django. Sorting queryset with ajax
<!--Article sorting for Ajax-->
<select title="{{ _('Select sort param') }}" class="selectpicker" id="sort"
data-url="{{ url('articles:sort') }}">
<option value="-create_date" data-icon="glyphicon glyphicon-arrow-up">
{{ _('Date') }}
</option>
<option value="create_date" data-icon="glyphicon glyphicon-arrow-down">
{{ _('Date') }}
</option>
<option value="-rate" data-icon="glyphicon glyphicon-arrow-up">
@LowerDeez
LowerDeez / main.js
Last active October 24, 2017 08:00
Django. Simple Follower System
// Include before main script file and after jquery (or after jquery if everything in one file)
$(document).ready(function(){
...
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Cross Site Request Forgery protection~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
@LowerDeez
LowerDeez / task.py
Created August 8, 2017 17:00
Django. Periodic task in Celery + RebbitMQ
from celery.schedules import crontab
from services import crawlers
from celery.task.base import periodic_task
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
@periodic_task(run_every=(crontab(hour="*", minute="*", day_of_week="*")))
def update_githab():
# logger.info("Start task")
@LowerDeez
LowerDeez / create.html
Last active October 24, 2017 08:07
Django. UpdateView and CreateView with inline formsets (and with django-extra-views)
{% extends 'base.html' %}
{% load static %}
{% load widget_tweaks %}
{% load i18n %}
{% block content %}
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h2>
{% if action == 'update' %}
@LowerDeez
LowerDeez / models.py
Last active October 24, 2017 08:09
Django. GEnericRelation. Removing objects of Generic Relation model if content_object has been removed
# Article - your model
# Action, Like - Generic Relation models
# Deleting action and likes, associated with article, if we deleting article
def deleted_gfk_Action(sender, instance, using, **kwargs):
ctype = ContentType.objects.get_for_model(sender)
try:
action_objs = Action.objects.filter(target_ct=ctype, target_id=instance.id)
like_objs = Like.objects.filter(content_type=ctype, object_id=instance.id)
except Action.DoesNotExist:
pass
@LowerDeez
LowerDeez / second-template.jinja.html
Last active October 24, 2017 08:26
Django-mptt. Getting recursive tree objects for jinja2 templates to avoid n+1 queries for children nodes when using translations fields in model(parler)
<!--Another jinja template for variant with cache categories-->
<ul class="categories">
<li>
<form id="all_categories_form" action="{{ url('articles:home') }}" method="get">
<input type="hidden" name="is_clear" value="True">
<a href="#" onclick="document.getElementById('all_categories_form').submit();">All</a>
</form>
</li>
@LowerDeez
LowerDeez / partial_form_fields.jinja.html
Created August 29, 2017 12:57
Django-Jinja2. Render form in template
{% for hidden_field in form.hidden_fields() %}
{{ hidden_field }}
{% endfor %}
{% if form.non_field_errors %}
{% for error in form.non_field_errors() %}
{{ error }}
{% endfor %}
{% endif %}
@LowerDeez
LowerDeez / template.html
Last active September 4, 2017 19:19
Django. Django-mptt. Displaying checkboxes for categories, which have articles
<!-- With bootstrap accordeon for category's children -->
<ul class="categories">
<li>
<form id="all_categories_form" action="{{ url('articles:home') }}" method="get">
<input type="hidden" name="is_clear" value="True">
<a href="#" onclick="document.getElementById('all_categories_form').submit();">All</a>
</form>
</li>
@LowerDeez
LowerDeez / views.py
Created September 4, 2017 19:09
Django. How to process more than one form at once in CVB
class EditProfileView(LoginRequiredMixin, SuccessMessageMixin, FormView):
model = User
template_name = 'accounts/settings/settings.html'
success_message = _('Your profile was successfully updated!')
form_class = UserForm
second_form_class = ProfileForm
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
if 'user_form' not in context:
@LowerDeez
LowerDeez / forms.py
Created September 8, 2017 09:35
Django. How to use Django Extra Views and django-parler for Create and Update View with inline_formset
class ArticleForm(TranslatableModelForm):
title = TranslatedField()
short_description = TranslatedField(
label=_('Short description'),
required=False,
help_text=_("You can use html tags for short description. Max length: 1000 characters."))
content = TranslatedField(
label=_('Content'),
form_class=forms.CharField,
widget=TinyMCE,