Exemple #1
0
  def post(self, request, format=None):
    pdb.set_trace()
    # html POST 
    if request.accepted_renderer.format == 'html':
      user_form = UserForm(request.POST)
      buy_form = BuyForm(request.POST)
      # check form's validity
      if user_form.is_valid() and buy_form.is_valid():
        # check former undone request with same email
        old_users = User.objects.filter(email=user_form.cleaned_data['email'])
        #old_reqs = Buy.objects.filter(email=form.cleaned_data['email']).filter(is_done=False)
        if (old_users.count() > 0):
          data = {'msg':'save failed. same email exists', 'errcode':'101'}
          resp_status = status.HTTP_409_CONFLICT
        # send confirm mail after save
        else:
          try:
            user = user_form.save(commit=False)
            user.is_active = False
            user.set_password(user.password)
            buy = buy_form.save(commit=False)
            user.save()
            buy.user = user
            buy.cellphone = buy.cellphone.translate({ord('-'):None})
            buy.save()
          except StandardError:
            if not user == None:
              User.objects.filter(id = user.id).delete()
            resp_status = status.HTTP_400_BAD_REQUEST
          else:
            self.send_email(buy.id, user.email)
            data = {'msg':'saved', 'errcode':'000'}
            resp_status = status.HTTP_201_CREATED
      # invalid form
      else:
        msg = 'invalid form'
        if not user_form.is_valid():
          msg += ' - ' + str(user_form.errors)
        if not buy_form.is_valid():
          msg += ' - ' + str(dealer_form.errors)
        data = {'msg':msg, 'errcode':'103'}
        resp_status = status.HTTP_400_BAD_REQUEST
      return Response(data, status = resp_status, template_name='consumer_request_result.html')

    # Mobile POST
    else:
      user = NewUserSerializer(data=request.DATA)
      buy = BuyNewSerializer(data=request.DATA)
      if user.is_valid() and buy.is_valid():
        # check former undone request with same email
        old_users = User.objects.filter(email=request.DATA['email'])
        #oldReqs = Buy.objects.filter(email=serializer.data['email']).filter(is_done=False)
        if (old_users.count() > 0):
          data = {'msg':'save failed. same email exists', 'errcode':'101'}
          resp_status = status.HTTP_409_CONFLICT
        # send confirm mail after save
        else:
          try:
            saved_user = user.save()
            saved_user.is_active = False
            saved_user.set_password(saved_user.password)
            saved_user.save()
            saved_buy = buy.save()
            saved_buy.user = saved_user
            saved_buy.cellphone = saved_buy.cellphone.translate({ord('-'):None})
            saved_buy.save()
          except StandardError:
            if not saved_user == None:
              User.objects.filter(id = saved_user.id).delete()
            resp_status = status.HTTP_400_BAD_REQUEST
          else:
            self.send_email(saved_buy.id, saved_user.email)
            data = {'msg':'saved', 'errcode':'000'}
            resp_status = status.HTTP_201_CREATED
      else:
        data = {'msg':'invalid request', 'errcode':'102'}
        resp_status = status.HTTP_400_BAD_REQUEST

      return Response(data, status = resp_status)
Exemple #2
0
  def post(self, request, format=None):
    pdb.set_trace()
    # html POST
    if request.accepted_renderer.format == 'html':
      is_registered = False
      user_form = UserForm(request.POST)
      dealer_form = DealerNewForm(request.POST)
      # check form's validity
      if user_form.is_valid() and dealer_form.is_valid():
        # check former undone request with same email
        old_users = User.objects.filter(email=user_form.cleaned_data['email'])
        if (old_users.count() > 0):
          msg = 'register failed. same email exists'
          resp_code = '101'
          resp_status = status.HTTP_409_CONFLICT
        else:
          user = user_form.save(commit=False)
          user.is_active = False
          user.set_password(user.password) 
          dealer = dealer_form.save(commit=False)
          user.save()
          dealer.user = user
          dealer.phone = dealer.phone.translate({ord('-'):None})
          dealer.save()
          # send confirm mail after save
          self.send_email(user.id, user.email)
          msg = 'saved'
          resp_code = '000'
          resp_status = status.HTTP_201_CREATED
          is_registered = True
      # invalid form
      else:
        msg = 'invalid form'
        if not user_form.is_valid():
          msg += ' - ' + str(user_form.errors)
        if not dealer_form.is_valid():
          msg += ' - ' + str(dealer_form.errors)
        resp_code = '103'
        resp_status = status.HTTP_400_BAD_REQUEST
      data = {'msg':msg, 'resp code':resp_code}
      return Response(data, status = resp_status, template_name='dealer_register_result.html')

    # Mobile POST
    else:
      #  WANING!! model dealer need used id. client send temporary uid 1
      user = NewUserSerializer(data=request.DATA)
      dealer = NewDealerSerializer(data=request.DATA)
      if user.is_valid() and dealer.is_valid():
        # check former undone request with same email
        old_users = User.objects.filter(email=request.DATA['email'])
        if (old_users.count() > 0):
          resp_status = status.HTTP_409_CONFLICT
        else:
          try:
            user.save()
            user.is_active = False
            user.set_password(user.password)
            user.save()
            dealer.save()
            dealer.user = user
            dealer.phone = dealer.phone.translate({ord('-'):None})
            dealer.save()
            #user = user_serializer.save()
            #user.is_active = False
            #user.set_password(user.password)
            #user.save()
            #dealer = dealer_serializer.save()
            #dealer.user = user
            #dealer.save()
          except StandardError:
            if not user == None:
              User.objects.filter(id = user.id).delete()
            resp_status = status.HTTP_400_BAD_REQUEST
          else:
            # send confirm mail after save
            self.send_email(user.id, user.email)
            resp_status = status.HTTP_201_CREATED
      else:
        resp_status = status.HTTP_400_BAD_REQUEST

      return Response(status = resp_status)