def like_movie(userid='', movie_code=''): if not userservice.check_user_exist(userid=userid): return {}, 0, '用户不存在' if not check_movie_exist(movie_code=movie_code): return {}, 0, '影片不存在' movie = Avmoo.objects(code=movie_code)[0] like = Like.objects(userid=userid, movie_code=movie_code) if like.__len__() > 0: like = like[0] like.valid = 1 like.update_time = datetime.now() else: like = Like(userid=userid, movie_code=movie.code, movieid=str(movie.id), valid=1, update_time=datetime.now()) like.save() if movie.like_count is None: movie.like_count = 1 else: movie.like_count += 1 movie.save() return {}, 1, ''
def post(self, post_id): key = db.Key.from_path('Post', int(post_id), parent=blog_key()) post = db.get(key) if not post: self.error(404) return """ While commenting, a comment datastore object is created and stored, with respect to each user and post. """ c = "" if (self.user): # On clicking like, post-like value increases. if (self.request.get('like') and self.request.get('like') == "update"): likes = db.GqlQuery("select * from Like where post_id= " + post_id + " and user_id= " + str(self.user.key().id())) if self.user.key().id() == post.user_id: self.redirect("/blog/" + post_id + "?error= You cannot like your own post") return elif likes.count() == 0: l = Like(parent=blog_key(), user_id=self.user.key().id(), post_id=int(post_id)) l.put() # On commenting, it creates new comment tuple if (self.request.get('comment')): c = Comment(parent=blog_key(), user_id=self.user.key().id(), post_id=int(post_id), comment=self.request.get('comment')) c.put() else: self.redirect("/blog/" + post_id + "?error= Need to Login Please.") return comments = db.GqlQuery("select * from Comment where post_id = " + post_id + "order by created desc") likes = db.GqlQuery("select * from Like where post_id=" + post_id) self.render("permalink.html", post=post, comments=comments, no_of_likes=likes.count(), new=c)
def create_likes(user_id, business_id, liking): """liking a business""" # check if busid and userid exist likes = Like.query.filter(Like.user_id == user_id, Like.business_id == business_id).first() if likes is None: likes = Like(user_id=user_id, business_id=business_id, liking=liking) db.session.add(likes) else: likes.liking = liking db.session.commit() return likes
def toggle_like_state_ajax(): """Toggle the Like state on an image via ajax""" ajax = request.get_json() image_id = ajax.get('imageId') if image_id is None: return jsonify({'message': 'no image part'}) user_id = session.get('userId') if user_id is None: return jsonify({'message': 'no user part'}) image = Image.query.get(image_id) if image is None: return jsonify({'message': 'image not found'}) user = User.query.get(user_id) if user is None: return jsonify({'message': 'user not found'}) like = Like.toggle(user, image) like_count = len(Like.query.filter_by(image=image).all()) result = { 'like': { 'isLiked': like is not None, 'likeCount': like_count, }, } # pprint(result) return jsonify(result)
def post(self): """increment post liked number by one""" pid = self.request.get("pid") if self.with_no_user() or self.user_match_post_author(pid): self.redirect("/") return pid = long(pid) post = Post.by_id(pid) post.liked += 1 post.put() pid = long(pid) like = Like(uid=self.user.key().id(), pid=pid) like.put() self.redirect("/post/%s" % pid)
def get_user_like(userid='', page=1): likes = Like.objects(userid=userid, valid=1).order_by('-update_time') if likes.__len__() == 0: return {}, 1, '' per_page = 26 like_codes = [i['movie_code'] for i in likes] movies = Avmoo.objects(code__in=like_codes)[(page-1) * per_page:(page * per_page)-1] return movies.to_mongo(), 1, ''
def create_like(user_id, note_id): """Create a like for a note.""" like = Like(user_id=user_id, note_id=note_id) db.session.add(like) db.session.commit() return like
def create_like(user, res, group_name=None): """ Create a like """ like = Like(user=user, res=res, group_name=group_name) db.session.add(like) db.session.commit() return like
def get_like_status(userid='', movie_code=''): like = Like.objects(userid=userid, movie_code=movie_code, valid=1) if like.__len__() > 0: return { 'status': 1 }, 1, '' else: return { 'status': 0 }, 1, ''
def post(self): date_time = self.request.get('date_time') post_key = Post.query(Post.date_time == date_time).fetch()[0].key post = post_key.get() query = Like.query(Like.post_key == post.key).fetch() like_author = None author_key = Key('User', self.session.get('user_key_id')) if len(query) > 0: like_author = query[0].author_key # if not (User.query(User.email == user.email).fetch()): if not (like_author == author_key): score = self.request.get('score') post.score = int(score) + 1 Like(author_key = author_key, post_key = post_key).put() post.put() time.sleep(0.1) self.redirect('/index')
def delete_like(post_id, like_id): user = SESSION.get(request.headers.get('Authorization')) if user is None: abort(400, {'message': 'TOKEN_NOT_FOUND'}) from model import Like like_exist = Like.find(like_id) if like_exist is None: abort(400, {'message': 'NOT_YET_LIKED'}) like_exist.delete() return jsonify("Deleted")
def test_like_creation(self): print '- test_like_creation' user = User.query.get(1) image = Image.query.get(2) like = Like.create(user, image) self.assertEqual(like.user_id, 1) self.assertIsInstance(like.user, User) self.assertEqual(like.image_id, 2) self.assertIsInstance(like.image, Image) print '+ passed'
def get_user_like(userid='', page=1): likes = Like.objects(userid=userid, valid=1).order_by('-update_time') if likes.__len__() == 0: return {}, 1, '' per_page = 26 like_codes = [i['movie_code'] for i in likes] movies = Avmoo.objects(code__in=like_codes)[(page - 1) * per_page:(page * per_page) - 1] return movies.to_mongo(), 1, ''
def like_book(book_id): if not get_book_by_id(book_id): return current_user_id = get_current_user_id() if not current_user_id: return if get_like(book_id, current_user_id): return like = Like(book_id=book_id, user_id=current_user_id) session.add(like) session.commit() return like
def like(): pid = int(request.args.get("post")) print(pid) post = Post.query.filter(Post.pid == pid).first() if not post: return error("That post does not exist!") likes = Like.query.filter(Like.pid == pid).first() if (likes and likes.uid == current_user.uid): error("You already like it!") else: like = Like(uid=current_user.uid, pid=pid, post_time=datetime.utcnow()) current_user.likes.append(like) post.likes.append(like) db.session.commit() return redirect("/viewpost?post=" + str(pid))
def add_sample_likes(num_workouts, num_users): """User1 is liking every fifth workout that is in the db.""" for j in range(1, num_users * num_workouts + 1): if j % 5 == 0: new_like = Like( workout_id=j, user_id=1, ) if (j % 15 == 0): print new_like db.session.add(new_like) db.session.commit()
def dislike_movie(userid='', movie_code=''): if not userservice.check_user_exist(userid=userid): return {}, 0, '用户不存在' if not check_movie_exist(movie_code=movie_code): return {}, 0, '影片不存在' like = Like.objects(userid=userid, movie_code=movie_code) if like.__len__() == 0: return {}, 0, '未收藏该影片' movie = Avmoo.objects(code=movie_code)[0] movie.like_count -= 1 movie.save() like = like[0] like.valid = 0 like.save() return {}, 1, ''
def get(self, pid): """render post page including post content comments and liked number""" pid = long(pid) post = Post.by_id(pid) if post: author = User.by_id(post.uid) comments = Comment.by_pid(pid) like = None if not self.with_no_user(): like = Like.by_uid_and_pid(self.user.key().id(), pid) self.render("post.html", post=post, author=author, comments=comments, like=like) else: self.render("404.html")
def like(): """ liking user and saving into database """ user_id = session["user_id"] #current user id liked_user_id = int(request.form.get('likedUserId')) #liked user id # query Like table to see if this user has already liked the person liked_in_like = Like.query.filter_by(likes_user_id=user_id, liked_user_id=liked_user_id).first() if not liked_in_like: # adding liked user to like table add_liked_user = Like(likes_user_id=user_id, liked_user_id=liked_user_id) db.session.add(add_liked_user) # check liked user exist in dislike table DB liked_in_dislike = Dislike.query.filter_by( dislikes_user_id=user_id, disliked_user_id=liked_user_id).first() if liked_in_dislike: # delete record from dislike table db.session.delete(liked_in_dislike) db.session.commit() liked_back = Like.query.filter_by(likes_user_id=liked_user_id, liked_user_id=user_id).first() if liked_back: liked_back = True else: liked_back = False # Query User table by liked_user_id to get phone number phone = db.session.query( User.contact_no).filter_by(user_id=liked_user_id).first() return jsonify({ 'status': 'ok', 'liked_back': liked_back, 'phone_num': phone })
def create_like(post_id): user = SESSION.get(request.headers.get('Authorization')) if user is None: abort(400, {'message': 'TOKEN_NOT_FOUND'}) # input_data = request.get_json() from model import Like, Post post_exist = Post.find(post_id) if post_exist is None: abort(400, {'message','POST_NOT_FOUND'}) like = Like() like.post_id = post_id like.user_id = user.id like.save() return "Liked."
def selected_categories(): """Get all selected check boxes and add it to database, Like.""" user = User.query.get(session['user_id']) submitted_categories = request.form.getlist('cat_id') submitted_image = request.form.get('image_id') if submitted_categories: for ident in submitted_categories: like = Like(user_id=user.user_id, cat_id=ident) db.session.add(like) db.session.commit() if submitted_image: user_image = UserImage(user_id=user.user_id, image_id=submitted_image) db.session.add(user_image) db.session.commit() return redirect('/munchbuddies')
def like(): """Allow user to like a doctor""" # doctor0 = db.session.query(Doctor).filter(Doctor.doctor_id==session['info_doc']['p_id']).first() doctor0 = Doctor.query.get(session['info_doc']['p_id']) # if doctor not already in Doctor table, we add it: if doctor0 is None: doctor1 = Doctor(doctor_id=session['info_doc']['p_id'], first_name=session['info_doc']['first_name'], last_name=session['info_doc']['last_name'], specialty=session['info_doc']['short_specialty'], yelp_rating=session['info_doc']['rating']) db.session.add(doctor1) # add the like record in the record table: like1 = Like(doctor_id=session['info_doc']['p_id'], user_id=session['user_id']) db.session.add(like1) db.session.commit() nb_likes = Like.query.filter_by( doctor_id=session['info_doc']['p_id']).count() session['likes'] = nb_likes return jsonify({'like': nb_likes})
def add_dislike(post_id): """When the user "dislikes" a post, it adds it to the dbase & updates page with new like info (Tested)""" #Queries from all of the dbase tables that need to be updated and/or rendered user_id = User.query.filter_by(email=session['current_user']).one().user_id forum_id = Post.query.filter_by(post_id=post_id).one().forum_id like_query = Like.query.filter(Like.post_id == post_id, Like.user_id == user_id).all() post_query = db.session.query(Post).filter_by(post_id=post_id).one() #If the user hasn't already liked/disliked the post, it adds the dislike to the dbase if like_query == []: new_dislike = Like(user_id=user_id, post_id=post_id, like_dislike="dislike") (db.session.query(Post).filter_by(post_id=post_id).update( {"dislike_num": (post_query.dislike_num + 1)})) db.session.add(new_dislike) db.session.commit() #If the user previously liked the comment, it updates it to a dislike else: (db.session.query(Like).filter(Like.post_id == post_id, Like.user_id == user_id).update({ "user_id": user_id, "post_id": post_id, "like_dislike": "dislike" })) (db.session.query(Post).filter_by(post_id=post_id).update({ "dislike_num": (post_query.dislike_num + 1), "like_num": (post_query.like_num - 1) })) db.session.commit() #Re-renders the forum page with the updated dislike info return redirect("/forums/order_by_date/{}/1".format(forum_id))
def add_user_like(user_id, ttxid): new_user_like = Like(user_id=user_id, ttxid=ttxid) db.session.add(new_user_like) db.session.commit() print(str(user_id) + " " + ttxid + "SUCCESS")
def create_like(user, post): like = Like(user=user, post=post) db.session.add(like) db.session.commit() return like
def seed_data(testing=False): tf_model = TFModel.create(title='fast_style_transfer', description='Created by Logan Engstrom') style_dir = BASEDIR + '/fast_style_transfer/styles/' muse_file = FileStorage(stream=open(style_dir + 'muse.ckpt')) muse_image = FileStorage(stream=open(style_dir + 'muse.jpg')) Style.create(style_file=muse_file, image_file=muse_image, tf_model=tf_model, title='La Muse', artist='Pablo Picasso', description="This painting is also known as Young Woman Drawing and Two Women. It's a story of two women. One sits, copying what she sees in the mirror set up in front of her; the other sleeps with her head in her arms.\nThere was a series of paintings Picasso did at this time of young women drawing, writing or reading. This is Marie Therese Walther not with the rounded, ample forms the painter normally used to depict her but with an angular style. The sleeping girl resembles her as well, and indeed she would be somewhere (anywhere's better than nowhere) in Picasso's affections for some years to come. Maia, their daughter was born a few months after this was painted, in October 1935.") rain_file = FileStorage(stream=open(style_dir + 'rain.ckpt')) rain_image = FileStorage(stream=open(style_dir + 'rain.jpg')) Style.create(style_file=rain_file, image_file=rain_image, tf_model=tf_model, title='Rain Princess', artist='Leonid Afremov', description="Rain Princess is a painting by Leonid Afremov which was uploaded on February 17th, 2014.\nYou can buy this painting from his official shop via this link: https://www.etsy.com/listing/16654120") scream_file = FileStorage(stream=open(style_dir + 'scream.ckpt')) scream_image = FileStorage(stream=open(style_dir + 'scream.jpg')) Style.create(style_file=scream_file, image_file=scream_image, tf_model=tf_model, title='The Scream', artist='Edvard Munch', description="Munch's The Scream is an icon of modern art, the Mona Lisa for our time. As Leonardo da Vinci evoked a Renaissance ideal of serenity and self-control, Munch defined how we see our own age - wracked with anxiety and uncertainty.") udnie_file = FileStorage(stream=open(style_dir + 'udnie.ckpt')) udnie_image = FileStorage(stream=open(style_dir + 'udnie.jpg')) Style.create(style_file=udnie_file, image_file=udnie_image, tf_model=tf_model, title='Udnie', artist='Francis Picabia', description="Udnie was inspired by a dance performance given by the Polish-born actress Stacia Napierkowska on the boat taking Francis Picabia to New York for the Armory Show in 1913. The work combines the decomposition of volumes into planes characteristic of Cubism with the enthusiasm for a world in movement of Italian Futurism. The energy and vitality of the dance find expression in springing arabesque, fragmented coloured planes and the jostling of simplified forms. In the radicalism of its treatment, Udnie marks a decisive step towards the emergence of abstraction in Europe.") wave_file = FileStorage(stream=open(style_dir + 'wave.ckpt')) wave_image = FileStorage(stream=open(style_dir + 'wave.jpg')) Style.create(style_file=wave_file, image_file=wave_image, tf_model=tf_model, title='Under the Wave off Kanagawa', artist='Katsushika Hokusai', description="Katsushika Hokusai's Under the Wave off Kanagawa, also called The Great Wave has became one of the most famous works of art in the world-and debatably the most iconic work of Japanese art. Initially, thousands of copies of this print were quickly produced and sold cheaply. Despite the fact that it was created at a time when Japanese trade was heavily restricted, Hokusai's print displays the influence of Dutch art, and proved to be inspirational for many artists working in Europe later in the nineteenth century.") wreck_file = FileStorage(stream=open(style_dir + 'wreck.ckpt')) wreck_image = FileStorage(stream=open(style_dir + 'wreck.jpg')) Style.create(style_file=wreck_file, image_file=wreck_image, tf_model=tf_model, title='The Shipwreck of the Minotaur', artist='Joseph Mallord William Turner', description="Shipwreck may be regarded as one of the worst things a human being can encounter. The sea is no respecter of persons- instantly, 100s of men can be wiped out. Turner's fascination with man vs. nature is display in The Shipwreck. He wished to portray the power of the elements and how no one is immune from the dangers of an angry sea; he can struggle and fight but ultimately he will be swallowed up by the sea. The unlikelihood of deliverance from such calamity is great.") user = User.create(username='******', email='*****@*****.**', password='******') user_image = FileStorage(stream=open( 'fast_style_transfer/source-images/melons.jpg')) SourceImage.create(image_file=user_image, user=user, title="Melon - Eden's Gem (Rocky Ford Honeydew)", description="Popular green-flesh muskmelon with a heavily netted rind, and smooth, sweet-flavoured flesh with a complex spicy flavour. This early maturing variety, also known as the Rocky Ford cantaloupe, was developed in 1905 at Rocky Ford, Colorado as a 'crate melon.' Softball-sized 500-g (1-lb) fruit.\nhttps://www.urbanseedling.com/product/melon-edens-gem/") if testing: image = Image.query.get(1) source = SourceImage.query.get(1) style = Style.query.get(1) StyledImage.create(source_image=source, style=style, testing=testing) comment = Comment(user=user, image=image, body="hello world") like = Like(user=user, image=image) tag = Tag(name='melon') image_tag = ImageTag(tag=tag, image=image) db.session.add_all([comment, like, tag, image_tag]) else: # create a placeholder image so testing does not # write over an actual user's file i = Image(file_extension='') j = Image(file_extension='') db.session.add_all([i, j]) db.session.commit()
def get_like_status(userid='', movie_code=''): like = Like.objects(userid=userid, movie_code=movie_code, valid=1) if like.__len__() > 0: return {'status': 1}, 1, '' else: return {'status': 0}, 1, ''
def load_users(): """Create example data for the test database.""" Like.query.delete() Dislike.query.delete() User_Hobbies.query.delete() Image.query.delete() User.query.delete() Hobbie.query.delete() deepika = User( fname="Deepika", lname="Padukone", email="*****@*****.**", password= "******", age=32, gender="F", interested_in="M", city="Santa Clara", state="CA", contact_no="9421853667", occupation="Actress") shahid = User( fname="Shahid ", lname="Kapoor", email="*****@*****.**", password= "******", age=38, gender="M", interested_in="F", city="Portland", state="OR", contact_no="4084544445", occupation="Fashion Designer") akshay = User( fname="Akshay ", lname="Kumar", email="*****@*****.**", password= "******", age=42, gender="M", interested_in="F", city="Los Angeles", state="CA", contact_no="9997188913", occupation="Actor") ranvir = User( fname="Ranvir ", lname="Singh", email="*****@*****.**", password= "******", age=30, gender="M", interested_in="F", city="San Francisco", state="CA", contact_no="4082222679", occupation="Actor") rani = User( fname="Rani ", lname="Mukherjee", email="*****@*****.**", password= "******", age=35, gender="F", interested_in="M", city="Atlanta", state="GA", contact_no="9427158913", occupation="Engineer") hrithik = User( fname="Hrithik ", lname="Roshan", email="*****@*****.**", password= "******", age=40, gender="M", interested_in="F", city="Palo Alto", state="CA", contact_no="9227155913", occupation="Actor") ranbir = User( fname="Ranbir ", lname="Kapoor", email="*****@*****.**", password= "******", age=18, gender="M", interested_in="F", city="Palo Alto", state="CA", contact_no="9223559199", occupation="Actor") shahrukh = User( fname="Shahrukh ", lname="Khan", email="*****@*****.**", password= "******", age=18, gender="M", interested_in="F", city="Palo Alto", state="CA", contact_no="9223559145", occupation="Actor") aish = User( fname="Aishwarya ", lname="Rai", email="*****@*****.**", password= "******", age=35, gender="F", interested_in="M", city="Atlanta", state="GA", contact_no="9427158911", occupation="Actress") sonam = User( fname="Sonam ", lname="Kapoor", email="*****@*****.**", password= "******", age=35, gender="F", interested_in="M", city="Atlanta", state="GA", contact_no="9427158914", occupation="Designer") katrina = User( fname="Katrina ", lname="Kaif", email="*****@*****.**", password= "******", age=30, gender="F", interested_in="M", city="Destin", state="FL", contact_no="9421366790", occupation="Actress") db.session.add_all([ deepika, shahid, akshay, ranvir, rani, hrithik, ranbir, shahrukh, aish, sonam, katrina ]) db.session.commit() hobbie1 = Hobbie(hobbie_name="Travel") hobbie2 = Hobbie(hobbie_name="Yoga") hobbie3 = Hobbie(hobbie_name="Reading") hobbie4 = Hobbie(hobbie_name="Fishing") hobbie5 = Hobbie(hobbie_name="Cricket") hobbie6 = Hobbie(hobbie_name="Swimming") hobbie7 = Hobbie(hobbie_name="Gardening") db.session.add_all( [hobbie1, hobbie2, hobbie3, hobbie4, hobbie5, hobbie6, hobbie7]) db.session.commit() deepika.hobbies.extend([hobbie4, hobbie2, hobbie3, hobbie7]) #extends stores shahid.hobbies.append(hobbie3) akshay.hobbies.append(hobbie4) ranvir.hobbies.extend([hobbie4, hobbie6]) rani.hobbies.append(hobbie3) hrithik.hobbies.extend([hobbie7, hobbie1, hobbie4]) ranbir.hobbies.extend([hobbie1, hobbie4, hobbie7]) shahrukh.hobbies.extend( [hobbie1, hobbie2, hobbie3, hobbie5, hobbie6, hobbie7]) aish.hobbies.extend([hobbie1, hobbie7]) sonam.hobbies.append(hobbie5) katrina.hobbies.append(hobbie5) db.session.commit() # deepika.likes.append(hrithik) like1 = Like(likes_user=deepika, liked_user=hrithik) like2 = Like(likes_user=hrithik, liked_user=deepika) like3 = Like(likes_user=akshay, liked_user=rani) like4 = Like(likes_user=ranvir, liked_user=deepika) like5 = Like(likes_user=ranbir, liked_user=aish) like6 = Like(likes_user=sonam, liked_user=hrithik) db.session.add_all([like1, like2, like3, like4, like5, like6]) db.session.commit() dlike1 = Dislike(dislikes_user=deepika, disliked_user=katrina) dlike2 = Dislike(dislikes_user=akshay, disliked_user=katrina) dlike3 = Dislike(dislikes_user=rani, disliked_user=katrina) dlike4 = Dislike(dislikes_user=akshay, disliked_user=sonam) dlike5 = Dislike(dislikes_user=ranvir, disliked_user=rani) dlike6 = Dislike(dislikes_user=shahid, disliked_user=rani) db.session.add_all([dlike1, dlike2, dlike3, dlike4, dlike5, dlike6]) db.session.commit() img1 = Image(user=deepika, filename="1_dp.png") img2 = Image(user=shahid, filename="2_sh.png") img3 = Image(user=akshay, filename="3_ak.png") img4 = Image(user=ranvir, filename="4_rv.png") img5 = Image(user=rani, filename="5_rn.png") img6 = Image(user=hrithik, filename="6_hr.png") img7 = Image(user=ranbir, filename="7_rk.png") img8 = Image(user=shahrukh, filename="7_sk.png") img9 = Image(user=aish, filename="8_as.png") img10 = Image(user=sonam, filename="9_snk.png") db.session.add_all( [img1, img2, img3, img4, img5, img6, img7, img8, img9, img10]) db.session.commit() #making profile pic saparate deepika.profile = img1 shahid.profile = img2 akshay.profile = img3 ranvir.profile = img4 rani.profile = img5 hrithik.profile = img6 ranbir.profile = img7 shahrukh.profile = img8 aish.profile = img9 sonam.profile = img10 db.session.commit()