FinalsClub / karmaworld

KarmaNotes.org v3.0
GNU Affero General Public License v3.0
7 stars 6 forks source link

NoReverseMatch error on Beta running Celery worker #388

Closed btbonval closed 9 years ago

btbonval commented 9 years ago
[2015-01-09 19:37:22,558: ERROR/MainProcess] tweet_note[08a0918f-0556-4a47-8ecc-37189cc0c07a]: Traceback (most recent call last):
  File "/app/karmaworld/apps/notes/tasks.py", line 37, in tweet_note
    update = tweet_string(n)
  File "/app/karmaworld/apps/notes/tasks.py", line 56, in tweet_string
    note.get_absolute_url()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/functional.py", line 15, in _curried
    return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/models/base.py", line 985, in get_absolute_url
    return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs)
  File "/app/karmaworld/apps/notes/models.py", line 305, in get_absolute_url
    return reverse('note_detail', args=[self.course.school.slug, self.course.slug, self.slug])
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/urlresolvers.py", line 520, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/urlresolvers.py", line 440, in _reverse_with_prefix
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'note_detail' with arguments '(u'harvard', u'reason-and-faith-in-the-west', u'')' and keyword arguments '{}' not found.
btbonval commented 9 years ago

https://github.com/FinalsClub/karmaworld/blob/master/karmaworld/urls.py#L102-L108

btbonval commented 9 years ago

called from https://github.com/FinalsClub/karmaworld/blob/master/karmaworld/apps/notes/tasks.py#L55-L56 (that URL is bad, it should be dynamically generated!)

calls to https://github.com/FinalsClub/karmaworld/blob/master/karmaworld/apps/notes/models.py#L305

where self.slug is null.

I'm guessing Note.slug has not yet been generated?

btbonval commented 9 years ago

Production logs appear to show no errors, although I'm not sure what the None is all about.

2015-01-13T18:42:02.810833+00:00 app[celerywrapper.1]: [2015-01-13 13:42:02,810: INFO/MainProcess] Got task from broker: tweet_note[9a22d403-bcc4-402c-afdb-080a035d5ab3]
2015-01-13T18:42:06.040772+00:00 app[celerywrapper.1]: [2015-01-13 13:42:06,040: INFO/MainProcess] Task tweet_note[9a22d403-bcc4-402c-afdb-080a035d5ab3] succeeded in 3.12053799629s: None
btbonval commented 9 years ago

Note.slug is not nullable, so it ought to be set to something: https://github.com/FinalsClub/karmaworld/blob/master/karmaworld/apps/notes/models.py#L68

It looks like the slug is the empty string (the third parameter is u''). That would be worth checking in this if clause: https://github.com/FinalsClub/karmaworld/blob/master/karmaworld/apps/notes/models.py#L302

Rather than if self.slug is not None:, perhaps if self.slug: would be better. That'll fail against an empty string as desired.

A hack might be to forcibly call note._generate_unique_slug() prior to calling reverse() on self.slug https://github.com/FinalsClub/karmaworld/blob/master/karmaworld/apps/notes/models.py#L149

btbonval commented 9 years ago

There is indeed a Note with an empty string slug in the Beta database, and it is part of Reason and Faith in the West.

>>> from karmaworld.apps.notes.models import Note
>>> Note.objects.filter(slug=u'')
[<Note: Note at https://www.filepicker.io/api/file/dtQElzaxTuhfO0qouMsn (from None) (1071)>]
>>> Note.objects.filter(slug=u'')[0].course
<Course: Course Reason and Faith in the West in None
btbonval commented 9 years ago

Production has no Notes with an empty slug.

>>> from karmaworld.apps.notes.models import Note
>>> Note.objects.filter(slug=u'')
[]
btbonval commented 9 years ago

Fixed Beta's database so that errant note is no longer an exception. Now Beta has no notes with an empty slug.

>>> from karmaworld.apps.notes.models import Note
>>> note = Note.objects.filter(slug=u'')[0]
>>> note.slug
u''
>>> note._generate_unique_slug()
>>> note.slug
u'-4-1-948651'
>>> note.save()
>>> Note.objects.filter(slug=u'')
[]

Seems to be an edge case, probably due to all the hacking on Beta's database engine. This is very likely solved. Closing the ticket. Will reopen if the NoReverseMatch error reappears in the logs.