Chapter 11

Cloudwatch_event.vue javascript内包表記 post requests( backend→lambda)

hello_yogurt
hello_yogurt
2022.07.23に更新

javascript内包表記
v-selectに追加で値を入れる

frontend/src/views/Cloudwatch_event.vue
<v-select
  required
  v-model="minute" label="minute" :items="minutes">
</v-select>

<script>
data() {
    const range = (start, stop) => Array.from({ length: (stop - start) + 1}, (_, i) => start + i);
    const minute_l = range(0, 59);
    minute_l.push("*");
    }
</script>

post requests(backend→lambda)

backend/myapp/views.py
lambda_cloudwatch_event_endpoint = "送信先のlambdaのApiGatewayのエンドポイント"

payload = {
        "item_id": item_id,
        "item_name": item_ob_id.item_name,
        "item_url": item_ob_id.item_url,
        "username": item_ob_id.username.username,
        "cloudwatch_event_name" : cloudwatch_event_name,
        "minute"                : minute,
        "hour"                  : hour,
        "day"                   : day,
        "weekday"               : weekday,
        "state"                 : state
        }

        response = requests.post(
            lambda_cloudwatch_event_endpoint,
            data=json.dumps(payload)
        )

lambda_cloudwatch_event_endpoint
→送信先のlambdaのApiGatewayのエンドポイント

受け手のlambda

test_post.py
import json
import requests

def lambda_handler(event, context):
    
    item_id               = event['item_id']
    item_name             = event['item_name']
    item_url              = event['item_url']
    username               = event['username']
    
    backend_endpoint = "http://backend.ドメイン/"
    backend_endpoint_post = "http://backend.ドメイン/post/"
    url = "http://backend.ドメイン/test_post/"
    
    r = requests.get(backend_endpoint)
    
    csrftoken = r.cookies['csrftoken']
    
    payload = {
        "item_id"               : item_id,
        "item_name"             : item_name,
        "item_url"              : item_url,
        "username"              : username
        }
    
    headers = {'csrfmiddlewaretoken': csrftoken, 'Referer': url}
    
    print(headers)
    
    response = requests.post(
        url, 
        data=payload,
        headers=headers
    )
    
    print("21")
    print("response", response)
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }


lambda→backendでcsrfが通らなかったので、django先で@csrf_exemptを指定した

backend/myapp/views.py
@csrf_exempt
def test_post(request):

    if request.method == 'POST':
        post = request.POST
        print(post)
        print("aaaaaaaaaaaaaaaaaaaaaaaaaaa")

        username    = post['username']
        item_name   = post['item_name']
        item_url    = post['item_url']
        stock       = 0
        scraping_at = '0'

        user = User.objects.get(username=username)

        id = user.id

        user_ob_id = User.objects.get(id=id)

        print("user_ob_id", user_ob_id)

        items = Item.objects.filter(item_name=item_name)
        print("items出力", items)
        if not items:
            user_ob_id.item_set.create(
                item_name   = item_name,
                item_url    = item_url,
                stock       = stock,
                scraping_at = scraping_at,
            )
        else:
            items[0].item_name = item_name
            items[0].item_url  = item_url
            items[0].stock     = stock
            items[0].save()

        url = items[0].item_url

        item_stock_pre = item[0].stock

        stock_result, item_stock = scraping(url)

        items[0].stock = stock_result

        print("スクレイピング完了")

        now = datetime.datetime.now(pytz.timezone('Asia/Tokyo'))
        print("now出力", now.strftime('%Y-%m-%d %H:%M:%S'))
        items[0].scraping_at = now.strftime('%Y-%m-%d %H:%M:%S')

        print("items[0].scraping_at出力", items[0].scraping_at)

        line_access_token_ob = LineAccessToken.objects.filter(username=id)
        access_token = line_access_token_ob[0].line_access_token

        if stock_result == 1:
            if item_stock_pre == 0:
                line.stock_availability(items[0], access_token)

        elif stock_result == 0:
            if item_stock_pre == 1:
                line.stock_not_availability(items[0], item_stock, access_token)

        else:
            line.same(items[0], item_stock, access_token)

        items[0].save()

        return JsonResponse(post)