Esempio n. 1
0
 def post(self):
     req_user = self.request.user
     gu_obj = GoogleUser.get_or_insert(req_user.user_id())
     if gu_obj.name:
         self.redirect('/')
         return
     
     errors = []
     name = self.POST['name'].strip().lower()
     if name:
         if len(name)<20:
             if re.search('^[a-zA-Z0-9]+$', name):
                 check_obj = Member.get_by_key_name(str(name))
                 if check_obj:
                     errors.append('该用户名已被注册,请换一个吧')
                 else:
                     #get member id
                     mid_obj = Counter.get_or_insert('member_auto_increment',name = 'member_auto_increment', value = 1)
                     nuser_obj = Member(key_name=name, id = mid_obj.value, flag = 1, add = int(time()))
                     nuser_obj.put()
                     if nuser_obj.is_saved():
                         #set google user
                         gu_obj.name = name
                         db.run_in_transaction(obj_runput,gu_obj)
                         #all member num +1
                         mid_obj.value += 1
                         db.run_in_transaction(obj_runput,mid_obj)
                         self.redirect('/setavatar')
                         return
                     else:
                         errors.append('服务器出现意外错误,请稍后再试')
             else:
                 errors.append('用户名只能包含字母和数字')
         else:
             errors.append('用户名太长了')
     else:
         errors.append('用户名必填')
     
     self.echo('setname.html', {
         'title': "设置名字",
         'errors':errors,
         'name':name,
         'newest_node': Node.get_newest(),
     }, layout='_layout.html')
Esempio n. 2
0
    def post(self):
        req_user = self.request.user
        gu_obj = GoogleUser.get_or_insert(req_user.email())
        if gu_obj.name:
            self.redirect('/')
            return

        errors = []
        name = self.POST['name'].strip().lower()
        if name:
            if len(name) < 20:
                if re.search('^[a-zA-Z0-9]+$', name):
                    check_obj = Member.get_by_key_name(str(name))
                    if check_obj:
                        errors.append('该用户名已被注册,请换一个吧')
                    else:
                        #get member id
                        mid_obj = Counter.get_or_insert(
                            'member_auto_increment',
                            name='member_auto_increment',
                            value=1)
                        nuser_obj = Member(key_name=name,
                                           id=mid_obj.value,
                                           flag=1,
                                           add=int(time()))
                        nuser_obj.put()
                        if nuser_obj.is_saved():
                            #set google user
                            gu_obj.name = name
                            db.run_in_transaction(obj_runput, gu_obj)
                            #all member num +1
                            mid_obj.value += 1
                            db.run_in_transaction(obj_runput, mid_obj)

                            #try to Fetch Google Plus Profile Picture
                            try:
                                img_url = 'https://profiles.google.com/s2/photos/profile/' + req_user.nickname(
                                )
                                img_data = read_img(img_url)
                                #self.header['Content-Type'] = "image/jpg"
                                if img_data:
                                    pass
                                else:
                                    img_url = 'https://www.gravatar.com/avatar/%s?s=73&d=monsterid' % md5(
                                        req_user.email()).hexdigest()
                                    img_data = read_img(img_url)

                                if img_data:
                                    imgobj = images.Image(img_data)
                                    if imgobj.width <= 73 and imgobj.height <= 73:
                                        img_large = img_data
                                    else:
                                        imgobj.resize(73, 73)
                                        imgobj.im_feeling_lucky()
                                        img_large = imgobj.execute_transforms(
                                            output_encoding=images.JPEG,
                                            quality=95)

                                    u = UpYun(DOMAIN_NAME_AVATAR, UPYUN_USER,
                                              UPYUN_PW)
                                    file_path_name = '/avatar/%s.jpg' % self.cur_user.name
                                    avatar = u.writeFile(
                                        file_path_name, img_large, True)

                                    if avatar:
                                        m_obj = self.cur_user  #Member.get_by_key_name(self.cur_user.name)
                                        if m_obj.flag == 1:
                                            if m_obj.id == 1:
                                                m_obj.flag = 99
                                            else:
                                                m_obj.flag = 2
                                            db.run_in_transaction(
                                                obj_runput, m_obj, [
                                                    'cur_user:'******'Fetch Google Plus Profile Picture Failure: '
                                    + req_user.email())
                            #
                            self.redirect('/setavatar')
                            return
                        else:
                            errors.append('服务器出现意外错误,请稍后再试')
                else:
                    errors.append('用户名只能包含字母和数字')
            else:
                errors.append('用户名太长了')
        else:
            errors.append('用户名必填')

        self.echo('setname.html', {
            'title': "设置名字",
            'errors': errors,
            'name': name,
            'newest_node': Node.get_newest(),
        },
                  layout='_layout.html')