rest api

2018-06-07  本文已影响0人  入间

Api 接口 高山山书写例子

  # 注册用户
  def put(self, request, *args, **kwargs):
      data = request.PUT
      username = data.get('username', '')
      password = data.get('password', '')
      regist_code = data.get('regist_code', '')
      session_regist_code = request.session.get('regist_code', '')
      category = data.get('category', 'userinfo')
      ensure_password = data.get('ensure_password', '')
  
      # 构建错误信息字典
      errors = dict()
      if not username:
          errors['username'] = '没有提供用户名'
      elif User.objects.filter(username=username):
          errors['username'] = '用户名已存在'
      if len(password) < 6:
          errors['password'] = '密码长度不够'
      if password != ensure_password:
          errors['ensure_password'] = '密码不一样'
      if regist_code != str(session_regist_code):
          errors['regist_code'] = '验证码不对'
      if errors:
          return HttpResponse(json.dumps(errors), content_type='application/json')
      user = User()
      user.username = username
      # 设置密码
      user.set_password(password)
      user.save()
      # 根据用户类型,创建普通用户或者客户
      if category == 'userinfo':
          userinfo = UserInfo()
          userinfo.user = user
          userinfo.name = '姓名'
          userinfo.save()
      else:
          customer = Customer()
          customer.name = '客户名称'
          customer.user = user
          customer.save()
  
      return HttpResponse(json.dumps({
          "msg": "创建成功",
          "user_id": user.id
      }), content_type='application/json')
  
  # 更新用户
  def post(self, request, *args, **kwargs):
      data = request.POST
      user = request.user
      if user.is_authenticated:
          # 判断是否是普通用户
          if hasattr(user, 'userinfo'):
              userinfo = user.userinfo
              userinfo.name = data.get('name', '姓名')
              userinfo.age = data.get('age', '')
              userinfo.gender = data.get('gender', '')
              userinfo.phone = data.get('phone', '')
              userinfo.email = data.get('email', '')
              userinfo.address = data.get('address', '')
  
              # 时间特殊处理
              try:
                  birthday = datetime.strptime(
                      data.get('birthday', '2018-01-01'), "%Y-%m-%d")
              except Exception as e:
                  birthday = datetime.now()
  
              userinfo.birthday = birthday
  
              userinfo.qq = data.get('qq', '')
              userinfo.wechat = data.get('wechat', '')
              userinfo.job = data.get('job', '')
              userinfo.salary = data.get('salary', '')
              userinfo.save()
          # 判断是否是客户
          elif hasattr(user, 'customer'):
              customer = user.customer
              customer.name = data.get('name', '客户名称')
              customer.email = data.get('email', '')
              customer.company = data.get('company', '')
              customer.address = data.get('address', '')
              customer.phone = data.get('phone', '')
              customer.mobile = data.get('mobile', '')
              customer.qq = data.get('qq', '')
              customer.wechat = data.get('wechat', '')
              customer.web = data.get('web', '')
              customer.industry = data.get('industry', '')
              customer.description = data.get('description', '')
              customer.save()
          return HttpResponse(json.dumps({
              'msg': '更新成功'
          }), content_type="application/json")
      return HttpResponse(json.dumps({
          'msg': '还未登录'
      }), content_type="application/json")

用户登录与退出

class SessionResource(Resource):

  def get(self, request, *args, **kwargs):
      if request.user.is_authenticated:
          return json_response({
              'msg': '已经登录'
          })
      return json_response({
          'msg': '还未登录'
      })
  
  def put(self, request, *args, **kwargs):
      data = request.PUT
      username = data.get('username', '')
      password = data.get('password', '')
      user = authenticate(username=username, password=password)
      if user:
          login(request, user)
          return json_response({
              'msg': '登录成功'
          })
      return json_response({
          'msg': '用户名或密码错误'
      })
  
  def delete(self, request, *args, **kwargs):
      logout(request)
      return json_response({
          'msg': '退出成功'
      })

问卷资源

class QuestionnaireResource(Resource):

@customer_required
def get(self, request, *args, **kwargs):
return json_response({})

  @customer_required
  def put(self, request, *args, **kwargs):
      return json_response({})


  @customer_required
  def post(self, request, *args, **kwargs):
      return json_response({})
  
  @customer_required
  def delete(self, request, *args, **kwargs):
      return json_response({})

​```
  
​```
上一篇下一篇

猜你喜欢

热点阅读