def add_product(request): add_product_form = AddProductForm(request.GET) if add_product_form.is_valid(): product_name = add_product_form.cleaned_data['product_name'] hashtag_list = add_product_form.cleaned_data['hashtags'].split(', ') period_start = request.GET['period'] context = { 'hashtags': hashtag_list, 'product_name': product_name } context['sentiments'] = fetch_sentiments(product_name, hashtag_list, period_start) return HttpResponse( json.dumps(context), content_type='application/json', status=200 ) else: return HttpResponse(json.dumps({ 'errors': add_product_form.errors }), content_type='application/json', status=200 )
def update_period(request): products = json.loads(request.GET['products']) period_start = request.GET['period'] context = list() for product in products: context.append({ 'product_name': product['product_name'], 'hashtags': product['hashtags'], 'category': product['category'], 'sentiments': fetch_sentiments(product['product_name'], product['hashtags'], period_start) }) return HttpResponse( json.dumps(context), content_type='application/json', status=200 )