Exemplo n.º 1
0
    def post(self):

        tool_user, db_user = ADayThere.tool_user()
        if not tool_user:
            self.response.status = 401
            return

        data = json.loads(self.request.body)

        day = Day.query_user_title(db_user.user_id, data["title"]).get()

        KeywordsDayList.delete_keywords(day)

        day.full_locality = data["full_locality"]
        day.title = data["title"]
        day.description = data["description"]

        if isinstance(data["keywords"], basestring):
            if "," in data["keywords"]:
                day.keywords = data["keywords"].split(",")
            else:
                day.keywords = data["keywords"].split(" ")
        else:
            day.keywords = data["keywords"]

        for keyword in day.keywords:
            Keywords.add_if_missing(keyword)

        day.places = []
        for place in data["places"]:
            p = Place()
            p.name = place["name"]
            p.comment = place["comment"]
            p.location = Location()
            p.location.latitude = str(place["location"]["latitude"])
            p.location.longitude = str(place["location"]["longitude"])
            p.location.vicinity = place["vicinity"]
            day.places.append(p)

        day.photos = []
        for photo in data["photos"]:
            day_photo = DayPhoto()
            day_photo.title = photo["title"]
            day_photo.description = photo["description"]

            day.photos.append(day_photo)

            photo_query = Photos.query_photo(db_user.user_id, photo["title"])
            pq = photo_query.get()
            cnt = pq.used_by.count(day.title)
            if cnt == 0:
                pq.used_by.append(day.title)

        day.put()

        KeywordsDayList.add_keywords(day)

        Words.update_words(day.title, day.description, day.key, day.full_locality)

        self.response.status = 200
Exemplo n.º 2
0
    def delete(self):

        tool_user, db_user = ADayThere.tool_user()
        if not tool_user:
            self.response.status = 401
            return

        title = self.request.get("title")
        day = Day.query_user_title(db_user.user_id, title).get()

        for photo in day.photos:
            photo_query = Photos.query_photo(db_user.user_id, photo.title)
            pq = photo_query.get()
            try:
                index = pq.index(day.title)
                day.title.pop(index)
            except:
                pass

            pq.key.delete()

        day.key.delete()

        KeywordsDayList.delete_keywords(day)

        Words.delete_words(day.key)

        self.response.status = 200
Exemplo n.º 3
0
    def put (self):
        logged_in_user, commenters_id = ADayThere.tool_user ()
       
        if not logged_in_user:
            self.response.status = 401
            return

        data = json.loads (self.request.body)

        print "DATA", data

        userid = data.get ('userid', None)
        title = data.get ('title', None)
        review = data.get ('rating', 1)

        if title is None or userid is None:
            self.response.status = 401
            return
            

        
        prev_comment = UserComment.query_previous_comment (commenters_id.name, userid, title).get ()

        if prev_comment is not None:
            self.response.status = 409
            self.response.write (json.dumps ({'rating': prev_comment.rating, 'title': prev_comment.title}))
            return

        day = Day.query_user_title (userid, title).get ()

        if day is None:
            self.response.status = 403
            return


        new_comment = UserComment ()
        new_comment.commenters_name = commenters_id.name
        new_comment.userid = userid
        new_comment.title = title
        new_comment.text = data.get ('text', None)
        new_comment.rating = review

        new_comment.put ()
        
        if day.numberOfReviews == None or day.numberOfReviews == 0:
            day.numberOfReviews = 1;
            day.averageReview = review;
        else:
            rev = (day.numberOfReviews * day.averageReview) + review
            day.numberOfReviews = day.numberOfReviews + 1
            day.averageReview = rev / day.numberOfReviews

        day.put ()

        self.response.write (json.dumps ({'numberOfReviews': day.numberOfReviews, 'averageReview': day.averageReview, 'review': new_comment.to_dict ()}));

        self.response.status = 200
Exemplo n.º 4
0
    def post (self):
        
        tool_user, db_user = ADayThere.tool_user ()

        if not ADayThere.admin_user (db_user):
            self.response.status = 401 
            self.response.write ("Unauthorized")
            return

        sent_day = json.loads (self.request.body)
    
        day_query = Day.query_user_title (sent_day['userid'], sent_day['title'])
        day = day_query.get ()
        if day is None:
            self.response.status = 404
            self.response.write ("Not Found")
            return

        day.key.delete ()

        self.response.status = 200
        self.response.write ("OK")
Exemplo n.º 5
0
    def get(self):

        rand = self.request.get("random", None)
        if rand is not None:
            days = Day.query_random(20)
            self.return_days(days, None, False)
            return

        userid = self.request.get("user_id", None)
        if userid is not None:
            title = self.request.get("title", None)

            if title is not None:
                day = Day.query_user_title(userid, title).get()
                days = []
                days.append(day)
            else:
                days = Day.query_user(userid).fetch()

            self.return_days(days, None, False)
            return

        wordstr = self.request.get("words", None)
        words = None

        if wordstr is not None:
            if wordstr.find(",") != -1:
                words = wordstr.split(",")
            else:
                words = wordstr.split(" ")

        args = {}

        args["limit"] = self.request.get("limit", 20)

        if self.request.get("full_locality", None) is not None:
            args["full_locality"] = self.request.get("full_locality")
        if self.request.get("cursor", None) is not None:
            args["cursor"] = Cursor(urlsafe=self.request.get("cursor"))
        if self.request.get("user_id", None) is not None:
            args["user_id"] = self.request.get("user_id")
        if self.request.get("minimum_rating", None) is not None:
            args["minimum_rating"] = int(self.request.get("minimum_rating"))
        if self.request.get("all_words", None) is not None:
            args["all_words"] = self.request.get("all_words")

        keywords = self.request.get("keywords", None)

        keyhash = None
        if keywords is not None:
            word_list = sorted(map(lambda x: x.strip(), keywords.split(",")))
            keyhash = hashlib.sha256(str(word_list)).hexdigest()

        if keyhash is None:
            if words is None:
                days, cursor, more = Day.query_days(args)
            else:
                days, cursor, more = Day.query_word_days(args, words)
        else:
            days, cursor, more = Day.query_keyword_days(keyhash, args, words)

        if cursor is not None:
            cursor = cursor.urlsafe()

        self.return_days(days, cursor, more)