Beispiel #1
0
 def ajax(self, request):
     print 'in ajax'
     code = self.kwargs['code']
     #print 'new code is ' + code
     self.form_class = self.get_form_class();
     #print 'class is ' + str(self.form_class)
     form = self.form_class(data=json.loads(request.body))
     if form.is_valid():
         company_id = request.user.get_company()
         existingIntegration = CompanyIntegration.objects(company_id = company_id ).first()
         if existingIntegration is not None:
             if code in existingIntegration.integrations: # record for this system found - update
                 existingDict = existingIntegration.integrations
                 existingDict[code] = form.cleaned_data
                 existingDict[code]['access_token'] = ''
                 CompanyIntegration.objects(company_id = company_id ).update(integrations=existingDict)
                 print "Updated data for " + code
                 return HttpResponse("Updated data for " + code, status=status.HTTP_200_OK)
             else:
                 #existingIntegration.integrations[code] = form.cleaned_data
                 existingDict = existingIntegration.integrations
                 existingDict[code] = form.cleaned_data
                 #existingDict[code] [code]['access_token'] = ''
                 CompanyIntegration.objects(company_id = company_id ).update(integrations=existingDict)
                 return HttpResponse('New integration added to existing data', status=status.HTTP_200_OK)
         companyIntegration = CompanyIntegration()
         companyIntegration.company_id = company_id
         companyIntegration.integrations[code] = form.cleaned_data
         try:
             #print 'saving'
             companyIntegration.save()
             return HttpResponse('New integration added', status=status.HTTP_200_OK)   
         except Exception as e:
             print str(e)
             return HttpResponse(str(e), status=status.HTTP_400_BAD_REQUEST)
     else:
         response_data = {'errors': form.errors} #, 'success_url': force_text(self.success_url)
         return HttpResponse(json.dumps(response_data), content_type="application/json")