Beispiel #1
0
    def get(self, action):
        if action == 'add':
            app_id = int(self.get_argument('app_id',  int(self.get_cookie('app_id', -1))))
            if app_id == -1:
                self.write(u'缺少 app_id 参数')
                return

            self.render('admin/answer_edit.html', action=action)

        elif action == 'edit':
            _id = int(self.get_argument('id', 1))
            answer = db.get('SELECT * FROM answer WHERE id=%d' % _id)

            if answer is None:
                self.write(u'找不到该答案')
                return

            self.render('admin/answer_edit.html', action=action, answer=answer)

        elif action == 'delete':
            _id = int(self.get_argument('id', 1))
            answer = db.get('SELECT * FROM answer WHERE id=%d' % _id)

            if answer is None:
                self.write(json.dumps(dict(flag=False, message='未找到该答案')))
                return

            sql = "DELETE FROM answer WHERE id=%d" % _id
            db.delete(sql)

            self.write("删除成功")
Beispiel #2
0
    def get(self, action):
        if action == 'add':
            self.render('admin/app_edit.html', action=action)

        elif action == 'edit':
            app_id = int(self.get_argument('app_id', 1))
            app = db.get('SELECT * FROM app WHERE id=%d' % app_id)

            if app is None:
                self.write(u'找不到该 APP')
                return

            self.render('admin/app_edit.html', action=action, app=app)

        elif action == 'delete':
            app_id = int(self.get_argument('app_id', 1))
            app = db.get('SELECT * FROM app WHERE id=%d' % app_id)

            if app is None:
                self.write(json.dumps(dict(flag=False, message='未找到该 APP')))
                return

            sql = "DELETE FROM app WHERE id=%d" % app_id
            db.delete(sql)

            self.write("删除成功")
Beispiel #3
0
    def get_app(self, app_id):
        try:
            app_id = int(app_id)
            app = db.get("SELECT * FROM app WHERE id=%d" % app_id)

            if app is None:
                raise Exception("does not have this id")
            return app_id, app

        except Exception, e:
            self.write(u"Err:APP not found, " + e.message if e.message is not '' else str(e.args))
            return None, None
Beispiel #4
0
    def post(self, action):
        self.set_header('Content-Type', 'text/json')

        if action == 'add':
            app_id = int(self.get_argument('app_id', -1))
            if app_id == -1:
                self.write(u'缺少 app_id 参数')
                return

            logo_url = self.get_file_uploaded_url('logo_url')
            if logo_url is None:
                self.write(json.dumps(dict(flag=False, message='图片上传到七牛失败')))
                return

            sql = "INSERT INTO answer(app_id, logo_url, title, subtitle, content) " \
                  "VALUES(%d, '%s', '%s', '%s', '%s')" \
                  % (app_id,
                     MySQLdb.escape_string(logo_url),
                     MySQLdb.escape_string(self.get_body_argument('title')),
                     '',
                     MySQLdb.escape_string(self.get_body_argument('content')),)

            db.insert(sql)
            self.write(json.dumps(dict(flag=True, url='/test/admin/answer?app_id=%d' % app_id)))

        elif action == 'edit':
            _id = int(self.get_argument('id', 1))
            answer = db.get('SELECT * FROM answer WHERE id=%d' % _id)

            if answer is None:
                self.write(u'找不到该答案')
                return

            logo_url = self.get_file_uploaded_url('logo_url')
            if logo_url is None:
                self.write(json.dumps(dict(flag=False, message='图片上传到七牛失败')))
                return
            elif logo_url == "":
                logo_url = answer.logo_url

            sql = "UPDATE answer SET logo_url='%s', title='%s', " \
                  "subtitle='%s', content='%s' WHERE id=%d" \
                  % (MySQLdb.escape_string(logo_url),
                     MySQLdb.escape_string(self.get_body_argument('title')),
                     "",
                     MySQLdb.escape_string(self.get_body_argument('content')),
                     _id)

            db.update(sql)
            self.write(json.dumps(dict(flag=True, url='/test/admin/answer')))
