<repo_name>avdeyArtem/my-first-shop<file_sep>/catalog/migrations/0001_initial.py
# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-06-06 15:34
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='CategoriesFirst',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, verbose_name='Название раздела')),
                ('slug', models.CharField(max_length=50, verbose_name='Ссылка')),
            ],
            options={
                'verbose_name_plural': 'Раздел',
            },
        ),
        migrations.CreateModel(
            name='CategoriesSecond',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, verbose_name='Название категирии')),
                ('slug', models.CharField(max_length=50, verbose_name='Ссылка')),
            ],
            options={
                'verbose_name_plural': 'Категория',
            },
        ),
        migrations.CreateModel(
            name='Product',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, verbose_name='Название товара')),
                ('description', models.TextField(verbose_name='Описание')),
                ('size', models.CharField(max_length=50, verbose_name='Размер')),
                ('compound', models.CharField(max_length=100, verbose_name='Состав')),
                ('price', models.DecimalField(decimal_places=2, max_digits=6, verbose_name='Цена')),
                ('count', models.IntegerField(default=0, verbose_name='Количество')),
                ('land', models.CharField(max_length=50, verbose_name='Страна производитель')),
                ('image', models.ImageField(upload_to='img', verbose_name='Изображение товара')),
            ],
            options={
                'verbose_name_plural': 'Продукты',
            },
        ),
    ]
<file_sep>/user/views.py
from django.shortcuts import render_to_response, render
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.views.decorators import csrf
from .forms import RegistrationForm
from django.contrib.auth import authenticate, login as auth_login, logout
# Create your views here.
def register(request):
    context = {}
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            username = request.POST['username']
            password = request.POST['<PASSWORD>']
            user = authenticate(request, username=username, password=password)
            if user.is_active:
                auth_login(request, user)
                return HttpResponseRedirect('/')
            return HttpResponseRedirect('/')

    else:
        form = RegistrationForm()
        context = {'form':form}

    return render(request, "catalog/reg.html", context)

def login(request):
    context = {}
    username = password = ''
    if request.method == 'POST':
        username = request.POST['login']
        password = request.POST['<PASSWORD>']
        user = authenticate(request, username=username, password=<PASSWORD>)
        if user is not None:
            if user.is_active:
                auth_login(request, user)
                return HttpResponseRedirect('/')
    return render(request, "catalog/reg.html", context)

def logout_user(request):
    logout(request)
    return HttpResponseRedirect('/')
<file_sep>/catalog/migrations/0004_auto_20170626_2201.py
# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-06-26 19:01
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('catalog', '0003_product_category'),
    ]

    operations = [
        migrations.CreateModel(
            name='Brand',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, verbose_name='Название категории')),
                ('slug', models.CharField(max_length=50, verbose_name='Ссылка')),
            ],
            options={
                'verbose_name_plural': 'Брэнды',
            },
        ),
        migrations.AlterField(
            model_name='categoriessecond',
            name='name',
            field=models.CharField(max_length=50, verbose_name='Название категории'),
        ),
        migrations.AddField(
            model_name='product',
            name='brand',
            field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='catalog.Brand'),
            preserve_default=False,
        ),
    ]
<file_sep>/catalog/models.py
from django.db import models
class CategoriesFirst(models.Model):
    name = models.CharField(max_length = 50, verbose_name = "Название раздела")
    slug = models.CharField(max_length = 50, verbose_name = "Ссылка")
    def __str__ (self):
        return self.name
    class Meta:
        verbose_name_plural = "Раздел"

class CategoriesSecond(models.Model):
    name = models.CharField(max_length = 50, verbose_name = "Название категории")
    slug = models.CharField(max_length = 50, verbose_name = "Ссылка")
    section = models.ForeignKey(CategoriesFirst)
    def __str__ (self):
        return self.name
    class Meta:
        verbose_name_plural = "Категория"

class Brand(models.Model):
    name = models.CharField(max_length = 50, verbose_name = "Название категории")
    slug = models.CharField(max_length = 50, verbose_name = "Ссылка")
    def __str__(self):
        return self.name
    class Meta:
        verbose_name_plural = "Брэнды"

class Product(models.Model):
    name = models.CharField(max_length = 50, verbose_name = "Название товара")
    description = models.TextField(verbose_name = "Описание")
    brand = models.ForeignKey(Brand)
    size = models.CharField(max_length = 50, verbose_name = "Размер")
    compound = models.CharField(max_length = 100, verbose_name = "Состав")
    price = models.DecimalField(decimal_places = 2, max_digits = 6, verbose_name = "Цена")
    count = models.IntegerField(default = 0, verbose_name = "Количество")
    land = models.CharField(max_length = 50, verbose_name = "Страна производитель")
    image = models.ImageField(upload_to = 'img', verbose_name = "Изображение товара")
    category = models.ForeignKey(CategoriesSecond)
    views = models.IntegerField(default = 0, verbose_name = "Просмотры", blank = True)
    def __str__ (self):
        return self.name
    class Meta:
        verbose_name_plural = "Продукты"

class Order(models.Model):
    last_name = models.CharField(max_length = 256, verbose_name="Фамилия")
    first_name = models.CharField(max_length = 256, verbose_name="Имя")
    two_name = models.CharField(max_length = 256, verbose_name="Отчество")
    phone = models.CharField(max_length = 256, verbose_name="Фамилия")
    stat = models.CharField(max_length = 256, verbose_name="Город")
    street = models.CharField(max_length = 256, verbose_name="Улица")
    house = models.CharField(max_length = 256, verbose_name="Дом")
    index = models.CharField(max_length = 256, verbose_name="Индекс")
    att = models.TextField(verbose_name="Примечания")
    product = models.TextField(verbose_name="Товары")

    def __str__ (self):
        return self.last_name + " " + self.first_name + " " + self.two_name
    class Meta:
        verbose_name_plural = "Заказы"
<file_sep>/catalog/static/catalog/js/scripts.js
$(document).ready(function() {
    $('.top_menu').on('click', 'a.can_open', function(e) {
        e.preventDefault();
        $(this).parent().siblings().find('div').slideUp();
        $(this).next().slideToggle();
    });

    $('.about_company').click(function(e) {
        e.preventDefault();
        $('section.slider').hide();
        $('.unslider-nav').hide();
        $('section.about_block').show();
    });
    $('#about_company_back').click(function(e) {
        e.preventDefault();
        $('section.about_block').hide();
        $('section.slider').show();
        $('.unslider-nav').show();
    });

    
});
<file_sep>/catalog/views.py
from django.shortcuts import render, get_object_or_404
from cart.forms import CartAddProductForm
from .models import Product, CategoriesSecond, CategoriesFirst, Brand, Order
from cart.cart import Cart
from django.http import HttpResponseRedirect
import random
from django.db.models import Q
def index(request):
    brands = Brand.objects.all()[:15]
    popular = Product.objects.all().order_by('-views')[:15]
    products = Product.objects.all()

    categoryes = return_category()
    cart = Cart(request)
    context = {'products':products, 'category_1':categoryes['category_1'], 'category_2':categoryes['category_2'],
               'category_3':categoryes['category_3'], 'cart':cart, 'brands':brands, 'popular':popular}
    return render(request, "catalog/index.html", context)

def item(request, id):
    brands = Brand.objects.all()[:15]
    popular = Product.objects.all().order_by('-views')[:15]
    cart = Cart(request)
    randQuery = Product.objects.order_by('?')[:4]
    categoryes = return_category()
    item = get_object_or_404(Product, id = id)
    item.views += 1
    item.save()
    category = CategoriesSecond.objects.get(name = item.category)
    section = CategoriesFirst.objects.get(name = category.section)
    cart_product_form = CartAddProductForm()
    context = {'item':item, 'cart_product_form':cart_product_form, 'category': category, 'section':section,'category_1':categoryes['category_1'], 'category_2':categoryes['category_2'],
               'category_3':categoryes['category_3'], 'randquery':randQuery, 'cart':cart, 'brands':brands, 'popular':popular}
    return render(request, "catalog/item.html", context)

def category(request, slug):
    brands = Brand.objects.all()[:15]
    popular = Product.objects.all().order_by('-views')[:15]
    cart = Cart(request)
    category = CategoriesSecond.objects.get(slug = slug)
    section = CategoriesFirst.objects.get(name = category.section)
    products = Product.objects.filter(category = category)
    categoryes = return_category()
    context = {'category':category, 'products':products,'category_1':categoryes['category_1'], 'category_2':categoryes['category_2'],
               'category_3':categoryes['category_3'], 'section':section, 'cart':cart, 'brands':brands, 'popular':popular}
    return render(request, "catalog/catalog.html", context)

