Beispiel #1
0
    def get(self):
        form = AccountActiveForm(self.request.arguments)
        if not form:
            raise HTTPError(404)

        response_data = {}

        uid = form.uid.data
        code = form.code.data

        user = yield UserDocument.find_one({'_id': ObjectId(uid)})
        if not user:
            raise HTTPError(404)

        code = yield CodeDocument.find_one({'uid': user["_id"], 'code': code})
        if not code:
            raise HTTPError(404)

        if user['activated']:
            response_data.update({'error': '该账号已经激活!'})
        elif code['expired_time'] < datetime.now():
            response_data.update({'error': '激活码已失效! 请返回到登录界面重新发送激活码!'})
        else:
            yield UserDocument.update({'_id': user['_id']},
                                      {'$set': {
                                          'activated': True
                                      }})
            response_data.update({'error': '激活成功!'})

        yield CodeDocument.remove({'_id': code['_id']})

        self.render('user/template/feedback.html', response_data=response_data)
Beispiel #2
0
    def get(self):
        form = AccountActiveForm(self.request.arguments)
        if not form:
            raise HTTPError(404)

        response_data = {}

        uid = form.uid.data
        code = form.code.data

        user = yield UserDocument.find_one({'_id': ObjectId(uid)})
        if not user:
            raise HTTPError(404)

        code = yield CodeDocument.find_one({'uid': user["_id"], 'code': code})
        if not code:
            raise HTTPError(404)

        if user['activated']:
            response_data.update({'error': '该账号已经激活!'})
        elif code['expired_time'] < datetime.now():
            response_data.update({
                'error': '激活码已失效! 请返回到登录界面重新发送激活码!'
            })
        else:
            yield UserDocument.update(
                {'_id': user['_id']},
                {'$set': {'activated': True}}
            )
            response_data.update({'error': '激活成功!'})

        yield CodeDocument.remove({'_id': code['_id']})

        self.render('user/template/feedback.html', response_data=response_data)
Beispiel #3
0
    def post(self):
        response_data = {}

        login_reward_fetched = yield UserActivityDocument.login_reward_fetched(
            self.current_user['_id']
        )

        if login_reward_fetched:
            response_data.update({'error': '你已经领取了今日的登录奖励!'})
        else:
            now = datetime.now()

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'activity_type': UserActivityDocument.FETCH_LOGIN_REWARD,
                'time': now
            }
            activity_id = yield UserActivityDocument.insert(document)

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id']
            )
            quantity = (1 + continuous_login_days / 7) * 5

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'activity': DBRef(
                    UserActivityDocument.meta['collection'],
                    ObjectId(activity_id)
                ),
                'in_out_type': WealthRecordDocument.IN,
                'quantity': quantity,
                'time': now
            }
            yield WealthRecordDocument.insert(document)

            yield UserDocument.update_wealth(
                self.current_user['_id'], quantity
            )
            yield UserDocument.update(
                {'_id': ObjectId(self.current_user['_id'])},
                {'$inc': {'continuous_login_days': 1}}
            )

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id']
            )

            response_data.update({
                'wealth': self.current_user['wealth'] + quantity,
                'continuous_login_days': continuous_login_days
            })

        self.write_json(response_data)
Beispiel #4
0
    def post(self):
        response_data = {}

        login_reward_fetched = yield UserActivityDocument.login_reward_fetched(
            self.current_user['_id'])

        if login_reward_fetched:
            response_data.update({'error': '你已经领取了今日的登录奖励!'})
        else:
            now = datetime.now()

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'activity_type':
                UserActivityDocument.FETCH_LOGIN_REWARD,
                'time':
                now
            }
            activity_id = yield UserActivityDocument.insert(document)

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id'])
            quantity = (1 + continuous_login_days / 7) * 5

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'activity':
                DBRef(UserActivityDocument.meta['collection'],
                      ObjectId(activity_id)),
                'in_out_type':
                WealthRecordDocument.IN,
                'quantity':
                quantity,
                'time':
                now
            }
            yield WealthRecordDocument.insert(document)

            yield UserDocument.update_wealth(self.current_user['_id'],
                                             quantity)
            yield UserDocument.update(
                {'_id': ObjectId(self.current_user['_id'])},
                {'$inc': {
                    'continuous_login_days': 1
                }})

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id'])

            response_data.update({
                'wealth': self.current_user['wealth'] + quantity,
                'continuous_login_days': continuous_login_days
            })

        self.finish(json.dumps(response_data))
