<commit_before>from django import template

from molly.apps.weather.models import Weather
from molly.conf.applications import app_by_application_name

register = template.Library()

@register.tag
def weather(parser, token):
    return WeatherNode(*token.split_contents()[1:])
    
class WeatherNode(template.Node):
    """
    Adds a WeatherNode instance to the context.
    """
    
    def __init__(self, name="weather"):
        self.name = name
        
    def render(self, context):
        # Voodoo ahead! Gets the location ID from the molly settings file to use when accessing Weather object.
        context[self.name] = Weather.objects.get(ptype='o', location_id=app_by_application_name('molly.apps.weather').location_id)
        return ''<commit_msg>Weather template tag no longer falls over when no Weather object is available.<commit_after>from django import template

from molly.apps.weather.models import Weather
from molly.conf.applications import app_by_application_name

register = template.Library()

@register.tag
def weather(parser, token):
    return WeatherNode(*token.split_contents()[1:])
    
class WeatherNode(template.Node):
    """
    Adds a WeatherNode instance to the context.
    """
    
    def __init__(self, name="weather"):
        self.name = name
        
    def render(self, context):
        # Voodoo ahead! Gets the location ID from the molly settings file to use when accessing Weather object.
        try:
            context[self.name] = Weather.objects.get(ptype='o', location_id=app_by_application_name('molly.apps.weather').location_id)
        except Weather.DoesNotExist:
            context[self.name] = None
        return ''
{'fraction_non_alphanumeric': 0.09529481834425253, 'fraction_numerical': 0.0011911852293031567, 'mean_word_length': 3.4919786096256686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2367268', 'n_tokens_mistral': 484, 'n_tokens_neox': 463, 'n_words': 133}