enquiry.models: 25 total statements, 81.2% covered

Generated: Tue 2013-02-05 19:07 SGT

Source file: /Users/martin/Repos/django-enquiry/enquiry/models.py

Stats: 13 executed, 3 missed, 9 excluded, 72 ignored

  1. """Models for the ``enquiry`` app."""
  2. from django.conf import settings
  3. from django.db import models
  4. from django.utils.translation import get_language
  5. from django.utils.translation import ugettext_lazy as _
  6. from simple_translation.utils import get_translation_queryset
  7. class TransModelMixin(object):
  8. """Mixin that returns the translation object for a given parent model."""
  9. def get_translation(self):
  10. lang = get_language()
  11. return get_translation_queryset(self).filter(language=lang)[0]
  12. class Enquiry(TransModelMixin, models.Model):
  13. """
  14. An enquiry has start and end dates and a question with several answers.
  15. For translateable fields see the ``EnquiryTrans`` model.
  16. :creation_date: The DateTime when this enquiry was created.
  17. :created_by: The user who created this enquiry.
  18. :start_date: The start date of this enquiry.
  19. :end_date: The end date of this enquiry.
  20. :allow_anonymous: If ``True`` anonymous user can participate, otherwise
  21. users must be authenticated in order to vote.
  22. """
  23. def __unicode__(self):
  24. return self.get_translation().question
  25. class EnquiryTrans(models.Model):
  26. """
  27. Translateable fields for the ``Enquiry`` model.
  28. :question: The title of this category.
  29. """
  30. # Needed by simple-translation
  31. enquiry = models.ForeignKey(Enquiry, verbose_name=_('Enquiry'))
  32. language = models.CharField(
  33. max_length=2, verbose_name=_('Language'), choices=settings.LANGUAGES)
  34. class Answer(models.Model):
  35. """
  36. An answer belongs to an enquiry.
  37. For translateable fields see ``AnswerTrans`` model.
  38. :enquiry: FK to the enquiry this answer belongs to.
  39. """
  40. pass
  41. def __unicode__(self):
  42. return self.get_translation().text
  43. def get_vote_count(self):
  44. """Returns the number of votes for this answer."""
  45. pass
  46. class AnswerTrans(models.Model):
  47. """
  48. Translateable fields for the ``Answer`` model.
  49. :text: The text of this answer.
  50. """
  51. # Needed by simple-translation
  52. answer = models.ForeignKey(Answer, verbose_name=_('Answer'))
  53. language = models.CharField(
  54. max_length=2, verbose_name=_('Language'), choices=settings.LANGUAGES)
  55. class Vote(models.Model):
  56. """
  57. A vote is being cast on an answer by a user.
  58. :answer: The answer this user has chosen.
  59. :user: The user who cast this vote. Can be None if the user was anonymous.
  60. We also need to save something to identify anonymous users here. IP might
  61. not be a good idea because those students might all access the portal
  62. from within the campus and have the same IP.
  63. Maybe we are saving sessions in the database and we could save the session
  64. PK here? But we are cleaning up the sessions regularily, so then the
  65. PKs in this field would no longer be valid. Can't remember how we did this
  66. with the FAQ app, please come up with a good idea.
  67. """
  68. pass