Exemplo n.º 1
0
    def update(self, request, pk=None):
        '''Update paidtype data

        Only agent can update paidtype data.
        '''
        # To check if request body is empty
        if request.user.is_staff == False:
            return Response({'errors': _('Not allowed')},
                            status=HTTP_403_FORBIDDEN)
        if request.body:
            paidtype_obj = PaidType.objects.filter(id=pk).first()
            if not paidtype_obj:
                return Response({'errors': _('Not found')},
                                status=HTTP_404_NOT_FOUND)

            paidtype_data = json.loads(request.body)
            paidtype_keys = [
                'name', 'duration', 'bot_amount', 'faq_amount', 'third_party'
            ]
            err_msg, key_status = utils.key_validator(paidtype_keys,
                                                      paidtype_data)
            if not key_status:
                return Response({'errors': _(err_msg)},
                                status=HTTP_403_FORBIDDEN)
            for k in paidtype_data:
                if k == 'third_party':
                    paidtype_obj.third_party.set(paidtype_data.get(k))
                    continue
                setattr(paidtype_obj, k, paidtype_data.get(k))
            paidtype_obj.save()
            utils.reset_all_bots_thirdparty(paidtype_obj)
            return Response({'success': _('Update succeeded')},
                            status=HTTP_200_OK)
        return Response({'errors': _('No content')},
                        status=HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
 def create(self, request, id=None):
     user_obj = request.user
     mod_obj = Module.objects.filter(id=id).first()
     if not mod_obj:
         return Response({'errors': _('Not found')},
                         status=HTTP_404_NOT_FOUND)
     create_data = json.loads(request.body)
     que_key = ['group', 'content']
     err_msg, valid_status = utils.key_validator(que_key, create_data)
     if not valid_status:
         return Response({'errors': _(err_msg)}, status=HTTP_403_FORBIDDEN)
     mod_faq = \
         ModuleFAQGroup.objects.filter(id=create_data.get('group'),
                                       module=mod_obj).first()
     if not mod_faq:
         return Response({'errors': _('Not found')},
                         status=HTTP_404_NOT_FOUND)
     que_obj = \
         ModuleQuestion.objects.create(group=mod_faq, module=mod_obj,
                                       content=create_data.get('content'))
     if que_obj:
         res = {}
         res['id'] = que_obj.id
         res['content'] = que_obj.content
         res['group'] = que_obj.group.id
         return Response(res, status=HTTP_201_CREATED)
     return Response({'errors': _('Create failed')},
                     status=HTTP_400_BAD_REQUEST)
Exemplo n.º 3
0
    def create(self, request):
        '''Create paidtype

        Create new paidtype
        '''
        if request.user.is_staff == False:
            return Response({'errors': _('Not allowed')},
                            status=HTTP_403_FORBIDDEN)
        if request.body:
            paidtype_data = json.loads(request.body)
            paidtype_keys = [
                'name', 'duration', 'bot_amount', 'faq_amount', 'third_party'
            ]
            err_msg, key_status = utils.key_validator(paidtype_keys,
                                                      paidtype_data)
            if not key_status:
                return Response({'errors': _(err_msg)},
                                status=HTTP_403_FORBIDDEN)
            third_parties = paidtype_data.pop('third_party')
            paidtype_data['user_type'] = 'M'
            paidtype_obj = PaidType.objects.create(**paidtype_data)
            if not paidtype_obj:
                return Response({'errors': _('Create paidtype failed')},
                                status=HTTP_400_BAD_REQUEST)
            paidtype_obj.third_party.set(third_parties)
            paidtype_obj.save()
            res = {}
            res['id'] = paidtype_obj.id
            res['name'] = paidtype_obj.name
            return Response(res, status=HTTP_201_CREATED)
        return Response({'errors': _('No content')},
                        status=HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
 def create(self, request, id=None):
     if request.body:
         user_obj = request.user
         bot_obj = Chatbot.objects.filter(id=id,
                                          hide_status=False,
                                          user=user_obj).first()
         if not bot_obj:
             return Response({'errors': _('Not found')},
                             status=HTTP_404_NOT_FOUND)
         create_data = json.loads(request.body)
         ans_key = ['group', 'content']
         err_msg, valid_status = utils.key_validator(ans_key, create_data)
         if not valid_status:
             return Response({'errors': _(err_msg)},
                             status=HTTP_403_FORBIDDEN)
         faq_group_obj = \
             FAQGroup.objects.filter(id=create_data.get('group'),
                                     chatbot=bot_obj).first()
         if not faq_group_obj:
             return Response({'errors': _('Not found')},
                             status=HTTP_404_NOT_FOUND)
         ans_obj =\
             Answer.objects.create(group_id=create_data.get('group'),
                                   content=create_data.get('content'),
                                   chatbot=bot_obj)
         if ans_obj:
             res = {}
             res['id'] = ans_obj.id
             res['content'] = ans_obj.content
             res['group'] = ans_obj.group.id
             return Response(res, status=HTTP_201_CREATED)
         return Response({'errors': _('Create failed')},
                         status=HTTP_400_BAD_REQUEST)
     return Response({'errors': _('No content')},
                     status=HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
 def update(self, request, id=None, pk=None):
     if request.body:
         user_obj = request.user
         bot_obj = Chatbot.objects.filter(Q(user=user_obj)
                                          | Q(assign_user=user_obj),
                                          id=id,
                                          hide_status=False).first()
         if not bot_obj:
             return Response({'errors': _('Not found')},
                             status=HTTP_404_NOT_FOUND)
         update_data = json.loads(request.body)
         que_key = ['content']
         err_msg, valid_status = utils.key_validator(que_key, update_data)
         if not valid_status:
             return Response({'errors': _(err_msg)},
                             status=HTTP_403_FORBIDDEN)
         que_obj = Question.objects.filter(id=pk, chatbot=bot_obj).first()
         if not que_obj:
             return Response({'errors': _('Not found')},
                             status=HTTP_404_NOT_FOUND)
         que_obj.content = update_data.get('content')
         que_obj.train_content = update_data.get('content')
         que_obj.save()
         return Response({'success': _('Update succeeded')},
                         status=HTTP_200_OK)
     return Response({'errors': _('No content')},
                     status=HTTP_400_BAD_REQUEST)
Exemplo n.º 6
0
 def update(self, request, id, fb_id, pk):
     user_obj = request.user
     bot_obj = Chatbot.objects.filter(id=id, user=user_obj).first()
     if not bot_obj:
         return \
             Response({'errors': _('Not found')}, status=HTTP_404_NOT_FOUND)
     fb_obj = Facebook.objects.filter(id=fb_id, chatbot=bot_obj).first()
     if not fb_obj:
         return \
             Response({'errors': _('Not found')}, status=HTTP_404_NOT_FOUND)
     ignore_obj = \
         FacebookIgnore.objects.filter(facebook=fb_obj, id=pk).first()
     if not ignore_obj:
         return \
             Response({'errors': _('Not found')}, status=HTTP_404_NOT_FOUND)
     if request.body:
         req_data = json.loads(request.body)
         req_data['facebook'] = fb_obj
         req_data['facebook_uid'] = ''
         key_list = ['facebook', 'display_name', 'facebook_uid']
         err_msg, status = utils.key_validator(key_list, req_data)
         if status is True:
             ignore_obj.display_name = req_data.get('display_name')
             ignore_obj.save()
             return Response({'success': _('Update succeeded')},
                             status=HTTP_200_OK)
         else:
             return Response({'errors': _(err_msg)},
                             status=HTTP_400_BAD_REQUEST)
     return \
         Response({'errors': _('No content')}, status=HTTP_400_BAD_REQUEST)
Exemplo n.º 7
0
 def create(self, request, id, fb_id):
     user_obj = request.user
     bot_obj = Chatbot.objects.filter(id=id, user=user_obj).first()
     if not bot_obj:
         return \
             Response({'errors': _('Not found')}, status=HTTP_404_NOT_FOUND)
     fb_obj = Facebook.objects.filter(id=fb_id, chatbot=bot_obj).first()
     if not fb_obj:
         return \
             Response({'errors': _('Not found')}, status=HTTP_404_NOT_FOUND)
     if request.body:
         req_data = json.loads(request.body)
         req_data['facebook'] = fb_obj
         req_data['facebook_uid'] = ''
         key_list = ['facebook', 'display_name', 'facebook_uid']
         err_msg, status = utils.key_validator(key_list, req_data)
         if status is True:
             ignore_obj = FacebookIgnore.objects.create(**req_data)
             ignore_json = FacebookIgnoreSerializer(ignore_obj).data
             return Response(ignore_json, status=HTTP_201_CREATED)
         else:
             return Response({'errors': _(err_msg)},
                             status=HTTP_400_BAD_REQUEST)
     return \
         Response({'errors': _('No content')}, status=HTTP_400_BAD_REQUEST)
Exemplo n.º 8
0
 def update(self, request, pk=None):
     if request.body:
         user_obj = request.user
         bot_obj = \
             Chatbot.objects.filter(Q(user=user_obj) |
                                    Q(assign_user=user_obj),
                                    id=pk, hide_status=False,).first()
         if not bot_obj:
             return Response({'errors': _('Not found')},
                             status=HTTP_404_NOT_FOUND)
         update_data = json.loads(request.body)
         valid_update_key = [
             'robot_name', 'greeting_msg', 'failed_msg', 'postback_title',
             'postback_activate', 'choose_answer', 'domain'
         ]
         err_msg, key_status = utils.key_validator(valid_update_key,
                                                   update_data)
         if not key_status:
             return Response({'errors': _(err_msg)},
                             status=HTTP_403_FORBIDDEN)
         for k, v in update_data.items():
             setattr(bot_obj, k, v)
         bot_obj.save()
         return Response({'success': _('Update succeeded')},
                         status=HTTP_200_OK)
     return Response({'errors': _('No content')},
                     status=HTTP_400_BAD_REQUEST)
Exemplo n.º 9
0
 def create(self, request):
     if request.body:
         user_obj = request.user
         acc_obj = AccountInfo.objects.filter(user=user_obj).first()
         bot_data = json.loads(request.body)
         bot_keys = [
             'robot_name', 'greeting_msg', 'failed_msg', 'postback_title',
             'language'
         ]
         err_msg, key_status = utils.key_validator(bot_keys, bot_data)
         if not key_status:
             return Response({'errors': _(err_msg)},
                             status=HTTP_403_FORBIDDEN)
         bot_data['user_id'] = user_obj.id
         bot_data['bot_type'] = 'TASK'
         bot_obj = Chatbot.objects.create(**bot_data)
         if bot_obj:
             bot_obj.vendor_id = utils.generate_uuid(
                 str(bot_obj.id), bot_obj.robot_name)
             paid_type = acc_obj.paid_type
             for party in paid_type.third_party.all():
                 BotThirdPartyGroup.objects.create(chatbot=bot_obj,
                                                   third_party=party)
             bot_obj.save()
             Line.objects.create(chatbot=bot_obj)
             Facebook.objects.create(chatbot=bot_obj)
             nlumodel.initial_question_answer(bot_obj)
             # TODO: Remove this when NLU is working fine
             nlu_create_status, err_msg = nlumodel.create_model(bot_obj)
             # create_bot_obj = bot_obj
             create_bot_obj = \
                 utils.delete_create_failed_model(nlu_create_status,
                                                  bot_obj)
             if not create_bot_obj:
                 return Response({'errors':_('Create bot failed. '+\
                                  'Cause by NLU error.' + err_msg)},
                                 status=HTTP_400_BAD_REQUEST)
             res = {}
             res['id'] = create_bot_obj.id
             res['robot_name'] = create_bot_obj.robot_name
             return Response(res, status=HTTP_201_CREATED)
         return Response({'errors': _('Create bot failed')},
                         status=HTTP_400_BAD_REQUEST)
     return Response({'errors': _('No content')},
                     status=HTTP_400_BAD_REQUEST)
Exemplo n.º 10
0
 def update(self, request, id=None, pk=None):
     user_obj = request.user
     mod_obj = Module.objects.filter(id=id).first()
     if not mod_obj:
         return Response({'errors': _('Not found')},
                         status=HTTP_404_NOT_FOUND)
     update_data = json.loads(request.body)
     que_key = ['content']
     err_msg, valid_status = utils.key_validator(que_key, update_data)
     if not valid_status:
         return Response({'errors': _(err_msg)}, status=HTTP_403_FORBIDDEN)
     que_obj = ModuleQuestion.objects.filter(id=pk, module=mod_obj).first()
     if not que_obj:
         return Response({'errors': _('Not found')},
                         status=HTTP_404_NOT_FOUND)
     que_obj.content = update_data.get('content')
     que_obj.save()
     return Response({'success': _('Update succeeded')}, status=HTTP_200_OK)
Exemplo n.º 11
0
def member_register(request):
    '''Member register

    For registering member only

    Request format example:
    POST:
    {
        "username":"******",
        "password":"******",
        "first_name":"Jack_is_happy"
    }
    '''

    required_key = ['username', 'password', 'first_name', 'language']
    register_data = json.loads(request.body)
    err_msg, valid = utils.key_validator(required_key, register_data)
    if valid is not True:
        return Response({'errors': _(err_msg)}, status=HTTP_400_BAD_REQUEST)
    user_email = register_data.get('username')
    email_validator = EmailValidator()
    try:
        email_validator(user_email)
    except:
        return Response({'errors': _('Invalid email address')},
                        status=HTTP_400_BAD_REQUEST)
    user = User.objects.filter(username=register_data.get("username")).first()
    if not user:
        user_create_obj = {}
        acc_create_obj = {}

        user_create_obj['username'] = register_data.get('username')
        user_create_obj['password'] = register_data.get('password')
        user_create_obj['first_name'] = register_data.get('first_name')
        user_create_obj['is_active'] = False
        user = User.objects.create_user(**user_create_obj)

        trail_obj = PaidType.objects.get(name='Trial')
        acc_create_obj['user'] = user
        acc_create_obj['paid_type'] = trail_obj
        acc_create_obj['language'] = register_data.get('language')
        acc_create_obj['confirmation_code'] = \
            utils.generate_confirmation_code(user)
        # Confirmation code expire after 30 minutes
        acc_create_obj['code_reset_time'] = \
            datetime.now(timezone.utc) + timedelta(minutes=30)
        AccountInfo.objects.create(**acc_create_obj)
        send_successed = utils.send_confirmation_email(user, False)
        if send_successed:
            return Response({'success': _('User has successfully created. ' +
                            'Please check email for account validation')},
                            status=HTTP_201_CREATED)
        else:
            return \
                Response({'success': _('User has successfully created. ' +
                                       'Email sent failed. Please resend ' +
                                       'the email to validate ' +
                                       'the account')},
                         status=HTTP_201_CREATED)
    return Response({'errors': _('User name has existed')},
                    status=HTTP_403_FORBIDDEN)
Exemplo n.º 12
0
    def create(self, request):
        '''Create chatbot

        Steps:
            1. Check if the chatbot amount is over the limitation. If the
                limitation is 0 means the user has unlimited amount.
            2. Check if chatbot required keys are provided
            3. Assign the thirdparty demo to the bot with trail paidtype
            4. Create the bot and make sure NLU trains model succeed. If not,
                delete the whole chatbot.
        '''
        if request.body:
            user_obj = request.user
            acc_obj = AccountInfo.objects.filter(user=user_obj).first()
            # Check if bot creation is over upper limit
            bot_create_limit = acc_obj.paid_type.bot_amount
            bot_owned_amount = \
                Chatbot.objects.filter(user=user_obj,
                                       hide_status=False).count()
            if acc_obj.paid_type.user_type != 'S' and\
               bot_owned_amount >= int(bot_create_limit):
                return Response({'errors': _('Reach bot create limitation')},
                                status=HTTP_403_FORBIDDEN)

            # Check if faq creation is over upper limit
            faq_create_limit = acc_obj.paid_type.faq_amount
            faq_owned_amount = 0
            bots = Chatbot.objects.filter(user=user_obj, hide_status=False)
            for bot in bots:
                faq_count = FAQGroup.objects.filter(chatbot=bot).count()
                faq_owned_amount += faq_count
            if acc_obj.paid_type.user_type != 'S' and\
               (faq_owned_amount + 1) >= int(faq_create_limit):
                return Response({'errors': _('Reach faq create limitation')},
                                status=HTTP_403_FORBIDDEN)

            bot_data = json.loads(request.body)
            bot_keys = [
                'robot_name', 'greeting_msg', 'failed_msg', 'postback_title',
                'language'
            ]
            err_msg, key_status = utils.key_validator(bot_keys, bot_data)
            if not key_status:
                return Response({'errors': _(err_msg)},
                                status=HTTP_403_FORBIDDEN)
            bot_data['user_id'] = user_obj.id
            bot_obj = Chatbot.objects.create(**bot_data)
            if bot_obj:
                bot_obj.vendor_id = utils.generate_uuid(
                    str(bot_obj.id), bot_obj.robot_name)
                paid_type = acc_obj.paid_type
                for party in paid_type.third_party.all():
                    BotThirdPartyGroup.objects.create(chatbot=bot_obj,
                                                      third_party=party)
                bot_obj.save()
                Line.objects.create(chatbot=bot_obj)
                Facebook.objects.create(chatbot=bot_obj)
                nlumodel.initial_question_answer(bot_obj)
                nlu_create_status, err_msg = nlumodel.create_model(bot_obj)
                # TODO: Comment this comment after the NLU has setup
                create_bot_obj = \
                    utils.delete_create_failed_model(nlu_create_status,
                                                     bot_obj)
                # TODO: Comment create_bot_obj = bot_obj
                # create_bot_obj = bot_obj
                if not create_bot_obj:
                    return Response(
                        {
                            'errors':
                            _('Create bot failed. ' + 'Cause by NLU error.' +
                              err_msg)
                        },
                        status=HTTP_400_BAD_REQUEST)
                res = {}
                res['id'] = create_bot_obj.id
                res['robot_name'] = create_bot_obj.robot_name
                return Response(res, status=HTTP_201_CREATED)
            return Response({'errors': _('Create bot failed')},
                            status=HTTP_400_BAD_REQUEST)
        return Response({'errors': _('No content')},
                        status=HTTP_400_BAD_REQUEST)