Skip to content

Instantly share code, notes, and snippets.

View DJWOMS's full-sized avatar

Омельченко Михаил | Omelchenko Mikhail DJWOMS

View GitHub Profile
class AddCartItem(View):
"""Добавление товара в карзину"""
def check_cart(self, request):
if request.user.is_authenticated:
cart = Cart.objects.get(user=request.user, accepted=False)
else:
session_cart = request.session.get('cart')
if not session_cart:
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
@DJWOMS
DJWOMS / SortProduct
Created January 26, 2020 10:59
SortProduct
class CategoryProductVue(View):
"""Список товаров из категории для vue"""
def get(self, request):
return render(request, "shop/vue/list-product-vue.html")
def post(self, request):
slug = request.POST.get("slug")
node = Category.objects.get(slug=slug)
if Product.objects.filter(category__slug=slug).exists():
products = Product.objects.filter(category__slug=slug)
@DJWOMS
DJWOMS / context_processors.py
Created January 30, 2020 07:13
context_processors для вывода категорий
"""
Данный файл нужно добавить в ваше приложение магазина
"""
from .models import Category
def list_categoryes(request):
"""Список категорий в меню"""
return {"menu_categoryes": Category.objects.all()}
Создание пользователя
---------------------
adduser username
usermod -aG sudo username
group username
su username
---------------------------------------
Компиляции python 3.6
----------------------
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev
class User(UserBase):
id: int
is_active: bool
class Config:
orm_mode = True
async def create_user(user: schemas.UserCreate):
fake_hashed_password = user.password + "notreallyhashed"
Авторизация\Регистрация
https://github.com/jazzband/django-oauth-toolkit
https://github.com/pennersr/django-allauth
https://github.com/sunscrapers/djoser
https://github.com/django-guardian/django-guardian
https://github.com/flavors/django-graphql-jwt
Защита входа
https://github.com/jazzband/django-axes
https://github.com/jazzband/django-defender
class Comment(object):
def __init__(self, email, content, created=None):
self.email = email
self.content = content
self.created = created or datetime.datetime.now()
comment = Comment(email='[email protected]', content='My content')
from rest_framework import serializers
class CommentSerializer(serializers.Serializer):
email = serializers.EmailField()
content = serializers.CharField(max_length=200)
created = serializers.DateTimeField()
def restore_object(self, attrs, instance=None):
"""
Учитывая словарь десериализованных значений поля, либо обновить существующий экземпляр модели или
serializer = CommentSerializer(comment)
print(serializer.data)
# {'email': u'[email protected]', 'content': u'foo bar', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774)}
from rest_framework.renderers import JSONRenderer
json = JSONRenderer().render(serializer.data)
print(json)
# '{"email": "[email protected]", "content": "foo bar", "created": "2012-08-22T16:20:09.822"}'