Thanks Steve, that was very helpful!  I ended up updating the views that
render the events to call use_editable and removing it from the model
method.

Would there be any downsides to updating use_editable so that it only ever
can be called once per request, something like:

    def use_editable(self):        request = current_request()

        if not hasattr(request, 'settings_editable'):

            self._loaded = __name__ not in getattr(self,
"INSTALLED_APPS")            self._editable_cache = {}

            request.settings_editable = True

Thanks,
Josh


On Sat, Sep 6, 2014 at 2:02 AM, Stephen McDonald <[email protected]> wrote:

> There are a bunch of ways you could approach it, but fundamentally you
> need to not call use_editable inside a loop, which is what you've got right
> now. Instead you need to somehow call it once per request, have the correct
> settings come out, and make those available somehow to every event object
> in your loop.
>
> One way to do it would be to add those two settings
> to TEMPLATE_ACCESSIBLE_SETTINGS, then change the two methods into template
> tags - the template tag function will have access to the template context,
> which contains the variable named "settings" containing everything exposed
> by TEMPLATE_ACCESSIBLE_SETTINGS after having called use_editable (see
> mezzanine.core.context_processors.settings).
>
>
> On Sat, Sep 6, 2014 at 1:13 AM, Josh Cartmell <[email protected]>
> wrote:
>
>> I have a site that on some pages iterates over events, creating email and
>> facebook share links.  These links are created via models on the method,
>> i.e.:
>> def facebook_share_url(self):
>>     settings.use_editable()
>>     request = current_request()
>>     host = request.get_host()
>>     if self.featured_image:
>>         image = '%s%s%s' % (host, settings.MEDIA_URL, self.featured_image)
>>     else:
>>         image = ''
>>     full_path = host + self.get_absolute_url()
>>     link = "https://www.facebook.com/dialog/feed?app_id="; +
>> settings.FACEBOOK_APP_ID + "&link=http%3A%2F%2F" + full_path +
>> "&picture=http%3A%2F%2F" + image + "&name=" + self.title +
>> "&redirect_uri=http%3A%2F%2F" + full_path
>>     return link
>>
>> def email_share_url(self):
>>     settings.use_editable()
>>     request = current_request()
>>     host = request.get_host()
>>     full_path = host + self.get_absolute_url()
>>     site_title = '%s%s' % (settings.SITE_TITLE, ' Calendar')
>>     link = "mailto:?subject="; + urllib.quote(site_title) + "%20-%20" +
>> self.title + "!&body=http%3A%2F%2F" + full_path
>>     return link
>>
>> Then in the templates there are things like:
>> {{ event.email_share_url }}
>> {{ event.facebook_share_url }}
>>
>> Today I noticed that this is extremely inefficient, a page with 3 events
>> (each method was called 3 times, I checked to be sure that was the case) on
>> it used 547 SQL queries.  Simply removing settings.use_editable() reduced
>> the page to 31 queries.
>>
>> The 547 queries are mostly like:
>> *SELECT* ••• <http://127.0.0.1:8000/calendar/month/2014/09/#> *FROM*
>> "django_site" *WHERE* "django_site"."domain" *LIKE* '127.0.0.1:8000'
>> *ESCAPE* '\'
>> *SELECT* ••• <http://127.0.0.1:8000/calendar/month/2014/09/#> *FROM*
>> "conf_setting" *INNER JOIN* "conf_setting_sites" *ON*
>> ("conf_setting"."id" = "conf_setting_sites"."setting_id") *WHERE*
>> "conf_setting_sites"."site_id" = 1
>>
>> Does anyone have any ideas about how to solve this without removing
>> use_editable()?
>>
>> Thanks!
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Mezzanine Users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Stephen McDonald
> http://jupo.org
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mezzanine Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mezzanine Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to