P
P
pgamesorg2017-05-19 18:12:21
Django
pgamesorg, 2017-05-19 18:12:21

How to inject third-party code into a Django project?

Hi all!
As I learn Django, I am making a project.
And so I studied the view models templates, well, for now, the basics, and such a problem arose after adding a model that contains a field with ip, Port, protocol, etc.
I need to be able to receive data from a remote device whose address is specified in the model
The code itself that it receives is there, but how to implement it in Django?
As a result, I should just get a json file from that device
, here is this third-party code

import requests
import json

__BASE_URL__ = "http://194.67.209.244:8080/api/"
__HOST_URL__ = __BASE_URL__ + "host"
__USER_URL__ = __BASE_URL__ + "user"
__ARCH_URL__ = __BASE_URL__ + "arch"

class IoTDevices(object):
    def __init__(self, name=None, id = None):
        if name is not None:
            self.name = name
        else:
            self.name = ""
        if id is not None:
            self.id = id
        else:
            self.id = 0
        self.protocol = ""
        self.port = 9090
        self.emac = ""
        self.ip_address = "0.0.0.0"
        self.type = ""
        self.option = {}
        self.items = {}
        self.description = ""
        # Загружаем данные с сервера
        self.load_devais(to_name=(self.id == 0))

    def load_devais(self, to_name=True):
        if to_name:
            _param = self.hosts_request(method="host.get", name=self.name)
        else:
            _param = self.hosts_request(method="host.get", id=self.id)
        self.result = _param["result"]
        if self.result == "OK":
            self._response =_param["response"][0]
            self.name = self._response["name"]
            self.id = self._response["id"]
            self.type = self._response["type"]
            self.ip_address = self._response["ip_address"]
            self.protocol = self._response["protocol"]
            self.port = self._response["port"]
            self.emac = self._response["emac"]
            self.option = self._response["option"]
            self.items = self._response["items"]
            self.description = self._response["description"]
        else:
            # TODO
            """
                Описать ошибку
            """
            raise Exception()

    def load_arch(self, time_from=None, time_to=None, select=0):
        d = {}
        if time_from is not None:
            d.update({"time_from":time_from})
        if time_to is not None:
            d.update({"time_to":time_to})
        if select is not None:
            d.update({"select": select})
        d.update({"name": self.name})
        return self.arch_request(method="arch.filtre", param=d)

    def get_items(self, item=None):
        raw = self.hosts_request("host.items", id=self.id)
        res = raw["result"]
        if res == "OK":
            response = raw["response"]
            self.items = response
        if item is None:
            return self.items
        else:
            return self.items.get(item)

    def param_host(self,param):
        param.update({"id":self.id})
        self.hosts_request("host.update", param)
        self.load_devais(False)

    def hosts_request(self, method, param=None, **kwargs):
        return self.request(method, "host", param=param, **kwargs)

    def user_request(self, method, param=None, **kwargs):
        return self.request(method, "user", param=param, **kwargs)

    def arch_request(self, method, param=None, **kwargs):
        return self.request(method, "arch", param=param, **kwargs)

    def request(self,method,system, param=None, **kwargs):
        if param is not None:
            r = requests.post(url=__BASE_URL__+system, data=json.dumps({"method": method, "param": param}))
            _raw = r.text
            j =json.loads(_raw)
        else:
            r = requests.post(url=__BASE_URL__+system, data=json.dumps({"method": method, "param": kwargs}))
            _raw = r.text
            j = json.loads(_raw)

        return j

    def __str__(self):
        s = "id = %s\n"%self.id
        s = "%sname = %s\n" % (s, self.name)
        s = "%sp_address = %s\n" % (s, self.ip_address)
        s = "%sport = %s\n" % (s, self.port)
        s = "%semac = %s\n" % (s, self.emac)
        s = "%stype = %s\n" % (s, self.type)
        s = "%sprotocol = %s\n" % (s, self.protocol)
        s = "%sitems = %s\n" % (s, self.items)
        s = "%soption = %s\n" % (s, self.option)
        s = "%sdescription = %s\n" % (s, self.description)
        return s

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2017-05-19
@sim3x

https://docs.djangoproject.com/en/1.11/howto/custo...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question