Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: overriding attrs (settings_overrides) #120

Closed
Talkless opened this issue Sep 26, 2015 · 16 comments
Closed

Question: overriding attrs (settings_overrides) #120

Talkless opened this issue Sep 26, 2015 · 16 comments
Labels

Comments

@Talkless
Copy link

I want to override LEAFLET_CONFIG for specific custom widget, which would be used in specific LeafletGeoAdmin.

Is it possible to do that without reimplementing ModelForm? Here how it's looks currently:
my widgets.py:

from leaflet.forms.widgets import LeafletWidget

class ClientLocationWidget(LeafletWidget):    
    def __init__(self, *args, **kwargs):      
        attrs_override = {
                          'settings_overrides': 
                          { 
                           'DEFAULT_CENTER': (55.92934, 23.311752),
                           'DEFAULT_ZOOM': 12
                           }
                        }
        super().__init__(attrs = attrs_override) 
        #what should I do with original *args, **kwargs?

my admin.py:

class ClientAdmin(LeafletGeoAdmin):
    widget = ClientLocationWidget
    #all other mundane stuff...

But it has no effect.

@submarcos
Copy link
Member

Hi,
thanks for your interest in django-leaflet,
I think you should extends the original init widget method, (LeafletWidget extends BaseGeometryWidget), and its init method looks like :

def __init__(self, attrs=None):
    self.attrs = {}
    for key in ('geom_type', 'map_srid', 'map_width', 'map_height', 'display_raw'):
        self.attrs[key] = getattr(self, key)
    if attrs:
        self.attrs.update(attrs)

But, in your example, to fix original center and zoom level you just set settings file.

You can also write your own widget, with its own javascript, which takes your parameters to do leaflet actions (set custom center, custom zoom, custom rendering)

@leplatrem
Copy link
Collaborator

+1

You could try:

class ClientLocationWidget(LeafletWidget):    
    def __init__(self, attrs=None):      
        if attrs is None:
            attrs = {}
        attrs.update({
                          'settings_overrides': 
                          { 
                           'DEFAULT_CENTER': (55.92934, 23.311752),
                           'DEFAULT_ZOOM': 12
                           }
                        })
        super(ClientLocationWidget, self).__init__(attrs=attrs) 

/cc @PetrDlouhy :)

@Talkless
Copy link
Author

@leplatrem Thanks, but settings_overrides still has no effect.

@submarcos In documentation, there is example where you can change attrs:

from leaflet.forms.widgets import LeafletWidget
class WeatherStationForm(forms.ModelForm):

    class Meta:
        model = WeatherStation
        fields = ('name', 'geom')
        widgets = {'geom': LeafletWidget(attrs={
            'settings_overrides': {
                'DEFAULT_CENTER': (6.0, 45.0),
            }
        })}

I just thought maybe I could override DEFAULT_CENTER and other parameters without creating custom form class, which would be used in LeafletGeoAdmin. I just wanted inherit LeafletWidget with minor changes, but not sure how to make it work.

I know I can set global settings in settings.py, but Idea was to have different widgets for different purposes with different default settings, ignoring default ones.

@Talkless
Copy link
Author

I tried example with custom Form class I mentioned in last post, it simply does not work.

I think it's simply a bug.

After searching for "settings_overrides" in repository, it's note quite appearent how it could be actually passed as tag argument from widget class, unless you pass it manually to tag inside template.

But I can be wrong of course. Could someone please try that widgets = {'geom': LeafletWidget(attrs={ example..?

@leplatrem
Copy link
Collaborator

@Talkless I am sorry that you encounter those problems. Unfortunately I just can't spend time on it.

I cannot say if this feature works the way it is described. Maybe @PetrDlouhy could help us finding out :)

Please don't hesitate to hack around, open a pull-request with some fixs, it is always going to be easier to discuss with some code :) You could even write a small test to reproduce what you are trying to achieve, that would be the most efficient :)

@PetrDlouhy
Copy link
Contributor

My working usage is this:

class UserLeafletWidget(LeafletWidget):
    def __init__(self, *args, **kwargs):
        user_attendance = kwargs['user_attendance']
        settings_overrides = {}
        if user_attendance.team and user_attendance.team.subsidiary.city.location:
            settings_overrides['DEFAULT_CENTER'] = (user_attendance.team.subsidiary.city.location.y, user_attendance.team.subsidiary.city.location.x)
            settings_overrides['DEFAULT_ZOOM'] = 13

        super(UserLeafletWidget, self).__init__(
            attrs={
                "geom_type":'LINESTRING',
                "map_height":"500px",
                "map_width":"100%",
                'settings_overrides':settings_overrides,
            }
        )

The working unit test in code is this:

class SettingsOverridesTest(SimpleTestCase):

    def test_settings_overrides(self):
        widget = LeafletWidget(attrs={
            'settings_overrides': {
                'DEFAULT_CENTER': (8.0, 3.14),
            }
        })
        output = widget.render('geom', '', {'id': 'geom'})
        self.assertIn('"center": [8.0, 3.14]', output)

@Talkless: Are you sure, that the super() notation without any parameter is correct?

@Talkless
Copy link
Author

Talkless commented Oct 6, 2015

@PetrDlouhy I'm pretty sure in Python 3 you can skip arguments, check first example: https://docs.python.org/3/library/functions.html#super

Thanks for the input, I'll try again later.

@PetrDlouhy
Copy link
Contributor

@Talkless OK, I don't work in Python 3, so I was confused.
Have you tried running tests with Python 3?

Maybe it would be helpful to set up TOX testing for Django-leaflet

@PetrDlouhy
Copy link
Contributor

I overlooked, that the tests are here and running. So it is tested in Python 3.

Though it still doesn't prove, that the issue is not connected with Python 3. Try running your application on Python 2 (if possible).

@Talkless
Copy link
Author

Talkless commented Oct 8, 2015

How could I run tests? quicktest.py asks for some kind app..?

@PetrDlouhy
Copy link
Contributor

You can run it with python quicktest.py leaflet, but I gues it will run OK, since it is tested with Travis.

@leplatrem
Copy link
Collaborator

It would be worth documenting this indeed. As @dyve said, without tests it's no fun :)

@leplatrem
Copy link
Collaborator

@Talkless could you eventually sort this out ?

Thanks for letting others know :)

@Talkless
Copy link
Author

Talkless commented Nov 5, 2015

No. Sorry, I've been sick for a while, and just can't find time to produce simple test app :( .

@Talkless
Copy link
Author

Talkless commented Nov 7, 2015

python quicktest.py leaflet --db=postgres passed all the tests with Python 3.4.3 I am using.

I've created minimal app for reproducing my issue:
https://github.com/Talkless/django-leaflet-test

Please check leaflet_admin app.
It has single LeafletTestModel.
There is registered LeafletTestAdmin witch uses custom LeafletTestForm.
Inside that form, I try to override LeafletWidget attributes so it would change default location and zoom, just like in README form example... Though these changes has no effect when trying editing model with Django admin.

@PetrDlouhy
Copy link
Contributor

The problem is, that if you set the widget with LeafletGeoAdmin, the widget settings are ignored because the admin instantiate the widget itself. You have to use normal ModelAdmin and properly set template (I haven't try that).

I have added the possibility to set settings_overrides directly in LeafletGeoAdmin, which is in PR #134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants