Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 07:24 (UTC +02:00)
View GitHub Profile
@LowerDeez
LowerDeez / base.html
Last active September 10, 2024 13:52
Django. Simple Cart app for basic shop.
<div class="cart">
{% with total_items=cart|length %}
{% if cart|length > 0 %}
Ваша корзина:
<a href="{% url "cart:CartDetail" %}">
{{ total_items }} тов. {{ cart.get_total_price }} руб.
</a>
{% else %}
Корзина пустая
{% endif %}
@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 / forms.py
Last active October 8, 2017 16:41
Django. Extend PasswordResetForm to check for email in database.
from django.contrib.auth.forms import PasswordResetForm
class MailCheckPasswordResetForm(PasswordResetForm):
email = forms.EmailField(max_length=254)
error_messages = {
'unknown': _("That email address doesn't have an associated "
"user account. Are you sure you've registered?"),
'unusable': _("The user account associated with this email "
@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 / app.py
Last active September 20, 2017 12:05
Python string (password) generator (few examples)
# 1st
import string
import random
def pw_gen(size = 8, chars=string.ascii_letters + string.digits + string.punctuation):
return ''.join(random.choice(chars) for _ in range(size))
print(pw_gen(int(input('How many characters in your password?'))))
#2nd
@LowerDeez
LowerDeez / home.html
Created May 28, 2017 21:29
Django Pagination (with search query)
{% for article in articles %}
{% include 'articles/partial/partial_article.html' %}
{% empty %}
<h1>Nothing to show</h1>
{% endfor %}
<!-- Pagination -->
{% if articles.has_other_pages %}
<div class="text-center">
<ul class="pagination pagination-lg text-center">
@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 / 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 / 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: