{"action":"created","comment":{"url":"https://api.github.com/repos/p-p-m/ffw/pulls/comments/31042893","id":31042893,"diff_hunk":"@@ -12,55 +11,101 @@\n from django.core.context_processors import csrf\n from django.shortcuts import render_to_response\n from django.views.decorators.csrf import csrf_protect\n+from django.views.generic import TemplateView\n+from django.utils.decorators import method_decorator\n+\n \n+class CartSet(TemplateView):\n \n-@csrf_protect\n-def cart(request, *args, **kwargs):\n+    @method_decorator(csrf_protect)\n+    def dispatch(self, *args, **kwargs):\n+         return super(CartSet, self).dispatch(*args, **kwargs)\n \n-    if request.is_ajax:\n-        if request.method == 'POST':\n-            c = {}\n-            c.update(csrf(request))\n+    def post(self, request, *args, **kwargs):\n+\n+        if request.is_ajax:\n+            cont = cont_get(request)\n \n-            #  Data of cart in session: {'sum_cart': ..., 'count_cart': ..., 'products': {pk1: {'product_code': ..., 'name': ...,\n-            #  'price': ...}, pk2: {'product_code': ...,'name': ..., 'price': ...}, ....}}.\n-            action = request.POST.get('action', '')\n             request.session['products'] = request.session.get('products', {})\n             request.session['sum_cart'] = 0\n             request.session['count_cart'] = 0\n \n-            #  Action can be: 1 - \"remove\", 2 - 'clear', 3 - \"add\" (or any name includiing '' - its equal '\"add\").\n-            # Status can be: \"added\", \"removed\", \"exist\", \"cleared\"\n-            if action == 'clear':\n-                request.session['products'] = {}\n-                status = 'cleared'\n-                return\n-\n-            # Action is 'remove' or 'add'\n             product_pk = request.POST.get('product_pk', '')\n+            quant = int(request.POST.get('quant', 0))\n+\n             product = get_object_or_404(Product.objects, pk=product_pk)\n             price = float(product.price_uah)\n             name = product.name\n             product_code = product.code\n \n-            if action == 'remove':\n-                del request.session['products'][product_pk]\n-                status = 'remove'\n-            else:\n-\n-                # Action is 'add'\n-                if product_pk in request.session['products'].keys():\n-                    status = 'exist'\n-                else:\n-                    status = 'added'\n-                    request.session['products'][product_pk] = {\n-                        'product_code': product_code,\n-                        'name': name,\n-                        'price': price}\n-\n-            request.session['sum_cart'] = sum(\n-                [v['price'] for v in request.session['products'].values()])\n-            request.session['count_cart'] = len(request.session['products'])\n+            count = 0\n+            if product_pk in request.session['products'].keys():\n+                count = request.session['products'][product_pk].get('count',0)\n+\n+            count += quant\n+            sum_ = round(count * price, 2)\n+\n+            request.session['products'][product_pk] = {\n+                'product_code': product_code,\n+                'name': name,\n+                'price': price,\n+                'count': count,\n+                'sum_': sum_}\n+\n+            result(request)\n \n             return HttpResponse(json.dumps({'sum_cart': request.session['sum_cart'], 'count_cart': (\n-                request.session['count_cart']), 'status': status}), c)\n+                request.session['count_cart'])}), cont)\n+\n+\n+class CartRemove(TemplateView):\n+\n+    @method_decorator(csrf_protect)\n+    def dispatch(self, *args, **kwargs):\n+         return super(CartRemove, self).dispatch(*args, **kwargs)\n+\n+    def post(self, request, *args, **kwargs):\n+        if request.is_ajax:\n+            cont = cont_get(request)\n+\n+            product_pk = request.POST.get('product_pk', '')\n+            request.session[\"products\"].pop(product_pk)\n+            result(request)\n+\n+            return HttpResponse(json.dumps({'sum_cart': request.session['sum_cart'], 'count_cart': (\n+                 request.session['count_cart'])}), cont)\n+\n+\n+class Cart(TemplateView):\n+\n+    @method_decorator(csrf_protect)\n+    def dispatch(self, *args, **kwargs):\n+        return super(Cart, self).dispatch(*args, **kwargs)\n+\n+    def get(self,request, *args, **kwargs):\n+        if request.is_ajax:\n+            return HttpResponse(json.dumps({'products': request.session['products'],\n+                'sum_cart': request.session['sum_cart'], 'count_cart': request.session['count_cart']}))\n+\n+    def post(self, request, *args, **kwargs):\n+        if request.is_ajax:\n+            cont = cont_get(request)\n+            request.session['products'] = {}\n+            request.session['sum_cart'] = 0\n+            request.session['count_cart'] = 0\n+            return HttpResponse(json.dumps({}, cont))\n+\n+\n+def result(request):\n+\n+    request.session['sum_cart'] = round(sum(\n+        [v['sum_'] for v in request.session['products'].values()]), 2)\n+    request.session['count_cart'] = sum(\n+        [v['count'] for v in request.session['products'].values()])\n+\n+\n+def cont_get(request):","path":"ffw/cart/views.py","position":141,"original_position":141,"commit_id":"4af88e09a0ac224cd1ed2e8efa014b6dda217869","original_commit_id":"4af88e09a0ac224cd1ed2e8efa014b6dda217869","user":{"login":"p-p-m","id":1850488,"avatar_url":"https://avatars.githubusercontent.com/u/1850488?v=3","gravatar_id":"","url":"https://api.github.com/users/p-p-m","html_url":"https://github.com/p-p-m","followers_url":"https://api.github.com/users/p-p-m/followers","following_url":"https://api.github.com/users/p-p-m/following{/other_user}","gists_url":"https://api.github.com/users/p-p-m/gists{/gist_id}","starred_url":"https://api.github.com/users/p-p-m/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/p-p-m/subscriptions","organizations_url":"https://api.github.com/users/p-p-m/orgs","repos_url":"https://api.github.com/users/p-p-m/repos","events_url":"https://api.github.com/users/p-p-m/events{/privacy}","received_events_url":"https://api.github.com/users/p-p-m/received_events","type":"User","site_admin":false},"body":"Вместо этого метода лучше просто добавить `django.template.context_processors.csrf` в `settings.TEMPLATE_CONTEXT_PROCESSORS`","created_at":"2015-05-26T15:02:38Z","updated_at":"2015-05-26T15:02:38Z","html_url":"https://github.com/p-p-m/ffw/pull/6#discussion_r31042893","pull_request_url":"https://api.github.com/repos/p-p-m/ffw/pulls/6","_links":{"self":{"href":"https://api.github.com/repos/p-p-m/ffw/pulls/comments/31042893"},"html":{"href":"https://github.com/p-p-m/ffw/pull/6#discussion_r31042893"},"pull_request":{"href":"https://api.github.com/repos/p-p-m/ffw/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/p-p-m/ffw/pulls/6","id":36154624,"html_url":"https://github.com/p-p-m/ffw/pull/6","diff_url":"https://github.com/p-p-m/ffw/pull/6.diff","patch_url":"https://github.com/p-p-m/ffw/pull/6.patch","issue_url":"https://api.github.com/repos/p-p-m/ffw/issues/6","number":6,"state":"closed","locked":false,"title":"Feature/cart","user":{"login":"awsh10","id":5907270,"avatar_url":"https://avatars.githubusercontent.com/u/5907270?v=3","gravatar_id":"","url":"https://api.github.com/users/awsh10","html_url":"https://github.com/awsh10","followers_url":"https://api.github.com/users/awsh10/followers","following_url":"https://api.github.com/users/awsh10/following{/other_user}","gists_url":"https://api.github.com/users/awsh10/gists{/gist_id}","starred_url":"https://api.github.com/users/awsh10/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awsh10/subscriptions","organizations_url":"https://api.github.com/users/awsh10/orgs","repos_url":"https://api.github.com/users/awsh10/repos","events_url":"https://api.github.com/users/awsh10/events{/privacy}","received_events_url":"https://api.github.com/users/awsh10/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-05-25T19:26:46Z","updated_at":"2015-05-26T15:02:38Z","closed_at":"2015-05-25T20:24:04Z","merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/p-p-m/ffw/pulls/6/commits","review_comments_url":"https://api.github.com/repos/p-p-m/ffw/pulls/6/comments","review_comment_url":"https://api.github.com/repos/p-p-m/ffw/pulls/comments{/number}","comments_url":"https://api.github.com/repos/p-p-m/ffw/issues/6/comments","statuses_url":"https://api.github.com/repos/p-p-m/ffw/statuses/4af88e09a0ac224cd1ed2e8efa014b6dda217869","head":{"label":"p-p-m:feature/cart","ref":"feature/cart","sha":"4af88e09a0ac224cd1ed2e8efa014b6dda217869","user":{"login":"p-p-m","id":1850488,"avatar_url":"https://avatars.githubusercontent.com/u/1850488?v=3","gravatar_id":"","url":"https://api.github.com/users/p-p-m","html_url":"https://github.com/p-p-m","followers_url":"https://api.github.com/users/p-p-m/followers","following_url":"https://api.github.com/users/p-p-m/following{/other_user}","gists_url":"https://api.github.com/users/p-p-m/gists{/gist_id}","starred_url":"https://api.github.com/users/p-p-m/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/p-p-m/subscriptions","organizations_url":"https://api.github.com/users/p-p-m/orgs","repos_url":"https://api.github.com/users/p-p-m/repos","events_url":"https://api.github.com/users/p-p-m/events{/privacy}","received_events_url":"https://api.github.com/users/p-p-m/received_events","type":"User","site_admin":false},"repo":{"id":29500769,"name":"ffw","full_name":"p-p-m/ffw","owner":{"login":"p-p-m","id":1850488,"avatar_url":"https://avatars.githubusercontent.com/u/1850488?v=3","gravatar_id":"","url":"https://api.github.com/users/p-p-m","html_url":"https://github.com/p-p-m","followers_url":"https://api.github.com/users/p-p-m/followers","following_url":"https://api.github.com/users/p-p-m/following{/other_user}","gists_url":"https://api.github.com/users/p-p-m/gists{/gist_id}","starred_url":"https://api.github.com/users/p-p-m/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/p-p-m/subscriptions","organizations_url":"https://api.github.com/users/p-p-m/orgs","repos_url":"https://api.github.com/users/p-p-m/repos","events_url":"https://api.github.com/users/p-p-m/events{/privacy}","received_events_url":"https://api.github.com/users/p-p-m/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/p-p-m/ffw","description":"","fork":false,"url":"https://api.github.com/repos/p-p-m/ffw","forks_url":"https://api.github.com/repos/p-p-m/ffw/forks","keys_url":"https://api.github.com/repos/p-p-m/ffw/keys{/key_id}","collaborators_url":"https://api.github.com/repos/p-p-m/ffw/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/p-p-m/ffw/teams","hooks_url":"https://api.github.com/repos/p-p-m/ffw/hooks","issue_events_url":"https://api.github.com/repos/p-p-m/ffw/issues/events{/number}","events_url":"https://api.github.com/repos/p-p-m/ffw/events","assignees_url":"https://api.github.com/repos/p-p-m/ffw/assignees{/user}","branches_url":"https://api.github.com/repos/p-p-m/ffw/branches{/branch}","tags_url":"https://api.github.com/repos/p-p-m/ffw/tags","blobs_url":"https://api.github.com/repos/p-p-m/ffw/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/p-p-m/ffw/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/p-p-m/ffw/git/refs{/sha}","trees_url":"https://api.github.com/repos/p-p-m/ffw/git/trees{/sha}","statuses_url":"https://api.github.com/repos/p-p-m/ffw/statuses/{sha}","languages_url":"https://api.github.com/repos/p-p-m/ffw/languages","stargazers_url":"https://api.github.com/repos/p-p-m/ffw/stargazers","contributors_url":"https://api.github.com/repos/p-p-m/ffw/contributors","subscribers_url":"https://api.github.com/repos/p-p-m/ffw/subscribers","subscription_url":"https://api.github.com/repos/p-p-m/ffw/subscription","commits_url":"https://api.github.com/repos/p-p-m/ffw/commits{/sha}","git_commits_url":"https://api.github.com/repos/p-p-m/ffw/git/commits{/sha}","comments_url":"https://api.github.com/repos/p-p-m/ffw/comments{/number}","issue_comment_url":"https://api.github.com/repos/p-p-m/ffw/issues/comments{/number}","contents_url":"https://api.github.com/repos/p-p-m/ffw/contents/{+path}","compare_url":"https://api.github.com/repos/p-p-m/ffw/compare/{base}...{head}","merges_url":"https://api.github.com/repos/p-p-m/ffw/merges","archive_url":"https://api.github.com/repos/p-p-m/ffw/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/p-p-m/ffw/downloads","issues_url":"https://api.github.com/repos/p-p-m/ffw/issues{/number}","pulls_url":"https://api.github.com/repos/p-p-m/ffw/pulls{/number}","milestones_url":"https://api.github.com/repos/p-p-m/ffw/milestones{/number}","notifications_url":"https://api.github.com/repos/p-p-m/ffw/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/p-p-m/ffw/labels{/name}","releases_url":"https://api.github.com/repos/p-p-m/ffw/releases{/id}","created_at":"2015-01-19T22:51:59Z","updated_at":"2015-01-19T22:55:29Z","pushed_at":"2015-05-25T20:52:17Z","git_url":"git://github.com/p-p-m/ffw.git","ssh_url":"git@github.com:p-p-m/ffw.git","clone_url":"https://github.com/p-p-m/ffw.git","svn_url":"https://github.com/p-p-m/ffw","homepage":null,"size":21172,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"p-p-m:develop","ref":"develop","sha":"19f3935b09fb9bae39a8891c1387a4824febf00d","user":{"login":"p-p-m","id":1850488,"avatar_url":"https://avatars.githubusercontent.com/u/1850488?v=3","gravatar_id":"","url":"https://api.github.com/users/p-p-m","html_url":"https://github.com/p-p-m","followers_url":"https://api.github.com/users/p-p-m/followers","following_url":"https://api.github.com/users/p-p-m/following{/other_user}","gists_url":"https://api.github.com/users/p-p-m/gists{/gist_id}","starred_url":"https://api.github.com/users/p-p-m/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/p-p-m/subscriptions","organizations_url":"https://api.github.com/users/p-p-m/orgs","repos_url":"https://api.github.com/users/p-p-m/repos","events_url":"https://api.github.com/users/p-p-m/events{/privacy}","received_events_url":"https://api.github.com/users/p-p-m/received_events","type":"User","site_admin":false},"repo":{"id":29500769,"name":"ffw","full_name":"p-p-m/ffw","owner":{"login":"p-p-m","id":1850488,"avatar_url":"https://avatars.githubusercontent.com/u/1850488?v=3","gravatar_id":"","url":"https://api.github.com/users/p-p-m","html_url":"https://github.com/p-p-m","followers_url":"https://api.github.com/users/p-p-m/followers","following_url":"https://api.github.com/users/p-p-m/following{/other_user}","gists_url":"https://api.github.com/users/p-p-m/gists{/gist_id}","starred_url":"https://api.github.com/users/p-p-m/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/p-p-m/subscriptions","organizations_url":"https://api.github.com/users/p-p-m/orgs","repos_url":"https://api.github.com/users/p-p-m/repos","events_url":"https://api.github.com/users/p-p-m/events{/privacy}","received_events_url":"https://api.github.com/users/p-p-m/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/p-p-m/ffw","description":"","fork":false,"url":"https://api.github.com/repos/p-p-m/ffw","forks_url":"https://api.github.com/repos/p-p-m/ffw/forks","keys_url":"https://api.github.com/repos/p-p-m/ffw/keys{/key_id}","collaborators_url":"https://api.github.com/repos/p-p-m/ffw/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/p-p-m/ffw/teams","hooks_url":"https://api.github.com/repos/p-p-m/ffw/hooks","issue_events_url":"https://api.github.com/repos/p-p-m/ffw/issues/events{/number}","events_url":"https://api.github.com/repos/p-p-m/ffw/events","assignees_url":"https://api.github.com/repos/p-p-m/ffw/assignees{/user}","branches_url":"https://api.github.com/repos/p-p-m/ffw/branches{/branch}","tags_url":"https://api.github.com/repos/p-p-m/ffw/tags","blobs_url":"https://api.github.com/repos/p-p-m/ffw/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/p-p-m/ffw/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/p-p-m/ffw/git/refs{/sha}","trees_url":"https://api.github.com/repos/p-p-m/ffw/git/trees{/sha}","statuses_url":"https://api.github.com/repos/p-p-m/ffw/statuses/{sha}","languages_url":"https://api.github.com/repos/p-p-m/ffw/languages","stargazers_url":"https://api.github.com/repos/p-p-m/ffw/stargazers","contributors_url":"https://api.github.com/repos/p-p-m/ffw/contributors","subscribers_url":"https://api.github.com/repos/p-p-m/ffw/subscribers","subscription_url":"https://api.github.com/repos/p-p-m/ffw/subscription","commits_url":"https://api.github.com/repos/p-p-m/ffw/commits{/sha}","git_commits_url":"https://api.github.com/repos/p-p-m/ffw/git/commits{/sha}","comments_url":"https://api.github.com/repos/p-p-m/ffw/comments{/number}","issue_comment_url":"https://api.github.com/repos/p-p-m/ffw/issues/comments{/number}","contents_url":"https://api.github.com/repos/p-p-m/ffw/contents/{+path}","compare_url":"https://api.github.com/repos/p-p-m/ffw/compare/{base}...{head}","merges_url":"https://api.github.com/repos/p-p-m/ffw/merges","archive_url":"https://api.github.com/repos/p-p-m/ffw/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/p-p-m/ffw/downloads","issues_url":"https://api.github.com/repos/p-p-m/ffw/issues{/number}","pulls_url":"https://api.github.com/repos/p-p-m/ffw/pulls{/number}","milestones_url":"https://api.github.com/repos/p-p-m/ffw/milestones{/number}","notifications_url":"https://api.github.com/repos/p-p-m/ffw/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/p-p-m/ffw/labels{/name}","releases_url":"https://api.github.com/repos/p-p-m/ffw/releases{/id}","created_at":"2015-01-19T22:51:59Z","updated_at":"2015-01-19T22:55:29Z","pushed_at":"2015-05-25T20:52:17Z","git_url":"git://github.com/p-p-m/ffw.git","ssh_url":"git@github.com:p-p-m/ffw.git","clone_url":"https://github.com/p-p-m/ffw.git","svn_url":"https://github.com/p-p-m/ffw","homepage":null,"size":21172,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/p-p-m/ffw/pulls/6"},"html":{"href":"https://github.com/p-p-m/ffw/pull/6"},"issue":{"href":"https://api.github.com/repos/p-p-m/ffw/issues/6"},"comments":{"href":"https://api.github.com/repos/p-p-m/ffw/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/p-p-m/ffw/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/p-p-m/ffw/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/p-p-m/ffw/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/p-p-m/ffw/statuses/4af88e09a0ac224cd1ed2e8efa014b6dda217869"}}}}