Beispiel #5
0
    def get(self, app_id):
        input_str = self.get_argument("input", None)
        if input_str is None:
            self.write("Input is none")
            return

        app_id, app = self.get_app(app_id)
        if app is None:
            return

        count = db.get("SELECT COUNT(*) AS COUNT FROM answer WHERE app_id=%d" % app_id).COUNT
        if count == 0:
            self.write("This APP has no answers")
            return

        md5impl = hashlib.md5()
        md5impl.update(input_str)
        answer_row = int(md5impl.hexdigest(), 16) % count

        answer = db.get("SELECT * FROM answer WHERE app_id=%d LIMIT %d,1;" % (app_id, answer_row))

        BaseHandler.update_pv(app_id, app.pv+1, app.fake_pv+1)
        self.render('result.html', input_str=input_str, app=app, answer=answer, retest_url="/test/%d" % app_id)
Beispiel #6
0
    def post(self, action):
        self.set_header('Content-Type', 'text/json')

        if action == 'add':
            qrcode_url = self.get_file_uploaded_url('qrcode_url')
            if qrcode_url is None:
                self.write(json.dumps(dict(flag=False, message='图片上传到七牛失败')))
                return

            bg_url = self.get_file_uploaded_url('bg_url')
            if bg_url is None:
                self.write(json.dumps(dict(flag=False, message='图片上传到七牛失败')))
                return

            sql = "INSERT INTO app(name, qrcode_url, bg_url, question, intro, input_label, " \
                  "answer_prefix, uid, follow_tip, retest_tip, pv, fake_pv, active) " \
                  "VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d)" \
                  % (MySQLdb.escape_string(self.get_body_argument('name')),
                     MySQLdb.escape_string(qrcode_url),
                     MySQLdb.escape_string(bg_url),
                     MySQLdb.escape_string(self.get_body_argument('question')),
                     MySQLdb.escape_string(self.get_body_argument('intro')),
                     MySQLdb.escape_string(self.get_body_argument('input_label')),
                     '', 1,
                     MySQLdb.escape_string(self.get_body_argument('follow_tip')),
                     MySQLdb.escape_string(self.get_body_argument('retest_tip')),
                     0, int(self.get_body_argument('fake_pv')), True)

            db.insert(sql)
            self.write(json.dumps(dict(flag=True, url='/test/admin/app')))

        elif action == 'edit':
            app_id = int(self.get_argument('app_id', 1))
            app = db.get('SELECT * FROM app WHERE id=%d' % app_id)

            if app is None:
                self.write(json.dumps(dict(flag=False, message='未找到该 APP')))
                return

            qrcode_url = self.get_file_uploaded_url('qrcode_url')
            if qrcode_url is None:
                self.write(json.dumps(dict(flag=False, message='图片上传到七牛失败')))
                return
            elif qrcode_url == '':
                qrcode_url = app.qrcode_url

            bg_url = self.get_file_uploaded_url('bg_url')
            if bg_url is None:
                self.write(json.dumps(dict(flag=False, message='图片上传到七牛失败')))
                return
            elif bg_url == '':
                bg_url = app.bg_url

            sql = "UPDATE app SET name='%s', qrcode_url='%s', bg_url='%s', " \
                  "question='%s', intro='%s', input_label='%s', follow_tip='%s', retest_tip='%s', " \
                  "fake_pv=%d WHERE id=%d" \
                  % (MySQLdb.escape_string(self.get_body_argument('name')),
                     MySQLdb.escape_string(qrcode_url),
                     MySQLdb.escape_string(bg_url),
                     MySQLdb.escape_string(self.get_body_argument('question')),
                     MySQLdb.escape_string(self.get_body_argument('intro')),
                     MySQLdb.escape_string(self.get_body_argument('input_label')),
                     MySQLdb.escape_string(self.get_body_argument('follow_tip')),
                     MySQLdb.escape_string(self.get_body_argument('retest_tip')),
                     int(self.get_body_argument('fake_pv')),
                     app_id)

            db.update(sql)
            self.write(json.dumps(dict(flag=True, url='/test/admin/app')))