コード例 #1
0
def send_url(request):
    try:
        if "CONTENT_TYPE" in request.META:
            content_type = "CONTENT_TYPE"
        else:
            content_type = "HTTP_CONTENT_TYPE"
        if request.META[content_type] == RP_RESPONSE_CONTENT_TYPES[
                "URL_ENCODED"]:
            data = request.data
            content = data.dict()
        else:
            content = request.data
        access_logger.info(f"From rapidPro: {content}")
        # decrement key1
        to = content['to_no_plus']
        key1 = f"MO_MT_KEY_{to}"
        key2 = f"MSG_KEY_{to}"
        key1_val = int(r.decr(key1))
        if key1_val >= 0:
            r.lpush(key2, str(content))
        else:
            action = "end"
            # code here is currently temporary
            if content["session_status"] == RP_RESPONSE_STATUSES["waiting"]:
                action = "request"
            msg_extras = dict(msg_type="Outgoing",
                              status="Received",
                              action=action)
            content.update(msg_extras)
            push = push_ussd(content, request)
            # reset key to 1
            r.set(key1, 0, ex=10)
        return Response({"message": "success"}, status=200)
    except Exception as err:
        error_logger.exception(err)
コード例 #2
0
    def post(self, request, handler_id=None):
        access_logger.info(request)
        try:
            if handler_id:
                handler = Handler.objects.get(pk=handler_id)
                self.auth_scheme = handler.auth_scheme
                self.aggregator = handler.aggregator
                self.get_auth_token()
                self.isNewHandler = False
                form = HandlerForm(request.POST, instance=handler)
            else:
                form = HandlerForm(request.POST)
            if form.is_valid():
                form.save()
                # create new non-staff user and assign them an auth token
                if self.isNewHandler:
                    aggregator = form.cleaned_data["aggregator"]
                    self.create_auth_user(aggregator)

                self.msg = 'Saved'
                self.success = True
            else:
                self.msg = 'Form is invalid'
                self.success = False
            return render(request, self.template_name,
                          {"form": form, "msg": self.msg, "token": self.token, "success": self.success,
                           "auth_scheme": self.auth_scheme})

        except Exception as err:
            self.success = False
            error_logger.exception(err)
            self.msg = str(err)
            return render(request, self.template_name,
                          {"form": form, "msg": self.msg, "token": self.token, "success": self.success,
                           "auth_scheme": self.auth_scheme})
コード例 #3
0
 def post(self, request, *args, **kwargs):
     access_logger.info(request)
     try:
         if "channel_id" in kwargs:
             channel_id = kwargs['channel_id']
             channel = USSDChannel.objects.get(pk=channel_id)
             form = ChannelConfForm(request.POST, instance=channel)
         else:
             form = ChannelConfForm(request.POST)
         if form.is_valid():
             form.save()
             self.msg = 'Channel configurations  successfully saved.'
             self.success = True
             return redirect(reverse("channel_list"))
         else:
             self.msg = 'Form is not valid'
         return render(
             request, self.template_name, {
                 "form": form,
                 "msg": self.msg,
                 "success": self.success,
                 "hostname": self.hostname
             })
     except Exception as err:
         error_logger.exception(err)
コード例 #4
0
 def get(self, request):
     access_logger.info(request.META)
     channels = USSDChannel.objects.all()
     if len(channels) > 0:
         channel = channels[0]
         form = ChannelConfForm(instance=channel)
     else:
         form = ChannelConfForm()
     return render(request, self.template_name, {"form": form, "msg": self.msg, "success": self.success,
                                                 "hostname": self.hostname})
コード例 #5
0
 def get(self, request, *args, **kwargs):
     access_logger.info(request)
     if "channel_id" in kwargs:
         channel_id = kwargs['channel_id']
         if USSDChannel.objects.filter(pk=channel_id).exists():
             channel = USSDChannel.objects.get(pk=channel_id)
         else:
             return redirect(reverse('channel_conf'))
         form = ChannelConfForm(instance=channel)
     else:
         form = ChannelConfForm()
     return render(
         request, self.template_name, {
             "form": form,
             "msg": self.msg,
             "success": self.success,
             "hostname": self.hostname
         })
コード例 #6
0
 def post(self, request):
     access_logger.info(request.META)
     try:
         channels = USSDChannel.objects.all()
         if len(channels) > 0:
             channel = channels[0]
             form = ChannelConfForm(request.POST, instance=channel)
         else:
             form = ChannelConfForm(request.POST)
         if form.is_valid():
             form.save()
             self.msg = 'Channel configurations  successfully saved.'
             self.success = True
             return redirect("/")
         else:
             self.msg = 'Form is not valid'
         return render(request, self.template_name,
                       {"form": form, "msg": self.msg, "success": self.success, "hostname": self.hostname})
     except Exception as err:
         error_logger.exception(err)
コード例 #7
0
ファイル: views.py プロジェクト: unicef/ussd-external-channel
 def get(self, request, handler_id=None):
     access_logger.info(str(request))
     try:
         if handler_id:
             if Handler.objects.filter(id=handler_id).exists():
                 handler = Handler.objects.get(pk=handler_id)
                 self.aggregator = handler.aggregator
                 form = HandlerForm(instance=handler)
                 self.get_auth_token()
             else:
                 form = HandlerForm()
                 redirect('add_handler', permanent=True)
         else:
             form = HandlerForm()
         return render(request, self.template_name, {
             "form": form,
             "token": self.token
         })
     except Exception as err:
         error_logger.exception(err)
