<commit_before>import re

from django.core import exceptions
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from fluent_contents.models import ContentItem

# This will likely have to be tended as GMaps will change their
#  Share URL format over time.
# See tests.py
SHARE_URL_PATTERN = r'place\/((?P<name>.+)\/)?\@(?P<loc>.+)\/'


@python_2_unicode_compatible
class AbstractMapItem(ContentItem):
    """
    Embeds a Google Map inside an iframe from the Share URL.

    Rather than store the width/height in the DB, update the template
    used or override with CSS.
    """
    share_url = models.URLField(
        help_text='Share URL sourced from Google Maps. '
                  'See https://support.google.com/maps/answer/144361?hl=en',
        verbose_name='Share URL',
        max_length=500,
    )

    place_name = 'Unknown'
    loc = '0, 0'

    class Meta:
        abstract = True
        verbose_name = 'Google Map'

    def clean(self, *args, **kwargs):
        self.parse_share_url()
        super(AbstractMapItem, self).clean(*args, **kwargs)

    def parse_share_url(self):
        """Search the Share URL for place name and lat/lon coordinates."""
        regex = re.compile(SHARE_URL_PATTERN)
        result = regex.search(str(self.share_url))
        if getattr(result, 'groupdict'):
            self.place_name = result.groupdict()['name']
            self.loc = result.groupdict()['loc']
        else:
            raise exceptions.ValidationError('Could not parse map Share URL')

    def __str__(self):
        if self.place_name == 'Unknown':
            self.parse_share_url()
        return '%s @%s' % (self.place_name, self.loc)<commit_msg>Fix in case result is None.<commit_after>import re

from django.core import exceptions
from django.db import models
from django.utils.encoding import python_2_unicode_compatible

from fluent_contents.models import ContentItem

# This will likely have to be tended as GMaps will change their
#  Share URL format over time.
# See tests.py
SHARE_URL_PATTERN = r'place\/((?P<name>.+)\/)?\@(?P<loc>.+)\/'


@python_2_unicode_compatible
class AbstractMapItem(ContentItem):
    """
    Embeds a Google Map inside an iframe from the Share URL.

    Rather than store the width/height in the DB, update the template
    used or override with CSS.
    """
    share_url = models.URLField(
        help_text='Share URL sourced from Google Maps. '
                  'See https://support.google.com/maps/answer/144361?hl=en',
        verbose_name='Share URL',
        max_length=500,
    )

    place_name = 'Unknown'
    loc = '0, 0'

    class Meta:
        abstract = True
        verbose_name = 'Google Map'

    def clean(self, *args, **kwargs):
        self.parse_share_url()
        super(AbstractMapItem, self).clean(*args, **kwargs)

    def parse_share_url(self):
        """Search the Share URL for place name and lat/lon coordinates."""
        regex = re.compile(SHARE_URL_PATTERN)
        result = regex.search(str(self.share_url))
        if result and getattr(result, 'groupdict'):
            self.place_name = result.groupdict()['name']
            self.loc = result.groupdict()['loc']
        else:
            raise exceptions.ValidationError('Could not parse map Share URL')

    def __str__(self):
        if self.place_name == 'Unknown':
            self.parse_share_url()
        return '%s @%s' % (self.place_name, self.loc)
{'fraction_non_alphanumeric': 0.11053089643167972, 'fraction_numerical': 0.007542790832608065, 'mean_word_length': 2.8099447513812157, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '3853029', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1000, 'n_words': 294}