#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: langer
@software: PyCharm
@DATE: 2019/7/15 15:16
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: WebHook.py
@Contact: chanjunkai@163.com
"""
import json
import os
from django.http import HttpResponse
from django.views.generic import TemplateView

from object.ResponseObject import ResponseObject


class HookWebView(TemplateView):
    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation', None)
        request_dict = json.loads(request.body.decode('utf-8'))
        return self.validate(request_dict, operation)

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        request_dict = request.GET
        operation = kwargs.get('operation', None)
        return self.validate(request_dict, operation)

    def validate(self, request_dict, operation):
        response = ResponseObject()
        item = request_dict.get('item', None)
        if item == 'alexaOA':
            os.system("cd /web/amazon-alexa-camera;git pull;supervisorctl restart alexaOA")
            return response.json(0)
        else:
            return HttpResponse(status=404)
