예제 #1
0
파일: views.py 프로젝트: natea/fixcity
def source_factory(data):
    """Given something like a request.POST, decide which kind of Source
    to create, if any.

    Returns a tuple of (Source, created) where created is a boolean:
    True if the source was created anew, False if it already existed.
    """
    if data.get('source'):
        source = Source.objects.filter(id=data['source']).all()
        assert len(source) == 1, \
               "Unexpectedly got %d sources with id %s" % (len(source), data['source'])
        source = source[0]
        # Try to get a more specific subclass instance.
        source = source.get_child_source() or source
        return (source, False)

    source = None
    source_type = data.get('source_type')
    if source_type == 'twitter':
        source = TwitterSource(name='twitter', user=data['twitter_user'],
                               status_id=data['twitter_id'])
    elif source_type == 'email' and data.has_key('email'):
        source = EmailSource(name='email', address=data['email'])
    # XXX handle SeeClickFixSource here
    if source:
        # We need to save it to get the ID.  This means we'll need to
        # roll back the transaction if there are later validation
        # problems in the Rack.
        source.save()
    return (source, source is not None)
예제 #2
0
 def test_get_child_source(self):
     from fixcity.bmabr.models import TwitterSource
     ts = TwitterSource(name='twitter', user='******', status_id='99')
     ts.save()
     from fixcity.bmabr.models import Source
     generic_source = Source.objects.filter(id=ts.id).all()[0]
     self.assertEqual(generic_source.twittersource, ts)
     self.assertEqual(generic_source.get_child_source(), ts)
예제 #3
0
    def test_existing_source(self):
        from fixcity.bmabr.models import Source, TwitterSource
        from fixcity.bmabr.views import source_factory
        existing = Source()
        existing.name = 'misc source'
        existing.save()
        dupe, is_new = source_factory({'source': existing.id})
        self.assertEqual(dupe, existing)
        self.failIf(is_new)

        # It should work also with subclasses of Source...
        twit = TwitterSource(status_id=12345, name='twitter')
        twit.save()
        self.assertEqual((twit, False), source_factory({'source': twit.id}))