tulsawebdevs / django-multi-gtfs

Django app to import and export General Transit Feed Specification (GTFS)
http://tulsawebdevs.org/
Apache License 2.0
50 stars 32 forks source link

Autonaming breaks for feeds with calendar_dates.txt #74

Open mharvey opened 6 years ago

mharvey commented 6 years ago

When importing a feed with a calendar_dates.txt (The "TriMet Style") with the management command, the import fails with a nasty error when it tries to rename the feed at the very end. The rest of the import seems to work flawlessly.

When providing a name via the -n switch, the name generation is skipped and the error is avoided.

It appears that the Service objects are being created, but with Service.start_date and Service.end_date both nulled. When the importer gets to this part

https://github.com/tulsawebdevs/django-multi-gtfs/blob/f104c09424f9c6b2557db6ef08f40893ee4ea2ed/multigtfs/management/commands/importgtfs.py#L84-L92

the service exists, but service.start_date is None which causes the strftime to puke out this error:

INFO - Import completed in 2512.7 seconds.

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/mharvey/Envs/portal-rewrite/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/Users/mharvey/Envs/portal-rewrite/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/mharvey/Envs/portal-rewrite/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/mharvey/Envs/portal-rewrite/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/mharvey/Envs/portal-rewrite/lib/python3.6/site-packages/multigtfs/management/commands/importgtfs.py", line 92, in handle
    name += service.start_date.strftime(' starting %Y-%m-%d')
AttributeError: 'NoneType' object has no attribute 'strftime'

I think the better behavior would be to check for an existing FeedInfo and use the start/end dates from that, then fall back to Service, and if the start_date is None or the Service doesn't exist, then finally fall back to the default name with the date on which the feed was imported.

jwhitlock commented 6 years ago

I'd rather not make auto-naming any more complex than it already is, but that's probably the right thing for these horrible feeds. It may make sense to move this to a suggest_name function on the Feed model, so that we can get it under unit tests and code coverage, rather than import a whole feed to test it.

mharvey commented 6 years ago

I like the idea of a suggest_name function. I'd be happy to take a stab at knocking this out over the next week or so if you want.

jwhitlock commented 6 years ago

Thanks @mharvey I'd appreciate a PR