def edit(): if request.method == 'GET': unit_models = Unit.query.all(); class_models=Class.query.all(); carousel_item_models=CarouselItem.query.all(); main_link_models=MainLink.query.all(); #Construct all the JSON maps content=[unit_model.toJSON() for unit_model in unit_models] dates=[class_model.pst_datetime.strftime('%m/%d/%y') for class_model in class_models] carousel_items = [carousel_item_model.toJSON() for carousel_item_model in carousel_item_models] main_links = [main_link_model.toJSON() for main_link_model in main_link_models] return render_template('edit.html', content=formatJSON(content), carousel_items=formatJSON(carousel_items), main_links=formatJSON(main_links), dates=formatJSON(dates), title="Edit JSON") else: #POST method # try: #Todo - better error catching data = json.loads(request.form['all']) # for key, value in data.iteritems(): # print key, value content, dates, carousel_items, main_links = data['content'], data['dates'], data['carousel_items'], data['main_links'] print content, dates, carousel_items, main_links #Seconds since epoch at which a new PST day w/ physics begins epoch_day_offsets = [timegm(time.strptime(t, "%m/%d/%y")) for t in dates] #Zip time info into the topics dictionary date_iter = iter(epoch_day_offsets) new_units = [] #Populate the topics into their respective units, with the dates loaded in too for unit in content: unit_model = Unit() unit_model.title=unit['title'] unit_model.description=unit['description'] unit_model.visible = unit['visible'] for cl in unit['classes']: class_model = Class() #Make an empty model #Fill it with topical values for item in cl['items']: class_model.addItem(item) class_model.homework=cl['homework'] if 'additional' in cl: class_model.additional = cl['additional'] #Fill it with time values (could mess up here) t = date_iter.next() #Seconds since epoch of a new UTC day - could throw an error pst_dt = dt.utcfromtimestamp(t) #Datetime representing the local date and time class_model.epoch_time = t + PST_OFFSET #Seconds since epoch of a new PST day class_model.pst_datetime = pst_dt class_model.day_of_week = int(pst_dt.strftime("%w")) #1 = Monday, 2 = Tuesday, ..., 5 = Friday class_model.week_of_year = int(pst_dt.strftime("%W")) unit_model.addClass(class_model) new_units.append(unit_model) new_carousel_items = [] #Add carousel items for item in carousel_items: new_item = CarouselItem() if 'title' in item: new_item.title=item['title'] if 'description' in item: new_item.description=item['description'] if 'src' in item: new_item.src=item['src'] if 'alt' in item: new_item.alt=item['alt'] new_carousel_items.append(new_item) new_main_links = [] for link in main_links: new_link = MainLink() if 'link' in link: new_link.link = link['link'] if 'media-type' in link: new_link.media_type = link['media-type'] new_main_links.append(new_link); #Now that we have all the models, clear the database and push all the new values on Unit.query.delete() Class.query.delete() CarouselItem.query.delete() MainLink.query.delete() for unit_model in new_units: db.session.add(unit_model) for carousel_item_model in new_carousel_items: db.session.add(carousel_item_model) for main_link_model in new_main_links: db.session.add(main_link_model) db.session.commit() flash('Successfully updated database to reflect changes') # # except Exception as e: # print "Error: " + repr(e) # flash('Uncaught Exception: {0}'.format(e), category='error') return redirect(url_for('admin'))
def edit(): if request.method == 'GET': unit_models = Unit.query.all() class_models = Class.query.all() carousel_item_models = CarouselItem.query.all() main_link_models = MainLink.query.all() #Construct all the JSON maps content = [unit_model.toJSON() for unit_model in unit_models] dates = [ class_model.pst_datetime.strftime('%m/%d/%y') for class_model in class_models ] carousel_items = [ carousel_item_model.toJSON() for carousel_item_model in carousel_item_models ] main_links = [ main_link_model.toJSON() for main_link_model in main_link_models ] return render_template('edit.html', content=formatJSON(content), carousel_items=formatJSON(carousel_items), main_links=formatJSON(main_links), dates=formatJSON(dates), title="Edit JSON") else: #POST method # try: #Todo - better error catching data = json.loads(request.form['all']) # for key, value in data.iteritems(): # print key, value content, dates, carousel_items, main_links = data['content'], data[ 'dates'], data['carousel_items'], data['main_links'] print content, dates, carousel_items, main_links #Seconds since epoch at which a new PST day w/ physics begins epoch_day_offsets = [ timegm(time.strptime(t, "%m/%d/%y")) for t in dates ] #Zip time info into the topics dictionary date_iter = iter(epoch_day_offsets) new_units = [] #Populate the topics into their respective units, with the dates loaded in too for unit in content: unit_model = Unit() unit_model.title = unit['title'] unit_model.description = unit['description'] unit_model.visible = unit['visible'] for cl in unit['classes']: class_model = Class() #Make an empty model #Fill it with topical values for item in cl['items']: class_model.addItem(item) class_model.homework = cl['homework'] if 'additional' in cl: class_model.additional = cl['additional'] #Fill it with time values (could mess up here) t = date_iter.next( ) #Seconds since epoch of a new UTC day - could throw an error pst_dt = dt.utcfromtimestamp( t) #Datetime representing the local date and time class_model.epoch_time = t + PST_OFFSET #Seconds since epoch of a new PST day class_model.pst_datetime = pst_dt class_model.day_of_week = int(pst_dt.strftime( "%w")) #1 = Monday, 2 = Tuesday, ..., 5 = Friday class_model.week_of_year = int(pst_dt.strftime("%W")) unit_model.addClass(class_model) new_units.append(unit_model) new_carousel_items = [] #Add carousel items for item in carousel_items: new_item = CarouselItem() if 'title' in item: new_item.title = item['title'] if 'description' in item: new_item.description = item['description'] if 'src' in item: new_item.src = item['src'] if 'alt' in item: new_item.alt = item['alt'] new_carousel_items.append(new_item) new_main_links = [] for link in main_links: new_link = MainLink() if 'link' in link: new_link.link = link['link'] if 'media-type' in link: new_link.media_type = link['media-type'] new_main_links.append(new_link) #Now that we have all the models, clear the database and push all the new values on Unit.query.delete() Class.query.delete() CarouselItem.query.delete() MainLink.query.delete() for unit_model in new_units: db.session.add(unit_model) for carousel_item_model in new_carousel_items: db.session.add(carousel_item_model) for main_link_model in new_main_links: db.session.add(main_link_model) db.session.commit() flash('Successfully updated database to reflect changes') # # except Exception as e: # print "Error: " + repr(e) # flash('Uncaught Exception: {0}'.format(e), category='error') return redirect(url_for('admin'))