def brand(request, slug):
    brands = Brand.objects.all()[:15]
    popular = Product.objects.all().order_by('-views')[:15]
    brand = Brand.objects.get(slug = slug)
    products = Product.objects.filter(brand = brand)
    cart = Cart(request)
    categoryes = return_category()
    context = {'cart':cart, 'products' : products, 'brand' : brand, 'brands':brands, 'popular':popular}
    return render(request, "catalog/catalog.html", context)
def return_category():
    woman = CategoriesFirst.objects.get(name = "Женщинам")
    man = CategoriesFirst.objects.get(name = "Мужчинам")
    kid = CategoriesFirst.objects.get(name = "Детям")
    category_1 = CategoriesSecond.objects.filter(section = woman)
    category_2 = CategoriesSecond.objects.filter(section = man)
    category_3 = CategoriesSecond.objects.filter(section = kid)
    categoryes = {'category_1':category_1, 'category_2':category_2, 'category_3':category_3}
    return categoryes

def search(request):
    brands = Brand.objects.all()[:15]
    popular = Product.objects.all().order_by('-views')[:15]
    cart = Cart(request)
    categoryes = return_category()
    if request.method == "POST":
        q = request.POST['q']
        result = Product.objects.filter(Q(name__icontains=q) | Q(description__icontains=q) | Q(description__iexact=q) | Q(name__icontains=q.title()))
        context = {'result':result,'category_1':categoryes['category_1'], 'category_2':categoryes['category_2'],
                   'category_3':categoryes['category_3'], 'q' : q, 'cart':cart, 'brands':brands, 'popular':popular}
        return render(request, "catalog/search.html", context)
    else:
        context = {'category_1':categoryes['category_1'], 'category_2':categoryes['category_2'],
                   'category_3':categoryes['category_3'], 'cart':cart, 'brands':brands, 'popular':popular}
        return render(request, "catalog/search.html", context)

def order(request):
    brands = Brand.objects.all()[:15]
    popular = Product.objects.all().order_by('-views')[:15]
    products = Product.objects.all()
    categoryes = return_category()
    cart = Cart(request)
    context = {'products':products, 'category_1':categoryes['category_1'], 'category_2':categoryes['category_2'],
               'category_3':categoryes['category_3'], 'cart':cart, 'brands':brands, 'popular':popular}
    if request.method == "POST":
        order = Order()
        order.last_name = request.POST['last_name']
        order.first_name = request.POST['first_name']
        order.two_name = request.POST['two_name']
        order.phone = request.POST['phone']
        order.stat = request.POST['stat']
        order.street = request.POST['street']
        order.house = request.POST['house']
        order.index = request.POST['index']
        order.att = request.POST['att']
        order.product = request.POST['product']
        order.save()
        return render(request, "catalog/thank.html", context)
<file_sep>/catalog/admin.py
from django.contrib import admin
from catalog.models import CategoriesFirst,CategoriesSecond, Product, Brand, Order
admin.site.register(Product)
admin.site.register(CategoriesFirst)
admin.site.register(CategoriesSecond)
admin.site.register(Brand)
admin.site.register(Order)
<file_sep>/catalog/migrations/0006_order.py
# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-06-27 20:36
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('catalog', '0005_product_views'),
    ]

    operations = [
        migrations.CreateModel(
            name='Order',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('last_name', models.CharField(max_length=256, verbose_name='Фамилия')),
                ('first_name', models.CharField(max_length=256, verbose_name='Имя')),
                ('two_name', models.CharField(max_length=256, verbose_name='Отчество')),
                ('phone', models.CharField(max_length=256, verbose_name='Фамилия')),
                ('stat', models.CharField(max_length=256, verbose_name='Город')),
                ('street', models.CharField(max_length=256, verbose_name='Улица')),
                ('house', models.CharField(max_length=256, verbose_name='Дом')),
                ('index', models.CharField(max_length=256, verbose_name='Индекс')),
                ('att', models.TextField(verbose_name='Примечания')),
                ('product', models.TextField(verbose_name='Товары')),
            ],
            options={
                'verbose_name_plural': 'Заказы',
            },
        ),
    ]
<file_sep>/user/models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class User(User):
    phone = models.CharField(max_length = 30)
<file_sep>/catalog/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^$', views.index, name="index"),
    url(r'^search/$', views.search, name="search"),
    url(r'^(?P<id>[0-9]+)/$', views.item, name="item"),
    url(r'^category/(?P<slug>[\w-]+)/$', views.category, name="category"),
    url(r'^brand/(?P<slug>[\w-]+)/$', views.brand, name="brand"),
    url(r'^add_order/$', views.order, name="order"),

]
