# Path: brasilcomvc/projects/models.py
# class Project(models.Model):
# 
#     owner = models.ForeignKey(
#         settings.AUTH_USER_MODEL, related_name='projects_owned')
#     name = models.CharField(max_length=60)
#     slug = models.SlugField(editable=False)
#     relevant_fact = models.TextField(null=True, blank=True)
#     about = models.TextField()
#     short_description = models.TextField(null=True, blank=True)
#     how_to_help = models.TextField()
#     requirements = models.TextField(blank=True)
#     tags = models.ManyToManyField('Tag', blank=True)
#     agenda = models.TextField(default='', blank=True)
#     address = models.TextField(verbose_name='endereço')
#     location = models.TextField(verbose_name='local', help_text=(
#         'Local ou cidade. E.g. "Teatro Municipal" ou "São Paulo, SP".'))
#     latlng = models.PointField(
#         null=True, srid=4326, db_index=True, editable=False)
# 
#     img = ProcessedImageField(
#         format='JPEG',
#         options={'quality': 80},
#         processors=[ResizeToFill(854, 480)],
#         upload_to=project_img_upload_to)
#     img_thumbnail = ImageSpecField(
#         format='JPEG',
#         options={'quality': 80},
#         processors=[ResizeToFill(320, 240)],
#         source='img')
#     img_opengraph = ImageSpecField(
#         format='JPEG',
#         options={'quality': 80},
#         processors=[ResizeToFill(800, 420)],
#         source='img')
#     video = models.URLField(null=True, blank=True)
# 
#     name.verbose_name = 'nome'
#     relevant_fact.verbose_name = 'fato relevante'
#     about.verbose_name = 'descrição'
#     short_description.verbose_name = 'descrição curta'
#     how_to_help.verbose_name = 'como ajudar'
#     requirements.verbose_name = 'requisitos'
#     video.verbose_name = 'vídeo'
# 
#     class Meta:
#         verbose_name = 'Projeto'
#         verbose_name_plural = 'Projetos'
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('projects:project_details', kwargs={'slug': self.slug})
# 
#     def clean(self):
#         # Use Google Maps API to geocode `address` into `latlng`
#         try:
#             addr = Geocoder.geocode(' '.join(self.address.splitlines()))[0]
#             self.latlng = Point(y=addr.latitude, x=addr.longitude, srid=4326)
# 
#         except GeocoderError as err:
#             if err.status != GeocoderError.G_GEO_ZERO_RESULTS:
#                 raise
#             raise ValidationError('Endereço não reconhecido no Google Maps.')
# 
#     def save(self, **kwargs):
#         if self.pk is None:
#             self.slug = slugify(smart_text(self.name))
#         super(Project, self).save(**kwargs)
# 
# def project_img_upload_to(instance, filename):
#     return 'projects/{}/img.jpeg'.format(instance.slug)


import json
import re
import responses
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.contrib.auth import get_user_model
from pygeocoder import Geocoder, GeocoderError
from ..models import Project, project_img_upload_to
            name='This is a Test Project')
        project.save()
        self.assertEqual(project.slug, 'this-is-a-test-project')
        old_slug = project.slug
        project.name = 'Some New Name'
        project.save()
        self.assertEqual(project.slug, old_slug)

    def test_project_img_upload_to(self):
        project = Project(slug='wat-is-a-slug')
        filename = 'wat.png'
        expected = 'projects/wat-is-a-slug/img.jpeg'
        self.assertEqual(project_img_upload_to(project, filename), expected)

    @responses.activate
    def test_auto_geocode_should_fail_with_invalid_address(self):
        project = Project(address='Th1s iz such an 1nv4l1d @ddress')
        self._mock_pygeocoder_request({
            'results': [],
            'status': GeocoderError.G_GEO_ZERO_RESULTS})
        self.assertRaises(ValidationError, project.clean)
        self.assertIsNone(project.latlng)

    @responses.activate
    def test_auto_geocode_should_work_with_valid_address(self):
        self._mock_pygeocoder_request({
            'results': [
                {'geometry': {'location': {'lat': -23.5528, 'lng': -46.6307}}},
            ],
            'status': GeocoderError.G_GEO_OK})

