def post(self): logging.info('got mail') # self.validate_simple_auth() try: body = json.loads(self.request.body) except ValueError: self.abort(400, 'Expected JSON email description.') email = body.get('Sender') account = models.Account.query(models.Account.email == email).get() if account is None: self.abort(400, 'Unrecognized email address: %s' % email) account.last_response_date = datetime.now() account.put() full_email_text = body.get('Text-part') response_text = extract_latest_message(full_email_text) logging.info('got mail') logging.info(email) logging.info(body) logging.info(response_text) response = models.Response( parent=account.key, text=response_text) response.put()
def index(): if request.method == "POST": # get form data - create new response promptID = request.form.get('id', 'none') prompt = models.Prompt.objects.get(id=promptID) response = models.Response() response.responseText = request.form.get('response', 'none') response.likes = 1 prompt.responses.append(response) prompt.save() templateData = { 'prompts': models.Prompt.objects.limit(1), 'response': request.form.get('response', 'none') } return render_template("discussion.html", **templateData) else: # render the template templateData = {'prompts': models.Prompt.objects.limit(1)} return render_template("main.html", **templateData)
def handle_request(req): return models.Response( status_code=200, reason_phrase='OK', headers={'Content-Type': 'application/json'}, body=json.dumps(req.headers), )
def handle_request(connection: socket.socket, address, logger, root_dir): logger.debug("Connected at %r", address) try: req = models.Request(connection.recv(1024)) except IndexError: connection.send(b'Non HTTP protocol used') connection.close() logger.debug("Connection closed") return is_dir = False path = root_dir + urllib.parse.unquote(urllib.parse.urlparse(req.URL).path) if path[-1] == '/': is_dir = True path += 'index.html' resp_code = 200 if not os.path.exists(path): if is_dir: resp_code = 403 else: resp_code = 404 if path.find('../') != -1: resp_code = 403 resp: models.Response if resp_code == 200 and req.Method in methods: size = os.path.getsize(path) resp = models.Response(req.Protocol, req.Method, resp_code, mimetypes.guess_type(path)[0], size) else: resp = models.Response(req.Protocol, req.Method, resp_code) logger.debug(resp_code) connection.sendall(resp.get_raw_headers()) if req.Method == 'GET' and resp_code == 200: file = open(path, 'rb') connection.sendfile(file, 0) file.close() connection.shutdown(socket.SHUT_RDWR) connection.shutdown(socket.SHUT_RDWR) logger.debug("Connection closed")
def submit_response(session, studentID, questionID, response): vale = 0 if response: vale = 1 obj = models.Response(studentID=studentID, questionID=questionID, valid=vale) session.add(obj) session.commit() return {"message": "Submitted"}
def test_get_like(self): """get_like() should use the Response stored in the datastore.""" like = { 'objectType': 'activity', 'verb': 'like', 'id': 'tag:twitter.com,2013:222', 'object': {'url': 'http://my/favorite'}, } models.Response(id='tag:twitter.com,2013:000_favorited_by_222', response_json=json.dumps(like)).put() self.assert_equals(like, self.tw.get_like('unused', '000', '222'))
def surveyStart(): """Begin a survey This view creates a response object if none exists and provides the user with some additional information about the survey. Additionally, bot checks are made with a honeypot and a simple math question. """ # If it's a POST request, we need to check for bots. if request.method == 'POST': result = -100 try: result = int(request.form.get('result', '-100')) except ValueError: pass if (result == session.get('add_a', 0) + session.get('add_b', 0)) \ and request.form.get('hp_field', '') == '': return redirect('/survey/overview/') else: flash('''Please ensure that you have answered the simple question below to start the survey!''') # Create a new response object if none exists. if session.get('response_id') is not None: survey = models.Response.objects.get(id=session['response_id']) else: survey = models.Response() survey.metadata = models.ResponseMetadata( client_ip=request.remote_addr, client_ua=str(request.user_agent)) start_tp = models.Touchpoint(touchpoint_type=0) survey.metadata.touchpoints.append(start_tp) survey.save() session['response_id'] = str(survey.id) if len( models.Response.objects.filter( metadata__client_ip=request.remote_addr)) > 1: flash('''It appears that someone has already completed the furry survey from this computer or IP address. If this is a public computer, a household with multiple people sharing one IP address, or you believe that you have not completed this survey, please feel free to continue; otherwise, please <a href="/survey/cancel">cancel the survey</a> if you have already completed it.''') survey.metadata.touchpoints.append( models.Touchpoint(touchpoint_type=-6)) # Prepare bot checks. session['add_a'] = add_a = random.randint(1, 10) session['add_b'] = add_b = random.randint(1, 10) return render_template('start.html', survey_id=str(survey.id), add_a=add_a, add_b=add_b)
def post(self, request, pk): if not request.user.is_authenticated(): return http.HttpResponse(status=401) text = request.POST.get('answer') if not text: return http.HttpResponseBadRequest() try: question = models.Question.objects.get(pk=pk) except models.Question.DoesNotExist: return http.HttpResponseNotFound() response = models.Response(text=text, question=question, author=request.user.ask_user, is_right=False, created=datetime.now()) response.save() return http.HttpResponse()
def setUp(self): self.app = webtest.TestApp(api.app) self.testbed = testbed.Testbed() self.testbed.activate() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() self.testbed.setup_env(account_id_override=ACCOUNT_ID, overwrite=True) # This prevents data from leaking between tests. ndb.get_context().set_cache_policy(False) email = '*****@*****.**' account = models.Account(id=ACCOUNT_ID, email=email) account.put() for i in range(0, 89): models.Response(parent=account.key, text='My idea number %s.' % i).put()