Ejemplo n.º 1
0
    def get(self):
        cookie = self.request.get('cookie')

        self.response.headers['Content-Type'] = 'application/json; charset=utf-8'

        if not cookie:
            error = json_error('vote', 'failure', 'cookie')
            self.response.write(json.dumps(error))

        else:
            id = self.request.get('id')
        
            if not id:
                error = json_error('vote', 'failure', 'no id')
                self.response.write(json.dumps(error))
   
            else:
                session = Session.query(Session.cookie == cookie).get()

                if not session:
                    error = json_error('vote', 'failure', 'session')
                    self.response.write(error)
                else:
                    key = ndb.Key(urlsafe=id)
                    submission = key.get()

                    if not submission:
                        error = json_error('vote', 'failure', 'no  submission')
                        self.response.write(error)
                    else:
                        value = self.request.get('value')
                        if not value:
                            error = json_error('vote', 'failure', 'value')
                            self.response.write(error)
                        else:
                            value = int(value)
                            if value < -1 or value > 1:
                                error = json_error('vote', 'failure', 'value')
                                self.response.write(error)
                            else:
                                last_vote = Vote.query(ndb.AND(Vote.user == session.user, Vote.sub_id == id)).get()
                                if not last_vote:
                                    vote_val = 0     
                                else:
                                    vote_val = last_vote.value
                                    last_vote.key.delete()

                                vote_val = value - vote_val;
                                old_score = submission.rating
                                new_score = old_score + vote_val

                                submission.rating = new_score;
                                submission.put()

                                vote = Vote(sub_id = id, value = value, user = session.user)
                                vote.put()

                                response = vote_result('ok')
                                self.response.write(response)
Ejemplo n.º 2
0
    def get(self):
        cookie = self.request.get('cookie')

        self.response.headers['Content-Type'] = 'application/json; charset=utf-8'

        if not cookie:
            error = json_error('retrieve', 'failure', 'cookie')
            self.response.write(json.dumps(error))

        else:
            flag = self.request.get('flag')
        
            if not flag:
                error = json_error('retrieve', 'failure', 'no flag')
                self.response.write(json.dumps(error))

            else:
                session = Session.query(Session.cookie == cookie).get()

                if not session:
                    error = json_error('retrieve', 'failure', 'session')
                    self.response.write(error)

                else:
                    # flag = 1 means a single request
                    if flag == '1':
                        try:
                            id = self.request.get('id');
                            key = ndb.Key(urlsafe=id)

                            if not id:
                                self.response.write(json.dumps(json_error('single request', 'failure', 'id')))

                            else:
                                submission = key.get()

                                if not submission:
                                    error = json_error('single request', 'failure', 'no corresponding submission')
                                    self.response.write(json.dumps(error))

                                else:
                                    last_vote = Vote.query(ndb.AND(Vote.user == session.user, Vote.sub_id == submission.key.urlsafe())).get()
                                    if not last_vote:
                                        vote_val = 0     
                                    else:
                                        vote_val = last_vote.value
    
                                    string_submission = json_string(submission.key.urlsafe(), submission.name, submission.category, submission.description, submission.location,
                                                                    submission.image, submission.keywords, submission.submitter,
                                                                    submission.tfrom, submission.tto, submission.rating, vote_val)
                                    response = json.dumps(string_submission)
                                    self.response.write(response)

                        except:
                            self.response.write(json.dumps(json_error('single request', 'failure', 'id')))


                    # flag = 2 means that we are requesting submissions to display in what's new
                    elif flag == '2':
                        time_range = 1447786800
                        date = datetime.datetime.fromtimestamp(time_range/1e3)
                        submissions_number = 20

                        submissions = Submission.query(Submission.submitted >= date).fetch(submissions_number)

                        if not submissions:
                            error = json_error('what is new', 'failure', 'nothing in range')
                            self.response.write(json.dumps(error))

                        else:
                            submissions_array = []
                            if(submissions_number <= len(submissions)):
                                for i in range(0, submissions_number):
                                    submission = submissions[i]
                                    # what's new used for around you too. Server return location so that the app
                                    # can use it to display submissions near user on the map
                                    json_submission = json_array_with_location(submission.key.urlsafe(), submission.name,submission.image, submission.rating, submission.location)
                                    submissions_array.append(json_submission)

                            else:
                                for i in range(0, len(submissions)):
                                    submission = submissions[i]
                                    json_submission = json_array_with_location(submission.key.urlsafe(), submission.name, submission.image, submission.rating, submission.location)
                                    submissions_array.append(json_submission)


                            response = json.dumps(submissions_array)
                            self.response.write(response)

                    # flag = 4 means that we are requesting submissions for a specific category
                    elif flag == '4':
                        category = self.request.get('category')
                        submissions_number = 20

                        if not category:
                            error = json_error('retrieve category', 'failure', 'no category')
                            self.response.write(json.dumps(error))

                        else:
                            submissions = Submission.query(Submission.category == category).fetch(submissions_number)

                            if not submissions:
                                error = json_error('retrieve category', 'failure', 'empty category')
                                self.response.write(json.dumps(error))

                            else:
                                submissions_array = []
                                if(submissions_number <= len(submissions)):
                                    for i in range(0, submissions_number):
                                        submission = submissions[i]
                                        json_submission = json_array(submission.key.urlsafe(), submission.name,submission.image, submission.rating)
                                        submissions_array.append(json_submission)

                                else:
                                    for i in range(0, len(submissions)):
                                        submission = submissions[i]
                                        json_submission = json_array(submission.key.urlsafe(), submission.name,submission.image, submission.rating)
                                        submissions_array.append(json_submission)

                                response = json.dumps(submissions_array)
                                self.response.write(response)

                    # every other flag generate an error
                    else:
                        error = json_error('retrieve' ,'failure', 'flag')
                        self.response.write(json.dumps(error))