コード例 #1
0
def save():
    if request.method == 'POST':
        try:
            imglist = ['1d0', 'cvr', 'ar9', 'djk', '1nk', '9bf']
            img = random.choice(imglist)

            value_input = request.form['value_input']

            vote = Vote(value=int(value_input), image_id=img)
            res = RequestsApi.save_api(vote)
            return redirect(url_for('index'))
        except:
            return "not saved"
コード例 #2
0
ファイル: main_handler.py プロジェクト: roiprez/gae
    def get(self):
        reload(sys)
        sys.setdefaultencoding("utf-8")

        jinja = jinja2.get_jinja2(app=self.app)

        filtered = False

        if self.request.get('category') or self.request.get('text_filter'):
            filtered = True
        else:
            filtered = False

        if self.request.get('category'):
            books = Book.query(Book.category == self.request.get('category'))
        else:
            books = Book.query()

        text_filter = self.request.get('text_filter')

        book_list = []

        for book in books:
            if text_filter.lower() in book.title.lower():
                votes = Vote.query(Vote.book_id == book.key.id())

                ratings = []
                for vote in votes:
                    ratings.append(vote.stars)

                if ratings:
                    mean = float(sum(ratings)) / len(ratings)
                    stars = round(mean * 2) / 2
                else:
                    stars = 0

                book_list.append({
                    'id': book.key.id(),
                    'src': book.src,
                    'title': book.title,
                    'stars': stars,
                    'user': book.user,
                    'num_votes': len(ratings)
                })

        template_values = {'book_cards': book_list, 'users': users, 'filtered': filtered,
                           'category_filter': self.request.get('category'),
                           'text_filter': self.request.get('text_filter')}

        self.response.write(jinja.render_template("index.html", **template_values))
コード例 #3
0
ファイル: app.py プロジェクト: sza95/momento3python
def save():
    if session_validate() == False:
        return redirect(url_for('login'))

    if request.method == 'POST':
        try:
            imglist = ['12d', '7hv', '47c', 'MTkwMTA4Mw', '2fd', 'ORMN4W_9X']
            img = random.choice(imglist)
            value_input = request.form['value_input']
            vote = Vote(value=int(value_input), image_id=img)
            res = RequestsApi.save_api(vote)
            flash('Vote saved')
            return redirect(url_for('index'))
        except:
            flash('Not saved')
コード例 #4
0
ファイル: app.py プロジェクト: sebastianvarelag/m3-python
def save():
    if session_validate() == False:
        return redirect(url_for('login'))
    if request.method == 'POST':
        try:
            imglist = ['1d0', 'cvr', 'ar9', '1nk', '9bf']
            img = random.choice(imglist)

            value_input = request.form['value_input']

            vote = Vote(value=int(value_input), image_id=img)
            res = RequestsApi.save_api(vote)
            print(res)
            flash('Vote saved')
            return redirect(url_for('index'))
        except:
            flash('Not saved')
コード例 #5
0
ファイル: book_handler.py プロジェクト: roiprez/gae
    def get(self):
        jinja = jinja2.get_jinja2(app=self.app)

        user = users.get_current_user()
        if user:
            book_id = self.request.get('book_id')

            book_key = ndb.Key(Book, int(book_id))

            book = book_key.get()

            votes = Vote.query(Vote.book_id == book.key.id())

            ratings = []
            for vote in votes:
                ratings.append(vote.stars)

            if ratings:
                mean = sum(ratings) / len(ratings)
                stars = round(mean * 2) / 2
            else:
                stars = 0

            template_values = {
                'book': {
                    'id': book_id,
                    'src': book.src,
                    'title': book.title,
                    'description': book.description,
                    'votes': votes
                },
                'users': users
            }

            self.response.write(
                jinja.render_template("book.html", **template_values))

        else:
            template_values = {
                'login_url': users.create_login_url('/'),
                'users': users
            }

            self.response.out.write(
                jinja.render_template("sign_in.html", **template_values))
コード例 #6
0
    def get(self):
        user = users.get_current_user()
        if user == None:
            self.redirect("/")

        else:
            jinja = jinja2.get_jinja2(app=self.app)
            book_id = int(self.request.get('book_id'))
            votes = Vote.query(Vote.book_id == book_id)

            vote_list = []

            for vote in votes:
                vote_list.append({
                    'comment': vote.comment,
                    'stars': vote.stars,
                    'user': vote.user
                })

            template_values = {'votes': vote_list}

            self.response.write(
                jinja.render_template("book.html", **template_values))
コード例 #7
0
    def post(self):
        user = users.get_current_user()

        if user:

            book_id = int(self.request.get('book_id'))

            if self.request.get('deleting'):
                votes = Vote.query(Vote.book_id == book_id,
                                   Vote.user == user.email())

                for vote in votes:
                    vote.key.delete()

            else:
                stars = int(self.request.get('stars'))
                comment = self.request.get('comment')

                votes = Vote.query(Vote.book_id == book_id,
                                   Vote.user == user.email())

                for vote in votes:
                    vote.key.delete()

                vote = Vote(book_id=book_id,
                            stars=stars,
                            comment=comment,
                            user=user.email())

                vote.put()

            # Para darle tiempo al recargar a que coja el nuevo comentario
            time.sleep(1)

            self.redirect('/book?book_id={}'.format(book_id))

        else:
            greeting = str.format("<a href=\"{0}\">Sign in or register</a>.",
                                  users.create_login_url('/'))

            self.response.out.write(
                str.format("<html><body>{0}</body></html>", greeting))