Beispiel #5
0
    def post(self):
        form = ProfileSetForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        sex = form.sex.data
        birthday = form.birthday.data
        relationship_status = form.relationship_status.data
        province = form.province.data
        city = form.city.data
        phone = form.phone.data
        qq = form.qq.data
        signature = form.signature.data

        document = {}

        if birthday:
            year, month, day = map(int, birthday.split('-'))
            birthday = datetime(year, month, day)
            document.update({'birthday': birthday})

        if sex:
            document.update({'sex': sex})

        if relationship_status not in ['', 'single', 'in_love']:
            raise HTTPError(404)

        document.update({'relationship_status': relationship_status})

        if province and city:
            home = '%s-%s' % (province, city)
            if len(home) < 100:
                document.update({'home': home})
            else:
                raise HTTPError(404)

        regex = re.compile('^\d{11}$')
        document.update({'phone': phone})
        if phone and not regex.match(phone):
            response_data.update({'error': '手机号码错误!'})

        regex = re.compile('^\d{1,20}$')
        document.update({'qq': qq})
        if qq and not regex.match(qq):
            response_data.update({'error': 'qq号码错误!'})

        document.update({'signature': signature})
        if signature and len(signature) > 100:
            response_data.update({'error': '自我介绍不能超过100字!'})

        if not response_data:
            yield UserDocument.update(
                {'_id': ObjectId(self.current_user['_id'])},
                {'$set': document}
            )

        self.write_json(response_data)
Beispiel #6
0
    def post(self):
        form = LeagueBulletinSaveForm(self.request.arguments)
        if not form.validate() or self.current_user['user_type'] != 'league':
            raise HTTPError(404)

        league_bulletin = form.league_bulletin.data

        yield UserDocument.update(
            {'_id': ObjectId(self.current_user['_id'])},
            {'$set': {'league_bulletin': league_bulletin}}
        )
Beispiel #7
0
    def post(self):
        form = PasswordResetPostForm(self.request.arguments)
        if not form:
            raise HTTPError(404)

        password = form.password.data

        session_id = self.get_secure_cookie('sid')
        if not session_id:
            raise HTTPError(404)

        self.session = self.session_manager.load_session(session_id)

        uid = self.session.get('uid')
        code = self.session.get('code')

        if not uid or not code:
            raise HTTPError(404)

        code = yield CodeDocument.find_one({
            'uid': ObjectId(uid),
            'code': code
        })
        if not code:
            raise HTTPError(404)

        user = yield UserDocument.find_one({'_id': ObjectId(uid)})
        if not user:
            raise HTTPError(404)

        password = yield UserDocument.encrypt_password(password)
        yield UserDocument.update({'_id': user["_id"]},
                                  {'$set': {
                                      'password': password
                                  }})
        yield CodeDocument.remove({'_id': ObjectId(code['_id'])})

        try:
            Ejabberd.unregister(user['_id'])
            Ejabberd.register(user['_id'], password)
        except:
            pass

        self.session.clear()
        self.clear_cookie('sid')

        self.finish()
