Beispiel #1
0
  def post(self, **kwargs):
    result = {
      'code': 400,
      'message': 'bad request'
    }
    
    # update user info
    if kwargs.get('uid'):
      if self.get_user() is None:
        result['code'] = 401
        result['message'] = 'unauthorized'
      else:
        
        uid = self.get_user().get('uid')
  
        if kwargs.get('uid') != 'me' and uid != int(kwargs.get('uid')):
          result['code'] = 400
          result['message'] = 'already exists'
          return self.createRes(400, result)
        
        reqInfo = self.arguments
          
        # get user info
        user = Users.get(id=uid)
        if type(user) == ndb.key.Key:
          result['code'] = 400
          result['message'] = 'bad request'
        
        else:
          # using API, user can modify nickname and picture
          available_list = ['nickname', 'picture', 'hanasee', 'language'] + [kwargs.get('attribute')]
    
          reqInfo = []
          for field in self.arguments:
            if field in available_list:
              if hasattr(self.arguments[field], 'FieldStorageClass'):
                dtCreated = datetime.datetime.now()
                filename = "%d" % (time.mktime(dtCreated.timetuple())*1e3 + dtCreated.microsecond/1e3)
                
                image_url = self.create_file(self.arguments.get(field).value, filename, self.arguments.get(field).type)
                setattr(user, field, image_url)
              elif field == 'password':
                if user.password == md5.md5(self.arguments.get('old_password')).hexdigest():
                  setattr(user, field, md5.md5(self.arguments.get('password')).hexdigest())
                else:
                  result['code'] = 401
                  result['message'] = 'invalid password'
              else:
                setattr(user, field, self.arguments.get(field))
          
          if result['code'] != 401:
            user.put()
            
            result['code'] = 200
            result['message'] = 'OK'
            result['User'] = user.to_obj(mine = True)
        
    else:
      arguments = self.arguments
      args_regist = ['email', 'password', 'nickname']
  
      # check parameter validation
      if len(set(arguments) & set(args_regist)) == len(args_regist):
        user = Users.find(self.request.get('email'))
  
        if user:
          result['code'] = 401
          result['message'] = 'already registered'
          #return self.createRes(401, result)
  
        else:
          # check password
          #md5password = md5.md5(self.arguments.get('password')).hexdigest()
  
          # insert as a new user
          user = Users(auto_id=True)
          #user.password = md5password
          user.password = self.arguments.get('password')
          user.set(self.convertRequsetParameter(self.arguments, ['password']))
  
          if ('deviceInfo' in self.session) and (appName in self.session['deviceInfo']):
            deviceInfo = self.session['deviceInfo'][self.request.get('appName')]
  
            # save reg_id in user
            setattr(user, re.sub('\.', '', self.request.get('appName')), deviceInfo['regId'])
            user.put()
  
            # save user in device
            device = ndb.Key(Devices, "%s|%s" % (self.request.get('appName'), deviceInfo['deviceId'])).get()
            setattr(device, 'user', user.key)
            device.put()
  
          self.session['user'] = user.to_obj(mine=True)
          result['code'] = 200
          result['message'] = 'OK'
  
    if result['code'] == 200:
      if self.arguments.get('returnTo', None):
        return self.redirect(str(self.arguments.get('returnTo')))
      else:
        return self.createRes(200, result)
    else:
      if self.arguments.get('returnTo', None):
        options = {
          'returnTo': self.arguments.get('returnTo'),
          'message': result['message']
        };

        if self.get_user():
          options['uid'] = self.get_user().get('uid')
        template = JINJA_ENVIRONMENT.get_template(self.arguments.get('dialog'))
        return self.response.write(template.render(options))
      else:
        return self.createRes(401, result)
  def regist(self, **kwargs):
    result = {
      'code': 400,
      'message': 'bad request'
    }

    args_regist = ['email', 'password', 'nickname']

    # check parameter validation
    if len(set(self.arguments) & set(args_regist)) == len(args_regist):
      user = Users.find(self.arguments.get('email'))

      if user:
        result['code'] = 401
        result['message'] = 'already registered'
        #return self.createRes(401, result)

      else:
        # check password
        try:
          md5password = md5.md5(self.arguments.get('password')).hexdigest()
        except UnicodeEncodeError, e:
          logging.error("password = %s" % self.arguments.get('password'))
          return self.createRes(401, result)

        # insert as a new user
        user = Users(auto_id=True)
        user.password = md5password
        user.set(self.convertRequsetParameter(self.arguments, ['password']))

        if ('deviceInfo' in self.session) and ('appName' in self.session['deviceInfo']):
          deviceInfo = self.session['deviceInfo']

          # save reg_id in user
          setattr(user, re.sub('\.', '', '%s_regid' % self.arguments.get('appName')), deviceInfo['regId'])
          user.put()

          # save user in device
          device = ndb.Key(Devices, "%s|%s" % (self.arguments.get('appName'), deviceInfo['deviceId'])).get()
          if device is not None:
            setattr(device, 'user', user.key)
            device.put()

        message = '%s님이 하나시를 시작했습니다.' % user.nickname
        url = 'http://hanasee.com'
        Messages(user=user.key,
          action_user=user.key,
          action='regist',
          settings='system',
          app_name='hanasee',
          message=message,
          url=url).send(['MAIL','SNS'])
        self.session['user'] = user.to_obj(mine=True)
        result['code'] = 200
        result['message'] = 'OK'

      if result['code'] == 200:
        if self.session.get('returnTo', None):
          returnTo = self.session.pop('returnTo')
          return self.redirect(returnTo)
        else:
          result['code'] = 200
          result['message'] = 'OK'
          result['User'] = user.to_obj(mine=True)
          return self.createRes(200, result)
      else:
        if self.session.get('returnTo', None):
          options = {
            'returnTo': self.session.get('returnTo'),
            'message': result['message']
          };

          template = JINJA_ENVIRONMENT.get_template('signin.html')
          return self.response.write(template.render(options))
        else:
          return self.createRes(401, result)