Example #1
0
def attach_pictures(request):
    menu_query = DBSession.query(Menu).filter(Menu.id==request.matchdict['menu_id'])
    images_id = menu_query.one().images_id
    images_id = images_id.split(' ') if images_id else []
    if images_id:
        images = DBSession.query(Image).filter(Image.id.in_(images_id)).all()
    added_thumbs = []
    if "data" in request.params:
        data = json.loads(request.params["data"])
        for image_info in data:
            image, thumb = image_info
            if not image or not thumb:
                continue
            ext = os.path.splitext(image)[-1].lower()
            if ext in (".jpg", ".jpeg", ".png"):
                new_image = Image(image_url=image, thumb_url=thumb)
                added_thumbs.append(thumb)
                DBSession.add(new_image)
                DBSession.flush()
                DBSession.refresh(new_image)
                images_id.append(new_image.id)
        menu_query.update({
                "images_id": ' '.join([str(i) for i in images_id]),
                })
    return json.dumps(added_thumbs)
Example #2
0
 def add_record(self):
     """Verifies post form and saves record to database."""
     form_json = self.request.json
     with transaction.manager:
         record = Record.from_dict(form_json)
         DBSession.add(record)
         # refresh record before commit to send creted Record in response
         DBSession.flush()
         DBSession.refresh(record)
         response_json = record.to_json()
         transaction.commit()
     return Response(status=201, json=response_json)
Example #3
0
def create_menu_item(request):
    #create the new menu item entry
    new_menu_item = MenuItem(name=request.params['name'].encode('utf8'), description=request.params['description'].encode('utf8'), menu_id=request.matchdict['menu_id'], healthy=int(request.params['healthy']))
    DBSession.add(new_menu_item)
    DBSession.flush()
    DBSession.refresh(new_menu_item)

    #create corresponding allergens
    for allergen in ALLERGENS:
        if allergen in request.params:
            new_allergen = Allergen(menu_item_id=new_menu_item.id, allergen = allergen)
            DBSession.add(new_allergen)

    #find the corresponding menu
    menuQuery = DBSession.query(Menu).filter(Menu.id==request.matchdict['menu_id'])
    menu = menuQuery.one()
    menu_items = menu.menus

    #update the menu items on the menu
    if len(menu_items) > 0:     #just append
        menu_items = menu_items.split(' ')
        menu_items = map(int, menu_items)
        menu_items.append(int(new_menu_item.id))
    else: #create the first one
        menu_items = [int(new_menu_item.id)]

    menu_items_string = ' '.join(str(menu_item_id) for menu_item_id in menu_items)
    menu.menus = menu_items_string
    menuQuery.update({"menus":menu_items_string}, synchronize_session=False)
    #update menu name if needed
    first_menu_item = DBSession.query(MenuItem).filter(MenuItem.id==menu_items[0]).one()
    #if we just inserted the first item in the menu, update the menu name with the first item name
    if first_menu_item.id == new_menu_item.id:
        menuQuery.update({"name":first_menu_item.name})
    #update google calendar
    update_gcalendar(menu)

    url = request.route_url('edit_menu', cafe_id=int(request.matchdict['cafe_id']), menu_id=request.matchdict['menu_id'])
    return HTTPFound(location=url)
Example #4
0
def create_menu(request):
    #see if there's already a menu created with the same params
    m = request.matchdict
    menu = DBSession.query(Menu).filter(Menu.cafe_id==request.matchdict['cafe_id'])
    if 'date' in request.params:
        menu = menu.filter(Menu.date==request.params['date'])
    if 'time' in request.params:
        menu = menu.filter(Menu.time_sort_key==int(request.params['time']))
    menu = menu.first()

    #if not, create one
    if menu is None:
        # verify date
        date = request.params['date']
        if date is '0000-00-00':
            return HTTPFound(location= request.route_url('edit_menus_today'))
        menu = Menu(cafe_id=int(m['cafe_id']), name='', date=request.params['date'], time_sort_key=request.params['time'], menus='', sent=False)
        DBSession.add(menu)
        DBSession.flush()
        DBSession.refresh(menu)
    url = request.route_url('edit_menu', cafe_id=int(m['cafe_id']), menu_id=menu.id, allergen_list=ALLERGENS, healthy_factor=HEALTHY_FACTOR)
    update_gcalendar(menu)

    return HTTPFound(location=url)