def get(self): profile_path = self.request.get("profile_path", None) country = self.request.headers["X-AppEngine-Country"] cache_time = 10 * 60 headers.json(self.response) host = "http://www.theguardian.com" if gae.is_development(): host = "*" headers.set_cors_headers(self.response, host=host) if not profile_path: abort(400) headers.set_cache_headers(self.response, cache_time) key = ndb.Key(Contributor, profile_path) contributor = key.get() if not contributor: abort(404) data = { "personal" : contributor.twitter_handle, "brand" : "guardian", } if contributor.twitter_brand_handle: data['brand'] = contributor.twitter_brand_handle self.response.out.write(json.dumps(data))
def get(self): client = memcache.Client() query_str = self.request.query_string key = None if query_str: key = make_key(query_str) logging.info(key) if not key: key = 'all' logging.info(key) ophan_json = client.get(key) if not ophan_json: refresh_data(key) ophan_json = "[]" last_read = client.get(key + ".epoch_seconds") if last_read and not fresh(last_read): refresh_data(key) headers.json(self.response) headers.set_cache_headers(self.response, 60) headers.set_cors_headers(self.response) if 'view=html' in query_str: html = generate_html(ophan_json) if html is not None: self.response.write(formats.jsonp(self.request, json.dumps({"html" : html}))) else: self.error(404) self.response.out.write('404 page') else: self.response.out.write(formats.jsonp(self.request, ophan_json))
def get(self, max_or_min=None, star_value=None): filtered_reviews = film_reviews.filtered_reviews(max_or_min, star_value) logging.info(filtered_reviews) headers.json(self.response) headers.cache(self.response, 60) headers.cors(self.response) self.response.out.write(json.dumps(filtered_reviews))
def post(self): headers.cors(self.response) if not "score" in self.request.params: abort(400, "No score posted") if not "path" in self.request.params: abort(400, "No path supplied") score = self.request.params.get("score") path = self.request.params.get("path") quiz = ndb.Key(Quiz, path).get() if not quiz: quiz = Quiz(id=path, path=path) quiz.put() QuizScore(parent=quiz.key, score=int(score)).put() recalculation_flag = ndb.Key(QuizNeedingRecalculation, path).get() if not recalculation_flag: QuizNeedingRecalculation(id=path, path=path).put() headers.json(self.response) self.response.out.write(json.dumps({"score": score}))
def get(self): data = {"status" : "ok"} create_archive() headers.json(self.response) self.response.out.write(json.dumps(data))
def get(self): headers.set_cors_headers(self.response) headers.json(self.response) template = jinja_environment.get_template("js/user-interaction.js") template_values = {} self.response.out.write(template.render(template_values))
def get(self): data = {"status" : "ok"} read_todays_content() headers.json(self.response) self.response.out.write(json.dumps(data))
def get(self): for quiz in QuizNeedingRecalculation.query(): quiz_data = ndb.Key(Quiz, quiz.path).get() if quiz_data: deferred.defer(generate_summary, quiz_data.key, _countdown=random.randint(1, 30), _queue='quiz-results') quiz.key.delete() headers.json(self.response) self.response.out.write(json.dumps({"status" : "Summaries scheduled"}))
def get(self, checksum): captures_qry = CapturedSelection.query(CapturedSelection.checksum == checksum) count = 0 for capture in captures_qry.iter(): capture.key.delete() count += 1 headers.json(self.response) self.response.out.write({"Entries deleted" : count})
def get(self, section=None): headers.json(self.response) campaignType = self.request.get('campaignType') if campaignType == 'ga': codes = short_codes_ga.codes(section) else: codes = short_codes.codes(section) self.response.write(json.dumps(codes))
def get(self): data = {"status" : "ok"} content = read_todays_content() if content: memcache.set("today", json.dumps(content)) data['results'] = content headers.json(self.response) self.response.out.write(json.dumps(data))
def get(self): data = {"hello" : "world"} headers.json(self.response) ophan_data = read_weeks_ophan_data() data['ophan_data'] = ophan_data for content in ophan_data: logging.info(content) OphanData(url = content['url'], count = content['count']).put() self.response.out.write(json.dumps(data))
def get(self): date = today() data = {"date" : date} data.update(generate_totals(date)) data.update(reading_time(data)) if 'wordcount' in data: data['literature'] = literature(data['wordcount']) headers.json(self.response) self.response.out.write(json.dumps(data))
def get(self, minimum, maximum): result = {} data = memcache.get("today") if data: all_content = json.loads(data) valid_filter = lambda x: data_filters.has_wordcount(x) and data_filters.minutes(int(minimum), int(maximum))(x) and data_filters.valid_section(x) appropriate_content = [calculate_reading_time(i) for i in all_content if valid_filter(i)] sorted_content = sorted(appropriate_content, key = lambda x: int(extract.wordcount(x)), reverse = True) result['content'] = sorted_content headers.json(self.response) self.response.out.write(json.dumps(result))
def post(self): byline = self.request.get("byline", None) country = self.request.headers["X-AppEngine-Country"] if not byline: abort(400) headers.json(self.response) headers.set_cors_headers(self.response) headers.set_cache_headers(self.response, cache_seconds) data = twitter_lookup[byline] if not "brand" in data: data["brand"] = brand_by_country.get(country, "guardian") self.response.out.write(json.dumps(data))
def get(self): emails_to_send = models.email.unsent_emails() logging.info(emails_to_send) sender_address = configuration.lookup('EMAIL_FROM') for email in emails_to_send: mail.send_mail(sender_address, email.to, email.subject, email.message) logging.info(email) email.sent = True email.put() headers.json(self.response) output = {'pending_emails': emails_to_send.count()} self.response.out.write(json.dumps(output))
def get(self, rota_id): if not 'api-key' in self.request.GET: webapp2.abort(400, 'No API key specified') return api_key = self.request.get('api-key') if api_key not in configuration.lookup('VALID_API_KEYS', "").split(','): webapp2.abort(400, 'The API key {0} specified is invalid'.format(api_key)) return rota = models.lookup(rota_id) payload = { 'rota_id': rota_id, 'current': rota.current, } headers.json(self.response) self.response.out.write(json.dumps(payload))
def get(self): data = {"hello" : "world"} headers.json(self.response) cached_data = memcache.get("popular_content") if not cached_data: ophan_data = read_weeks_ophan_data() popular_content = [read_content(result) for result in ophan_data] popular_content = [item for item in popular_content if item] data['popular_content'] = popular_content memcache.set("popular_content", json.dumps(popular_content)) if cached_data: data['popular_content'] = json.loads(cached_data) self.response.out.write(json.dumps(data))
def get(self, section_id = None): if not section_id: section_id = 'all' client = memcache.Client() ophan_json = client.get(section_id) if not ophan_json: refresh_data(section_id) ophan_json = "[]" last_read = client.get(section_id + ".epoch_seconds") if last_read and not fresh(last_read): refresh_data(section_id) headers.json(self.response) headers.set_cache_headers(self.response, 60) headers.set_cors_headers(self.response) self.response.out.write(formats.jsonp(self.request, ophan_json))
def get(self, section_id=None): if not section_id: section_id = 'all' client = memcache.Client() ophan_json = client.get(section_id) if not ophan_json: refresh_data(section_id) ophan_json = "[]" last_read = client.get(section_id + ".epoch_seconds") if last_read and not fresh(last_read): refresh_data(section_id) headers.json(self.response) headers.set_cache_headers(self.response, 60) headers.set_cors_headers(self.response) self.response.out.write(formats.jsonp(self.request, ophan_json))
def get(self, target_date): headers.json(self.response) cache_key = "json.%s" % target_date json_result = memcache.get(cache_key) if not json_result: archive_date = datetime.datetime.strptime(target_date, '%Y-%m-%d') json_result = json.dumps(read_summary_data(archive_date)) try: memcache.set(cache_key, json_result, 60) except: pass headers.set_cache_headers(self.response, 60) self.response.out.write(jsonp(self.request, json_result))
def get(self): headers.json(self.response) cache_key = "json.today" json_result = memcache.get(cache_key) if not json_result: today = datetime.date.today() json_result = json.dumps(read_summary_data(today)) try: memcache.set(cache_key, json_result, 60) except: pass headers.set_cache_headers(self.response, 60) self.response.out.write(jsonp(self.request, json_result))
def get(self): headers.cors(self.response) headers.json(self.response) if not "path" in self.request.params: abort(400, "No path supplied") path = self.request.params.get("path") data = memcache.get(path, namespace="results") if data: self.response.write(data) return quiz = ndb.Key(Quiz, path).get() quiz_average = ndb.Key('QuizAverage', quiz.path).get() data = {'average_score': quiz_average.average_score} self.response.out.write(json.dumps(data))
def get(self, edition): client = memcache.Client() key = edition content_json = client.get(key) logging.info("content from memcache with key %s" % key) if not content_json: logging.info("no content found in memcache") content_json = content_api.editors_picks(edition) editors_picks = (json.loads(content_json)).get('response').get('editorsPicks') if editors_picks is None: logging.warning("could not parse editorsPicks from the response body") return_json = content_json else: return_json = json.dumps(editors_picks) headers.json(self.response) headers.set_cache_headers(self.response, 60) headers.set_cors_headers(self.response) self.response.out.write(formats.jsonp(self.request, return_json))
def get(self): rotas = models.rotas_due(datetime.date.today()) for rota in rotas: logging.debug(rota) next_start_date = rota.next_start_date + datetime.timedelta(days=rota.interval) if rota.members: queue_emails(rota, next_start_date) rota.current = rota.members[0] rota.next_start_date = next_start_date if len(rota.members) > 1: rota.members.append(rota.members[0]) rota.members = rota.members[1:] rota.put() headers.json(self.response) self.response.write(json.dumps({"rotas": [r.name for r in rotas]}))
def get(self): headers.json(self.response) self.response.out.write(json.dumps({"hello": "world"}))
def post(self): stars = self.request.POST.get('stars', None) movie_id = self.request.POST.get('movie_id', None) if not (stars and movie_id): abort(400, "Parameters not supplied") try: stars = int(stars) if not (0 < stars < 6): abort(400, "Star rating must be between 1 and 5") except ValueError: abort(400, "Need a numeric value for stars") data = { 'ip_address' : self.request.remote_addr, 'stars' : stars, 'movie_id' : movie_id } user_ratings = [] query = StarReview.query(StarReview.movie_id == movie_id, StarReview.ip_address == self.request.remote_addr) if not query.iter().has_next(): StarReview(movie_id=movie_id, stars=stars, ip_address=self.request.remote_addr).put() else: current_review = query.iter().next() current_review.stars = stars current_review.put() reviews = StarReview.query(StarReview.movie_id == movie_id) if not reviews: # Screw you eventual consistency! user_ratings.append(int(stars)) user_ratings.extend([review.stars for review in reviews]) summary_key = ndb.Key('StarReviewSummary', movie_id) summary = summary_key.get() if not summary: summary = StarReviewSummary(id=movie_id, movie_id=movie_id, average_rating=stars, max_rating=stars, min_rating=stars, ratings=1) if len(user_ratings) > 1 and not summary: # Recover from previous failure to generate summary summary = StarReviewSummary(id=movie_id, movie_id=movie_id, average_rating = sum(user_ratings) / len(user_ratings), max_rating = max(user_ratings), min_rating = min(user_ratings), ratings = len(user_ratings)) if summary: average_rating = int(sum(user_ratings) / len(user_ratings)) summary.average_rating = average_rating summary.max_rating = max(user_ratings) summary.min_rating = min(user_ratings) summary.ratings = len(user_ratings) summary.put() data['summary'] = summary_model_to_dict(summary) data['ratings_summary_text'] = ratings.ratings_summary_text(summary) headers.json(self.response) headers.cors(self.response) self.response.out.write(json.dumps(data))
def get(self): headers.json(self.response) headers.set_cors_headers(self.response) headers.set_cache_headers(self.response, cache_seconds) self.response.out.write(json.dumps(contributors))