Beispiel #8
0
    def post(self):
        form = PasswordResetPostForm(self.request.arguments)
        if not form:
            raise HTTPError(404)

        password = form.password.data

        session_id = self.get_secure_cookie('sid')
        if not session_id:
            raise HTTPError(404)

        self.session = self.session_manager.load_session(session_id)

        uid = self.session.get('uid')
        code = self.session.get('code')

        if not uid or not code:
            raise HTTPError(404)

        code = yield CodeDocument.find_one({
            'uid': ObjectId(uid),
            'code': code
        })
        if not code:
            raise HTTPError(404)

        user = yield UserDocument.find_one({'_id': ObjectId(uid)})
        if not user:
            raise HTTPError(404)

        password = yield UserDocument.encrypt_password(password)
        yield UserDocument.update(
            {'_id': user["_id"]},
            {'$set': {'password': password}}
        )
        yield CodeDocument.remove({'_id': ObjectId(code['_id'])})

        try:
            Ejabberd.unregister(user['_id'])
            Ejabberd.register(user['_id'], password)
        except:
            pass

        self.session.clear()
        self.clear_cookie('sid')

        self.finish()
Beispiel #9
0
    def post(self):
        response_data = {}

        form = PasswordSetForm(self.request.arguments)
        if form.validate():
            current_password = form.current_password.data
            new_password = form.new_password.data
            repeat_password = form.repeat_password.data

            encrypt_password = yield UserDocument.encrypt_password(
                current_password
            )

            if self.current_user['password'] != encrypt_password:
                response_data.update({'error': '密码错误!'})
            elif new_password != repeat_password:
                response_data.update({'error': '新密码与重复密码不一致!'})
            else:
                new_password = yield UserDocument.encrypt_password(
                    new_password
                )

                yield UserDocument.update(
                    {'_id': ObjectId(self.current_user['_id'])},
                    {'$set': {'password': new_password}}
                )

                try:
                    Ejabberd.unregister(self.current_user['_id'])
                    Ejabberd.register(self.current_user['_id'], new_password)
                except:
                    pass
        else:
            for field in form.errors:
                response_data.update({'error': form.errors[field][0]})
                break

        self.write_json(response_data)
Beispiel #10
0
    def post(self):
        form = AvatarSetForm(self.request.arguments)
        if not form.validate() or 'avatar' not in self.request.files:
            raise HTTPError(404)

        x = form.x.data
        y = form.y.data
        w = form.w.data
        h = form.h.data
        target_width = form.target_width.data

        crop_area = {
            'x': x,
            'y': y,
            'w': w,
            'h': h,
            'target_width': target_width
        }

        upload_file = self.request.files['avatar'][0]

        document = {
            'name': upload_file['filename'],
            'upload_time': datetime.now(),
            'content': Binary(upload_file['body']),
            'content_type': upload_file['content_type'],
            'owner': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            ),
            'crop_area': crop_area
        }

        try:
            image = Image.open(StringIO(upload_file['body']))
        except:
            raise HTTPError(404)

        scale = image.size[0] * 1.0 / target_width

        x = int(x * scale)
        y = int(y * scale)
        w = int(w * scale)
        h = int(h * scale)

        box = (x, y, x + w, y + h)
        image = image.crop(box)

        output50x50 = StringIO()
        output180x180 = StringIO()

        image50x50 = image.resize((50, 50), Image.ANTIALIAS)
        image180x180 = image.resize((180, 180), Image.ANTIALIAS)

        image50x50.save(
            output50x50,
            document['content_type'].split('/')[1].upper(),
            quality=100
        )
        image180x180.save(
            output180x180,
            document['content_type'].split('/')[1].upper(),
            quality=100
        )

        document['thumbnail50x50'] = Binary(output50x50.getvalue())
        document['thumbnail180x180'] = Binary(output180x180.getvalue())

        output50x50.close()
        output180x180.close()

        user_dbref = DBRef(
            UserDocument.meta['collection'],
            ObjectId(self.current_user['_id'])
        )

        yield AvatarDocument.remove({'owner': user_dbref}, multi=True)
        yield AvatarDocument.insert(document)
        yield UserDocument.update(
            {'_id': ObjectId(self.current_user['_id'])},
            {'$set': {'avatar_updated': True}}
        )

        self.finish()