コード例 #8
0
    def get(self, request, handler_id=None):
        access_logger.info(str(request))
        try:
            if handler_id:
                if Handler.objects.filter(id=handler_id).exists():
                    handler = Handler.objects.get(pk=handler_id)
                    self.callback_url = f"{handler.channel.send_url}/adaptor/call-back"
                    self.auth_scheme = handler.auth_scheme
                    self.aggregator = handler.aggregator
                    form = HandlerForm(instance=handler)
                    self.get_auth_token()
                else:
                    form = HandlerForm()
                    redirect('add_handler', permanent=True)
            else:
                form = HandlerForm()
            return render(request, self.template_name,
                          {"form": form, "token": self.token, "auth_scheme": self.auth_scheme,
                           "callback_url": self.callback_url})

        except Exception as err:
            error_logger.exception(err)
コード例 #9
0
def login_view(request):
    form = LoginForm(request.POST or None)

    msg = None

    if request.method == "POST":

        if form.is_valid():
            username = form.cleaned_data.get("username")
            password = form.cleaned_data.get("password")
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                access_logger.info(f"User {request.user.username} logged in at {timezone.now()}")
                return redirect("/")
            else:
                msg = 'Invalid credentials'
                access_logger.debug(f"User {username} login Failed at {timezone.now()}")
        else:
            msg = 'Error validating the form'

    return render(request, "accounts/login.html", {"form": form, "msg": msg})
コード例 #10
0
 def get(self, request, *args, **kwargs):
     access_logger.info(str(request))
     handlers = Handler.objects.all()
     return render(request, self.template_name, {"handlers": handlers})
コード例 #11
0
    def process_request(self):
        try:
            current_session = self.request_factory.log_session()
            is_new_session = self.request_factory.is_new_session
            still_in_flow = self.request_factory.is_in_flow
            handler = self.request_factory.get_handler
            end_action = handler.signal_end_string
            reply_action = handler.signal_reply_string
            urn = self.standard_request_string['from']
            channel = handler.channel

            if is_new_session:
                text = handler.repeat_trigger if still_in_flow else handler.trigger_word
            else:
                text = self.standard_request_string["text"].strip()
            allowed_urn = standard_urn(urn, handler)

            rapid_pro_request = {"from": allowed_urn, "text": text}
            access_logger.info(
                f"Is new session: {is_new_session}, Still in flow: {still_in_flow}"
            )
            access_logger.info(rapid_pro_request)
            # create redis keys
            key1 = f"MO_MT_KEY_{allowed_urn}"
            r.set(key1, 1)  # proven necessary or else
            key2 = f"MSG_KEY_{allowed_urn}"
            """""Channel details """ ""

            # receive_url is used to send msgs to rapidPro
            receive_url = channel.rapidpro_receive_url
            req = requests.post(receive_url,
                                rapid_pro_request,
                                headers=HEADERS)
            if req.status_code == 200:
                # increment key1
                r.incr(key1)
                r.expire(key1, 30)  # expire key1 after 30s
                data = r.blpop(
                    key2, channel.timeout_after
                )  # wait for configured time for rapidPro instance
                if data:
                    feedback = literal_eval(
                        data[1].decode("utf-8"))  # from RapidPro
                    text = feedback[RP_RESPONSE_FORMAT['text']]
                    status = feedback[RP_RESPONSE_FORMAT['session_status']]
                    access_logger.info(f"From redis key:  {data}")
                    if status == RP_RESPONSE_STATUSES['waiting']:
                        action = reply_action
                    else:
                        # mark session complete and give it a green badge
                        changeSessionStatus(current_session,
                                            SESSION_STATUSES['COMPLETED'],
                                            'success')
                        action = end_action
                    new_format = dict(text=text, action=action)
                    response = self.request_factory.get_expected_response(
                        new_format)
                else:
                    # mark session timed out and give it a red badge
                    changeSessionStatus(current_session,
                                        SESSION_STATUSES['TIMED_OUT'],
                                        'danger')
                    error_logger.debug(
                        f"Response timed out for redis key {key2}")
                    res_format = dict(text="Response timed out",
                                      action=end_action)
                    '''
                    We need to delete this contact as it will delete-cascade(which ever way they say it) sessions attached to it.
                    Next time user comes back, they will submit a trigger word
                    '''
                    contact = Contact.objects.get(urn=urn)
                    contact.delete()
                    response = self.request_factory.get_expected_response(
                        res_format)
                r.delete(key2)  # lets delete the key
            else:
                changeSessionStatus(current_session,
                                    SESSION_STATUSES['TIMED_OUT'], 'danger')
                res_format = dict(text="External Application error",
                                  action=end_action)
                contact = Contact.objects.get(urn=urn)
                contact.delete()
                error_logger.exception(req.content)
                response = self.request_factory.get_expected_response(
                    res_format)
            return Response(response, status=200)
        except Exception as err:
            error_logger.exception(err)
            response = {
                "responseString": "External Application error",
                "action": "end"
            }
            return Response(response, status=500)