Exemple #1
0
def sites():
    if request.method == 'GET':
        query = Site.query()
        compact_sites = []
        for site in query:
            report = Report.query().filter(
                Report.site == site.key).order(-Report.created_on).fetch(1)
            compact_site = Site.to_compact(site)
            compact_site.update(Report.to_dict(report[0]))
            compact_sites.append(compact_site)
        return jsonify({'siteList': compact_sites}), 200
    elif request.method == 'POST':
        request_body = request.get_json()
        name = request_body.get('name', '')
        url = request_body.get('url', '')
        id_info = get_user_id_info(request)
        email = id_info['email']
        try:
            report_results = PageSpeedInights.run(url)
            user_key = User.query().filter(User.email == email).fetch(
                keys_only=True)[0]
            site = Site(name=name, url=url)
            site.created_by = user_key
            site.last_edited_by = user_key
            site_key = site.put()
            report = Report(
                site=site_key,
                accessibility_score=report_results['accessibility_score'],
                best_practices_score=report_results['best_practices_score'],
                desktop_performance_score=report_results[
                    'desktop_performance_score'],
                mobile_performance_score=report_results[
                    'mobile_performance_score'],
                seo_score=report_results['seo_score'],
                pwa_score=report_results['pwa_score'],
            )
            report.put()
            return jsonify({'success': True}), 200
        except:
            raise Exception('Page Speed Insights API returned an error')
    else:
        raise Exception('Method not supported')
Exemple #2
0
def update_all_sites():
    appengine_cron_header = request.headers.get('X-Appengine-Cron')
    if not appengine_cron_header:
        raise Exception(
            'This is a cron task which can only be called from within Appengine'
        )
    sites = Site.query()
    for site in sites:
        report_results = PageSpeedInights.run(site.url)
        report = Report(
            site=site.key,
            accessibility_score=report_results['accessibility_score'],
            best_practices_score=report_results['best_practices_score'],
            desktop_performance_score=report_results[
                'desktop_performance_score'],
            mobile_performance_score=report_results[
                'mobile_performance_score'],
            seo_score=report_results['seo_score'],
            pwa_score=report_results['pwa_score'],
        )
        report.put()

    return jsonify({'success': True}), 200