def get(self):

        #   Default template values that we'll pass over
        template_values = {"status": "ok", "msg": {"kittens": 23, "puppies": 1}}

        #   We first need to see if we have the guardian
        #   api key in memcache...
        settingsJSON = memcache.get("settingsJSON")

        #   ...if we don't the apiKey in memcache then we
        #   will try and fetch it from the database...
        if settingsJSON is None:

            rows = Settings.all()

            #   If there isn't a database entry then we
            #   know that the thing hasn't been setup
            #   so we should send the user to the setup
            #   page
            if rows.count() == 0:
                path = os.path.join(os.path.dirname(__file__), "templates/apiKey.html")
                self.response.out.write(template.render(path, template_values))
            else:
                row = rows[0]
                settingsJSON = json.loads(row.settingsJSON)
                memcache.set("settingsJSON", settingsJSON, 86400)  # 60s * 60m *24h

        #   Now that we have the settings file (and
        #   if we have a settings file we automatically
        #   have the api key, we need to look and see
        #   if we need to backfill the old Picture Desk
        #   Live blogs
        if settingsJSON is not None:
            self.response.write("Check backfill")
    def get(self):

        # build an empty template_values object that we can stuff full
        # of handy information and so on
        template_values = {
          'msg': 'Hello World',
        }

        #   grab the settings file
        #   TODO: put this into memcache
        settingsRows = Settings.all()

        #   If we don't have the settings file, then we need to
        #   take the user to the settings page
        if settingsRows.count() == 0:
            path = os.path.join(os.path.dirname(__file__), 'templates/setup.html')
            self.response.out.write(template.render(path, template_values))
        else:

            #   Grab the JSON out of the settings row
            settingsJSON = simplejson.loads(settingsRows[0].json)

            #   If we don't have enough stories then we need to go to the
            #   setup page and let it carry on backfilling
            if len(settingsJSON['storiesList']) < 60:
                template_values['backfilling'] = True
                template_values['backfill'] = len(settingsJSON['storiesList'])
                path = os.path.join(os.path.dirname(__file__), 'templates/setup.html')
                self.response.out.write(template.render(path, template_values))
            else:
                template_values['settingsJSON'] = settingsJSON
                path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
                self.response.out.write(template.render(path, template_values))
Exemple #3
0
    def __init__(self):
        if not BlogInfo.__shared_values:
            settings = Settings.all()
            hasData = False
            for setting in settings:
                hasData = True
                BlogInfo.__shared_values[setting.name] = setting.value

            if not hasData:
                self.init_settings()

        self.__dict__ = BlogInfo.__shared_values
#!/usr/bin/env python
import os
import simplejson
from admin.models import Settings
from google.appengine.api import memcache
from admin.functions import get_query_string

args = get_query_string(os.environ['QUERY_STRING'])

memkey = 'settings'

itemsRows = memcache.get(memkey)
if itemsRows is None:
    itemsRows = Settings.all()
    memcache.add(memkey, itemsRows, 60 * 1)

if itemsRows.count() == 0:
    newResults = {'stat': 'error'}
else:
    results = simplejson.loads(itemsRows[0].json)
    del results['rejects']
    del results['rejectsList']

    #   TODO: validate limit in args as positive numerical
    if 'limit' in args:
        newStories = {}
        newStoriesList = []
        for i in range(int(args['limit'])):
            newStories[results['storiesList'][i]] = results['stories'][results['storiesList'][i]]
            newStoriesList.append(results['storiesList'][i])
#!/usr/bin/env python
import simplejson

from admin.models import Settings
from admin.functions import fetchStories

#   Get the settings file, if there isn't one then throw an error
settingsRows = Settings.all()

if settingsRows.count() == 0:
    statusJSON = {'status': 'error', 'msg': 'no settingsJSON record'}
else:
    #   Now we need to grab the API key and go fetch a whole bunch
    #   of stories from the Guardian
    apiKey = settingsRows[0].apiKey
    settingsJSON = simplejson.loads(settingsRows[0].json)

    #   go and top up the settingsJSON with a bunch of stories
    fetchStories(1, apiKey, 'prepend', settingsJSON)

    #   Now we need to put the data back into the database
    storeRow = settingsRows[0]
    storeRow.json = simplejson.dumps(settingsJSON)
    storeRow.put()

    statusJSON = {'status': 'ok', 'msg': len(settingsJSON['storiesList'])}

print 'Content-Type: application/json; charset=UTF-8'
print ''
print simplejson.dumps(statusJSON)