Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 10:46 (UTC +03:00)
View GitHub Profile
@LowerDeez
LowerDeez / project_name->middleware.py
Created April 30, 2017 09:46
Middleware заменяющий @login_required
# промежуточный класс для перенправления незарегистрированного пользователя на страницу входа
import re
from django.conf import settings
from django.shortcuts import redirect
from django.contrib.auth import logout
from django.urls import reverse
EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
@LowerDeez
LowerDeez / common.js
Created May 14, 2017 14:50
JavaScript Плавный скролл
$(function() {
// Smooth Scrolling
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
@LowerDeez
LowerDeez / common.js
Last active June 10, 2017 22:31
JavaScripts (Animate CSS + WayPoints javaScript Plugin) Добавление анимации секциям при скролле
function m(value) {
//$r = $(".s-how-to-order").animated('fadeInUp', 'fadeOutDown');
var inEffect = "fadeInUp"
var outEffect = "fadeOutDown"
var $r = $(value);
$r.css("opacity", "0").addClass("animated").waypoint(function (dir) {
if (dir == 'down') {
$r.removeClass(outEffect).addClass(inEffect).css("opacity", "1");
} else {
$r.removeClass(inEffect).addClass(outEffect).css("opacity", "1");
@LowerDeez
LowerDeez / example.py
Last active June 16, 2017 19:31
Creating a model mixin to handle generic relations
# The following is an example of how to use two generic relationships in your app (put
# this code in demo_app/models.py), as shown in the following:
# demo_app/models.py
# -*- coding: UTF-8 -*-
from __future__ import nicode_literals
from django.db import models
from utils.models import object_relation_mixin_factory
from django.utils.encoding import python_2_unicode_compatible
FavoriteObjectMixin = object_relation_mixin_factory( is_required=True,)
OwnerMixin = object_relation_mixin_factory(
@LowerDeez
LowerDeez / models.py
Last active June 16, 2017 20:06
Creating a model mixin to take care of meta tags
# utils/models.py
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.template.defaultfilters import escape
from django.utils.safestring import mark_safe
class MetaTagsMixin(models.Model):
"""
@LowerDeez
LowerDeez / utils.py
Created July 14, 2017 14:45
Client IP and City
from django.conf import settings
from django.contrib.gis.geoip2 import GeoIP2
GEO_DEFAULT_IP = getattr(settings, 'GEO_DEFAULT_IP', '72.14.207.99')
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for is not None:
ip = x_forwarded_for.split(',')[0]
else:
@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 / 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 / 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,