def example_data(): """Sample data for tests file.""" #Fake users user1 = User( user_id=1, name='Trinity Dunbar', username='******', password='******', email='*****@*****.**', location='San Francisco, CA, USA', ) user2 = User( user_id=2, name='Trinity Gaerlan', username='******', password='******', email='*****@*****.**', loation='Oakland, CA, USA', ) #Fake bookmarks bookmark1 = Bookmark(bookmark_id=1, bookmark_type=1, event_id='62010309505', user_id=1, timestamp=datetime.now()) bookmark2 = Bookmark(bookmark_id=2, bookmark_type_id=2, event_id='62010309505', user_id=2, timestamp=datetime.now())
def add_bookmark(): temp_title = add_title() temp_link = add_link() temp_details = add_details() bm = Bookmark(title=temp_title, link=temp_link, details=temp_details) bm.save() print('Your bookmark was added!')
def test_cannot_add_same_url_multiple_times(): bookmark1 = Bookmark("dc3a2e08-9e2b-4838-9719-bd1ce4a647b1", "google", 'https://www.google.com/', 'first notes', date.today()) bookmark2 = Bookmark("dc3a2e08-9e2b-4838-9719-bd1ce4a647b2", "google", 'https://www.google.com/', 'seconnd notes', date.today())
def example_data(): """Create some sample data for tests file to use.""" # Create some fake users u1 = User(user_id=1, email="*****@*****.**", name="Jessi DiTocco", password="******") u2 = User(user_id=2, email="*****@*****.**", name="Liz Lee", password="******") # Create some fake events e1 = Event(event_id="43104373341", name="Aristophanes @ Cafe du Nord", start_time="Friday, February 16 at 9:30PM+", end_time="Saturday, February 17 at 2:00AM", address="2174 Market Street, San Francisco, CA 94114", latitude=37.7649659, longitude=-122.431, venue_name="Slate") e2 = Event(event_id="41465350981", name="Funk", start_time="Friday, February 15 at 9:30PM+", end_time="Saturday, February 18 at 2:00AM", address="4123 Market Street, San Francisco, CA 94114", latitude=39.7649659, longitude=-122.431, venue_name="The Jam") # Create some bookmark types bt1 = BookmarkType(bookmark_type_id=1, bookmark_type="going") bt2 = BookmarkType(bookmark_type_id=2, bookmark_type="interested") # Create some fake bookmarks b1 = Bookmark(bookmark_id=1, user_id=1, event_id="43104373341", bookmark_type_id=2) b2 = Bookmark(bookmark_id=2, user_id=2, event_id="43104373341", bookmark_type_id=1) # Create some fake Comments c1 = Comment(comment_id=1, user_id=1, event_id="43104373341", comment="HI!") c2 = Comment(comment_id=2, user_id=2, event_id="43104373341", comment="HI!") db.session.add_all([u1, u2, e1, e2, bt1, bt2, b1, b2, c1, c2]) db.session.commit()
def post(self): if self.request.files and self.request.files['bookmark_file']: self.bookmark_file = self.request.files['bookmark_file'][0] if self.bookmark_file['content_type'] == 'text/html': # 解析上传的html 编码上有点小问题 self.bookmarks = self.parser_input( self.bookmark_file['body'].decode('utf-8', 'ignore')) # 添加到数据库 Bookmark.input_bookmarks(self.bookmarks) self.redirect('/dashboard')
def save_result(): """handle users saving Etsy results, with JSON data from get-item-info route""" # print request listing_data = request.get_json() #should get JSON blob back for shopstyle API item info print type(listing_data) #should be string; it is type UNICODE shop_id = listing_data['id'] #Check if listing already in DB? listing = EtsyResult.query.filter(EtsyResult.etsy_listing_id == shop_id).first() #if listing it's not there in EtsyResult, create it! if not listing: listing = EtsyResult(etsy_listing_id=listing_data["id"], listing_title=listing_data["name"], listing_url=listing_data["clickUrl"], listing_image=listing_data["image"]["sizes"]["Best"]["url"], listing_price=listing_data["price"]) #check this # listing_brand=listing_data["brand"]["name"]) # not going to use price $ label here; # db.session.add(listing) db.session.commit() #add the bookmark to bookmark table - regardless of whether or not listing has already been bookmarked bookmark = Bookmark(etsy_listing_id=listing.etsy_listing_id, user_id=session["user_id"]) db.session.add(bookmark) db.session.commit() return redirect('/bookmarks') #what does the viewfunction actually return here
def create_bookmark(user_id): """Adds a bookmark to the user's bookmarks.""" session_id = session_check(session['id'], request.remote_addr, request.user_agent, int(user_id)) if session_id == '': session['id'] = '' abort(401) # Unauthorized else: session['id'] = session_id post_id = int(request.form.get('post_id')) if not post_id: abort(400) # Bad request elif Post.query.filter(Post.id == post_id).one().erased: abort(403, 'Cannot bookmark erased post.') elif Bookmark.query.filter(Bookmark.post_id == post_id, Bookmark.user_id == user_id).first(): abort(403, 'Post already bookmarked.') else: new_bookmark = Bookmark(user_id=user_id, post_id=post_id) db.session.add(new_bookmark) db.session.commit() return ('', 204) # status 204: success, no content
def post(self): self.url = self.validate_url(self.get_argument('url', '')) # 错误的url if not self.url: self.redirect('/main') return self.bookmark = Bookmark.get_by_url(self.url) # 未收藏 if not self.bookmark: self.redirect('/bookmark/new/' + self.url) return self.title = self.get_argument('title', '') or self.url self.desc = self.get_argument('desc', '') self.tags = self.get_argument('tags', '').split() self.bookmark.update_by_dict({ 'tags': self.tags, 'title': self.title, 'desc': self.desc }) self.redirect('/bookmark/get/' + self.url)
def delete_bookmark(uid, article_id): id = Bookmark.id_for_bookmark(uid, article_id) bookmark = ndb.Key(Bookmark, id).get() if bookmark: bookmark.deleted = True bookmark.last_modified = datetime.datetime.now() bookmark.put()
def create_bookmark(user_id, resource_id): """Create and commit it""" new_bookmark = Bookmark(user_id=user_id, resource_id=resource_id) db.session.add(new_bookmark) db.session.commit() return new_bookmark
def add_bookmark(user_id, recipe_id): """Adds recipe to Bookmarks table. Returns instantiated Bookmark object.""" new_bookmark = Bookmark(user_id=user_id, recipe_id=recipe_id) db.session.add(new_bookmark) db.session.commit() return new_bookmark
def get(self, url): self.url = self.validate_url(url) if self.url: self.bookmark = Bookmark.get_by_url(self.url) if self.bookmark: self.bookmark.delete_bookmark() # 错误的url / 未收藏 / 成功删除 self.redirect('/main')
def load_fake_bookmarks(): """See fake bookmarks into popnoms db. Do this after seeding database and before running app.""" timestamp = datetime.now() for x in range(1, 15): bookmark = Bookmark(event_id="62010310508", user_id=x, bookmark_type="going", timestamp=timestamp) db.session.add(bookmark) for y in range(16, 45): bookmark = Bookmark(event_id="62010310508", user_id=y, bookmark_type="interested", timestamp=timestamp) db.session.add(bookmark) db.session.commit()
def bookmarks(uid, since=None): q = Bookmark.query(Bookmark.uid == uid).order(-Bookmark.last_modified) if since: q = q.filter(Bookmark.last_modified >= util.datetime_from_timestamp(since)) bookmarks = q.fetch(200) articles = ndb.get_multi([b.article for b in bookmarks]) def to_json(bookmark, article): j = bookmark.json() j['article'] = article.json() if article else None return j return { "bookmarks": [to_json(bookmark, article) for bookmark, article in zip(bookmarks, articles)], "since": util.datetime_to_timestamp(datetime.datetime.now()), "partial": since is not None }
def get(self, url): self.url = self.validate_url(url) # 错误的url if not url: self.redirect('/main') return self.bookmark = Bookmark.get_by_url(self.url) # 未收藏 if not self.bookmark: self.redirect('/bookmark/new/' + self.url) return self.render('get.html', title='get', contents=self.bookmark.to_dict())
def get(self, page): # 获取bookmark self.page = int(page or 1) self.bookmarks, self.page_count = Bookmark.get_by_page(self.page) if self.page > self.page_count: # url要求的页数超出了总页数 self.redirect('/main/' + str(self.page_count)) return # 获取tag self.tags = Tag.get_all_tags() self.render('main.html', title=self.page_count, contents={ 'bookmarks': self.bookmarks, 'tags': self.tags})
def get(self, tag_names): # 从output?tags=a,跳转到output/a self.filter_bookmark = self.get_argument('filter', '') if self.filter_bookmark: self.tags = '+'.join(self.get_arguments('tags')) self.redirect('/dashboard/output/' + self.tags) return # 如果有传入tag,针对tag过滤输出 self.tags = tag_names.replace('+', ' ').split() if tag_names else [] self.bookmarks = Bookmark.output_bookmarks(self.tags) from datetime import datetime output_time = datetime.now() self.render('netscape_bookmark_file.html', time=output_time, contents=self.bookmarks)
def add_bookmark_to_db(status, name, event_id, user_id): """Adds bookmark to DB or updates bookmark type if bookmark already exists""" bookmark_success = "Successfully bookmarked {} as {}".format(name, status) bookmark_failure = "You must be logged in to bookmark and event." # Removes non_ascii charecters from event names name = remove_non_ascii(name) # If the user is logged in if user_id: # Get BookmarkType object out of DB based on status bookmark_type_object = db.session.query(BookmarkType).filter_by( bookmark_type=status).one() # Get the bookmark_type_id out of the db, 1 for going, 2 for interested: gives us an int bookmark_type_id = bookmark_type_object.bookmark_type_id # Before make bookmark, check whether it exists; if it doesn't, create it, if it does, update it w bookmark type # We use .first() instead of .one() because with .one() if there are none, we get error # With .first() none gets returned if there isn't a bookmark bookmark_for_event = db.session.query( Bookmark).filter((Bookmark.event_id == event_id) & (Bookmark.user_id == user_id)).first() # If bookmark for event doesn't already exist, we want to create the bookmark if bookmark_for_event == None: # Make a new Bookmark, passing it the user_id, event_id, and bookmarktype object, add & commit bookmark = Bookmark(user_id=user_id, event_id=event_id, bookmark_type_id=bookmark_type_id) print "BOOKMARK", bookmark db.session.add(bookmark) db.session.commit() # Return success message return bookmark_success # If the bookmark for event already exist, we want to update the bookmark type else: bookmark_for_event.bookmark_type_id = bookmark_type_id db.session.commit() return bookmark_success # If the user is not logged in, return please login message else: return bookmark_failure
def add_bookmark(): """user can bookmark a recipe""" content = request.get_json() api_recipe_id = content.get('api_recipe_id') user_id = session['current_user'] # ingredients = (request.args.get('ingredients')) bookmark = Bookmark( user_id=user_id, api_recipe_id=api_recipe_id, ) db.session.add(bookmark) db.session.commit() print('adding a bookmark') return jsonify()
def get(self, tag_names, page): # 获取页数 self.page = int(page or 1) # 获取tag列表 self.tags = tag_names.replace('+', ' ').split() self.bookmarks, self.page_count = \ Bookmark.get_by_tags(self.tags, self.page) if self.page > self.page_count: # url要求的页数超出了总页数 self.redirect('/tag/{0}/{1}'.format( '+'.join(self.tags), self.page_count)) return self.tags = Tag.get_all_tags() self.render('filter.html', title=tag_names, contents={ 'bookmarks': self.bookmarks, 'tags': self.tags})
def add_bookmark_to_db(status, event_id, user_id): """Adds bookmarked event to database.""" bookmark_success = f"Successfully bookmarked as {status}." bookmark_failure = "You must be logged in to bookmark and event." timestamp = datetime.now() # If the user is logged in if user_id: # Make a new Bookmark, passing it the user_id, event_id, and bookmarktype object, add & commit bookmark = Bookmark(user_id=user_id, event_id=event_id, bookmark_type=status, timestamp=timestamp) db.session.add(bookmark) db.session.commit() # Return success message return bookmark_success # If the bookmark for event already exist, we want to update the bookmark type else: return bookmark_failure
def get(self): self.url = self.validate_url(self.get_argument('url', '')) # /get if not self.url: self.render('new.html', title='new', contents=None) return # /get/XXXX # 已收藏 if Bookmark.get_by_url(self.url): self.redirect('/bookmark/get/' + self.url) return self.title = self.get_title(self.url) self.render('new.html', title='new', contents={ 'url': self.url, 'title': self.title})
def add_or_update_bookmark(uid, reading_pos, article_id=None, article_url=None): # provide EITHER article_id or article_url if not (article_id or article_url): logging.error("Attempt to update bookmark without article_id or article_url") return None if not article_id and article_url: article = ensure_article_at_url(article_url) if article: article_id = article.key.id() else: logging.error("Tried to get article {0} for bookmarking, but failed".format(article_url)) return None id = Bookmark.id_for_bookmark(uid, article_id) bookmark, inserted = get_or_insert(Bookmark, id) bookmark.article = ndb.Key(Article, article_id) if reading_pos: bookmark.reading_position = reading_pos bookmark.last_modified = datetime.datetime.now() bookmark.uid = uid bookmark.deleted = False bookmark.put() return bookmark
def db_test_data(): """Create sample data for test database.""" # Add sample users u1 = User(uname='lemongrab') u2 = User(uname='bubblegum') u3 = User(uname='marceline') u4 = User(uname='simon') # Add sample posts p1 = Post(title="One million years dungeon", content="One million years dungeon", user=u1, created=datetime.utcnow()) p2 = Post(title="Poor Lemongrab", content="You try your best", user=u2, references=[p1], created=datetime.utcnow()) p3 = Post(title="Candy Kingdom", content="It's my favorite kingdom!", user=u2, created=datetime.utcnow()) db.session.add_all([u1, u2, u3, u4, p1, p2, p3]) db.session.commit() # Add bookmarks b1 = Bookmark(user=u3, post_id=p1.id) # Add followers f1 = Follower(user_id=u1.id, follower_id=u2.id) f2 = Follower(user_id=u2.id, follower_id=u3.id) f3 = Follower(user_id=u2.id, follower_id=u4.id) db.session.add_all([b1, f1, f2, f3]) db.session.commit()
def list(self): # from model import Bookmark b=Bookmark.select() return dict(bookmark=b,now=datetime.datetime.now())