Skip to content

Instantly share code, notes, and snippets.

View LowerDeez's full-sized avatar
🇺🇦
Focusing

Oleg Kleshchunov LowerDeez

🇺🇦
Focusing
  • Kyiv, Ukraine
  • 23:40 (UTC +03:00)
View GitHub Profile
@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 / 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 / base.babel.js
Created September 28, 2017 09:19
Django. Web-case template. Gulp-task example to minify images
import gulp from 'gulp'
import * as stylus from './tasks/stylus'
import * as templates from './tasks/templates'
import * as pub from './tasks/public'
import * as images from './tasks/images'
gulp.task('images', images.dev)
gulp.task('public', pub.dev)
gulp.task('stylus', stylus.dev)
@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 / csrf.ajax.js
Created October 19, 2017 21:39
csrf.ajax.js
$(document).ready(function(){
// using jQuery
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie !== '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
@LowerDeez
LowerDeez / admin.py
Created October 23, 2017 11:09
Django. Likes app
from django.contrib import admin
from .models import Like
admin.site.register(Like)
@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 / 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>