Example #1
0
def newplanet():
    data = request.json
    planet_name = data['planet_name']
    slug = data['slug']
    print "New planet slug:", slug
    print "New planet name:", planet_name
    # check to see if the slug is already in use
    planet = Planet.query.filter_by(slug=slug).first()
    print "Matching planet object:", planet
    if planet:
        print "planet already exists"
        raise Forbidden #403
    else:
        print "no planet has claimed this slug yet - creating new planet"
        planet = Planet()
        db.session.add(planet)
        planet.slug = data['slug']
        planet.name = data['planet_name']
        try:
            db.session.commit()
        except IntegrityError as inst:
            print str(inst)
            raise BadRequest(description="Planet data does not meet database constraints: %s" % str(inst))
    # Flask requires that this function return a json dict but it's not used.
    return "{}"
Example #2
0
def ws_planet_admin(slug):
    """Loads and saves planet & feed data for admin page."""

    if request.method == "POST":
        print "Saving admin page data"

        data = request.json
        planet_id = data['planet_id']
        print "Planet ID:", planet_id
        feeds_to_save = data['feeds']
        to_delete = {}
        print 
        print "========== Start of save =========="
        print_feed_info(feeds_to_save, to_delete)

        # load/create planet data object
        if planet_id == 0: # if a new planet that doesn't exist in the DB
            planet = Planet()
            db.session.add(planet)
        else: # if planet already exists in the DB
            planet = Planet.query.filter_by(slug=slug).first()

            # load planet's feeds from feed table
            db_feeds = Feed.query.filter_by(planet_id=planet_id).all()
            
            # create checklist with db feed URLS to determine which ones no longer exist in the DOM for later deletion
            for feed in db_feeds:
                to_delete[feed.id] = feed

        # save Planet data
        planet.slug = data['slug']
        planet.name = data['planet_name']
        planet.desc = data['planet_desc']

        # Save all of the feeds. \ΓΈ/
        for feed in feeds_to_save:
            # check to see if it's in the DB, only add if it's not there.
            if planet_id != 0:
                # ID feeds in DB, update any changed feed data, & remove it from list of feeds to delete.
                is_new = update_feeds(feed, db_feeds, to_delete) # returns False if it exists in DB
            else:
                is_new = True
            print "Is feed", feed['url'], "new?", is_new
            
            if is_new:
                print "creating a new feed object for the new feed"
                # todo: make sure feed content is not empty. if it is, ignore it.
                # (or do this with the form validation javascript?)
                newfeed = Feed()
                newfeed.name = feed['name']
                newfeed.url = feed['url']
                newfeed.image = feed['image']
                newfeed.planet = planet
                db.session.add(newfeed)


        print "Deleting %i removed feeds." % (len(to_delete))
        print "to_delete:", to_delete
        for feed in to_delete:
            
            # printouts for troubleshooting 
            feed_obj = to_delete[feed]
            print feed_obj
            feed_entries = FeedContent.query.filter_by(feed_id=feed).all()
            print len(feed_entries), "entries associated with this feed."

            db.session.delete(to_delete[feed])

        try:
            db.session.commit()
        except IntegrityError as inst:
            print str(inst)
            raise BadRequest(description="Planet data does not meet database constraints: %s" % str(inst))

        # Flask requires that this function return json but it's not used.
        return json.dumps({})


    else: #GET
        print "Loading"

        # load planet data:
        planet = Planet.query.filter_by(slug=slug).first()
        try:
            planet_name = planet.name
        except AttributeError:
            print "No planet found with slug %s. Please check the URL and try again." % (slug)
            raise NotFound
        planet_desc = planet.desc
        planet_id = planet.id

        # load planet feeds:
        db_feeds = Feed.query.filter_by(planet_id=planet_id).all()

        # build feed list from DB feeds for jsonification
        feeds = []
        for feed in db_feeds:
            newfeed = {}
            newfeed['id'] = feed.id
            newfeed['url'] = feed.url
            newfeed['name'] = feed.name
            newfeed['image'] = feed.image
            feeds.append(newfeed)
        print "All feeds:", feeds

        # package data for jsonification
        jdata = {'planet_id':planet_id, 'slug':slug, 'planet_name':planet_name, 'planet_desc':planet_desc, 'feeds':feeds}
        return json.dumps(jdata)