# Path: brasilcomvc/projects/models.py
# class Project(models.Model):
# 
#     owner = models.ForeignKey(
#         settings.AUTH_USER_MODEL, related_name='projects_owned')
#     name = models.CharField(max_length=60)
#     slug = models.SlugField(editable=False)
#     relevant_fact = models.TextField(null=True, blank=True)
#     about = models.TextField()
#     short_description = models.TextField(null=True, blank=True)
#     how_to_help = models.TextField()
#     requirements = models.TextField(blank=True)
#     tags = models.ManyToManyField('Tag', blank=True)
#     agenda = models.TextField(default='', blank=True)
#     address = models.TextField(verbose_name='endereço')
#     location = models.TextField(verbose_name='local', help_text=(
#         'Local ou cidade. E.g. "Teatro Municipal" ou "São Paulo, SP".'))
#     latlng = models.PointField(
#         null=True, srid=4326, db_index=True, editable=False)
# 
#     img = ProcessedImageField(
#         format='JPEG',
#         options={'quality': 80},
#         processors=[ResizeToFill(854, 480)],
#         upload_to=project_img_upload_to)
#     img_thumbnail = ImageSpecField(
#         format='JPEG',
#         options={'quality': 80},
#         processors=[ResizeToFill(320, 240)],
#         source='img')
#     img_opengraph = ImageSpecField(
#         format='JPEG',
#         options={'quality': 80},
#         processors=[ResizeToFill(800, 420)],
#         source='img')
#     video = models.URLField(null=True, blank=True)
# 
#     name.verbose_name = 'nome'
#     relevant_fact.verbose_name = 'fato relevante'
#     about.verbose_name = 'descrição'
#     short_description.verbose_name = 'descrição curta'
#     how_to_help.verbose_name = 'como ajudar'
#     requirements.verbose_name = 'requisitos'
#     video.verbose_name = 'vídeo'
# 
#     class Meta:
#         verbose_name = 'Projeto'
#         verbose_name_plural = 'Projetos'
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('projects:project_details', kwargs={'slug': self.slug})
# 
#     def clean(self):
#         # Use Google Maps API to geocode `address` into `latlng`
#         try:
#             addr = Geocoder.geocode(' '.join(self.address.splitlines()))[0]
#             self.latlng = Point(y=addr.latitude, x=addr.longitude, srid=4326)
# 
#         except GeocoderError as err:
#             if err.status != GeocoderError.G_GEO_ZERO_RESULTS:
#                 raise
#             raise ValidationError('Endereço não reconhecido no Google Maps.')
# 
#     def save(self, **kwargs):
#         if self.pk is None:
#             self.slug = slugify(smart_text(self.name))
#         super(Project, self).save(**kwargs)
# 
# def project_img_upload_to(instance, filename):
#     return 'projects/{}/img.jpeg'.format(instance.slug)


# Path: brasilcomvc/projects/tests/test_models.py
import json
import re
import responses
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.contrib.auth import get_user_model
from pygeocoder import Geocoder, GeocoderError
from ..models import Project, project_img_upload_to

            name='This is a Test Project')
        project.save()
        self.assertEqual(project.slug, 'this-is-a-test-project')
        old_slug = project.slug
        project.name = 'Some New Name'
        project.save()
        self.assertEqual(project.slug, old_slug)

    def test_project_img_upload_to(self):
        project = Project(slug='wat-is-a-slug')
        filename = 'wat.png'
        expected = 'projects/wat-is-a-slug/img.jpeg'
        self.assertEqual(project_img_upload_to(project, filename), expected)

    @responses.activate
    def test_auto_geocode_should_fail_with_invalid_address(self):
        project = Project(address='Th1s iz such an 1nv4l1d @ddress')
        self._mock_pygeocoder_request({
            'results': [],
            'status': GeocoderError.G_GEO_ZERO_RESULTS})
        self.assertRaises(ValidationError, project.clean)
        self.assertIsNone(project.latlng)

    @responses.activate
    def test_auto_geocode_should_work_with_valid_address(self):
        self._mock_pygeocoder_request({
            'results': [
                {'geometry': {'location': {'lat': -23.5528, 'lng': -46.6307}}},
            ],
            'status': GeocoderError.G_GEO_OK})

        project = Project(address='Sé, São Paulo, Brasil')