Hi, just a little follow up on this topic, I've set up a git to work on a
custom app called DeviceIP.

you can check my progress here
<https://github.com/joaohcca/openwisp-controller/tree/device-ip-app>


   1. I've added the openwisp_ipam and my DeviceIP app to the
   openwisp-controller project.
   2. Added some comments on what I'm planning to do with my signals.py (to
   help with the logic setup)
   3. Added some code on models.py to track devices and IP addresses.

I'll try to work on the logic for the signals implementation but I believe
the comments I've made should cover all the cases.

Let me know what you guys think it could be improved or suggestions for the
code implementation

Cheers,


Em qui., 6 de fev. de 2020 às 17:21, Federico Capoano <
[email protected]> escreveu:

> Sorry, I meant one ForeignKey to the Device model of openwisp-controller
> and one to the Ip model of openwisp-ipam.
> You may as well create an admin inline and add it to the DeviceAdmin
> dynamically (it's called monkey patching) so you'll be able to see this in
> the django admin.
>
> Here's an example of how to listen for signals in a django project and
> openwisp.
>
> from django.apps import AppConfig
> from django.db.models.signals import post_save
>
> class ExampleApp(AppConfig):
>     name = 'openwisp2.example_app'
>     label = 'example_app'
>
>
>     def ready(self):
>         from openwisp_controller.config.models import Device
>
>
>         post_save.connect(self.device_saved_receiver,
>                           sender=Device,
>                           dispatch_uid='device_saved_receiver')
>
>
>     @classmethod
>     def device_saved_receiver(cls, sender, instance, created, **kwargs):
>         # if not created:
>         #     False
>         # do something
>         pass
>
>     @classmethod
>     def device_deleted_receiver(cls, sender, instance, **kwargs):
>         # may want to do something on delete
>         pass
>
>
> See https://docs.djangoproject.com/en/3.0/ref/applications/.
> There's different way of doing this depending on how you want to do it.
> If you do create a custom django app mixing openwisp-controller and
> openwisp-ipam you may upload it on github and we'll help you out there as
> well.
>
> Alternatively, you can look for a simpler solution, in that case, you just
> need to place that file in the openwisp project folder (eg
> /opt/openwisp2/openwisp2 when using ansible-openwisp2) and then add to the
> INSTALLED_APPS the following: "openwisp2.example_app.ExampleApp".
>
> Federico
>
>
> On Thursday, February 6, 2020 at 2:37:46 PM UTC+1, João Henrique
> Albuquerque wrote:
>>
>> Just for clarifcation did you mean two ForeignKey pointing to the Device ?
>> Shouldn't one be pointing to device config or device context?
>>
>> Creating a new django app in which you add a new model, eg: DeviceIp, in
>>> which you add a ForeignKey field pointing to Device and a ForeignKey
>>> pointing to Device, the two fields should be flagged as unique_together
>>> (look all these terms on the django documentation).
>>>
>>> Then add a signal receiver function in this new app which listens to
>>> save events on Device, return if the "created" parameter is False so that
>>> you act only on created devices, and in this function you add the logic to
>>> write to the context field of the Device. There should be a method in
>>> django-ipam to get the first available ip from a subnet.
>>>
>>> Then you should decide also how to manage subnets, if all devices are on
>>> the same subnet is easy, otherwise you should probably have a way to tell
>>> the system to which subnet the device pertains to.
>>>
>>> Thanks
>>> Federico
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "OpenWISP" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/openwisp/80494464-8f8c-407a-aaa8-9f837c350658%40googlegroups.com
> <https://groups.google.com/d/msgid/openwisp/80494464-8f8c-407a-aaa8-9f837c350658%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"OpenWISP" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/openwisp/CAO5jQiafQWUufRTJyEj6riAT0aZeNRG7f48XntYyx3zrKJfkKA%40mail.gmail.com.

Reply via email to