]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/models/__init__.py
Merge pull request #1 from udacity/add_requirements_file
[osqa.git] / forum / models / __init__.py
old mode 100755 (executable)
new mode 100644 (file)
index 12a0239..2164e36
-from question import Question ,QuestionRevision, QuestionView, AnonymousQuestion, FavoriteQuestion\r
-from answer import Answer, AnonymousAnswer, AnswerRevision\r
-from tag import Tag, MarkedTag\r
-from meta import Vote, Comment, FlaggedItem\r
-from user import Activity, AnonymousEmail, EmailFeedSetting, AuthKeyUserAssociation\r
-from repute import Badge, Award, Repute\r
-\r
-from base import *\r
-\r
-# User extend properties\r
-QUESTIONS_PER_PAGE_CHOICES = (\r
-   (10, u'10'),\r
-   (30, u'30'),\r
-   (50, u'50'),\r
-)\r
-\r
-def user_is_username_taken(cls,username):\r
-    try:\r
-        cls.objects.get(username=username)\r
-        return True\r
-    except cls.MultipleObjectsReturned:\r
-        return True\r
-    except cls.DoesNotExist:\r
-        return False\r
-\r
-def user_get_q_sel_email_feed_frequency(self):\r
-    #print 'looking for frequency for user %s' % self\r
-    try:\r
-        feed_setting = EmailFeedSetting.objects.get(subscriber=self,feed_type='q_sel')\r
-    except Exception, e:\r
-        #print 'have error %s' % e.message\r
-        raise e\r
-    #print 'have freq=%s' % feed_setting.frequency\r
-    return feed_setting.frequency\r
-\r
-User.add_to_class('is_approved', models.BooleanField(default=False))\r
-User.add_to_class('email_isvalid', models.BooleanField(default=False))\r
-User.add_to_class('email_key', models.CharField(max_length=32, null=True))\r
-User.add_to_class('reputation', models.PositiveIntegerField(default=1))\r
-User.add_to_class('gravatar', models.CharField(max_length=32))\r
-\r
-#User.add_to_class('favorite_questions',\r
-#                  models.ManyToManyField(Question, through=FavoriteQuestion,\r
-#                                         related_name='favorited_by'))\r
-\r
-#User.add_to_class('badges', models.ManyToManyField(Badge, through=Award,\r
-#                                                   related_name='awarded_to'))\r
-User.add_to_class('gold', models.SmallIntegerField(default=0))\r
-User.add_to_class('silver', models.SmallIntegerField(default=0))\r
-User.add_to_class('bronze', models.SmallIntegerField(default=0))\r
-User.add_to_class('questions_per_page',\r
-                  models.SmallIntegerField(choices=QUESTIONS_PER_PAGE_CHOICES, default=10))\r
-User.add_to_class('last_seen',\r
-                  models.DateTimeField(default=datetime.datetime.now))\r
-User.add_to_class('real_name', models.CharField(max_length=100, blank=True))\r
-User.add_to_class('website', models.URLField(max_length=200, blank=True))\r
-User.add_to_class('location', models.CharField(max_length=100, blank=True))\r
-User.add_to_class('date_of_birth', models.DateField(null=True, blank=True))\r
-User.add_to_class('about', models.TextField(blank=True))\r
-User.add_to_class('is_username_taken',classmethod(user_is_username_taken))\r
-User.add_to_class('get_q_sel_email_feed_frequency',user_get_q_sel_email_feed_frequency)\r
-User.add_to_class('hide_ignored_questions', models.BooleanField(default=False))\r
-User.add_to_class('tag_filter_setting',\r
-                    models.CharField(\r
-                                        max_length=16,\r
-                                        choices=TAG_EMAIL_FILTER_CHOICES,\r
-                                        default='ignored'\r
-                                     )\r
-                 )\r
-\r
-# custom signal\r
-tags_updated = django.dispatch.Signal(providing_args=["question"])\r
-edit_question_or_answer = django.dispatch.Signal(providing_args=["instance", "modified_by"])\r
-delete_post_or_answer = django.dispatch.Signal(providing_args=["instance", "deleted_by"])\r
-mark_offensive = django.dispatch.Signal(providing_args=["instance", "mark_by"])\r
-user_updated = django.dispatch.Signal(providing_args=["instance", "updated_by"])\r
-user_logged_in = django.dispatch.Signal(providing_args=["session"])\r
-\r
-\r
-def get_messages(self):\r
-    messages = []\r
-    for m in self.message_set.all():\r
-        messages.append(m.message)\r
-    return messages\r
-\r
-def delete_messages(self):\r
-    self.message_set.all().delete()\r
-\r
-def get_profile_url(self):\r
-    """Returns the URL for this User's profile."""\r
-    return '%s%s/' % (reverse('user', args=[self.id]), slugify(self.username))\r
-\r
-def get_profile_link(self):\r
-    profile_link = u'<a href="%s">%s</a>' % (self.get_profile_url(),self.username)\r
-    logging.debug('in get profile link %s' % profile_link)\r
-    return mark_safe(profile_link)\r
-\r
-User.add_to_class('get_profile_url', get_profile_url)\r
-User.add_to_class('get_profile_link', get_profile_link)\r
-User.add_to_class('get_messages', get_messages)\r
-User.add_to_class('delete_messages', delete_messages)\r
-\r
-def calculate_gravatar_hash(instance, **kwargs):\r
-    """Calculates a User's gravatar hash from their email address."""\r
-    if kwargs.get('raw', False):\r
-        return\r
-    instance.gravatar = hashlib.md5(instance.email).hexdigest()\r
-\r
-def record_ask_event(instance, created, **kwargs):\r
-    if created:\r
-        activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ASK_QUESTION)\r
-        activity.save()\r
-\r
-def record_answer_event(instance, created, **kwargs):\r
-    if created:\r
-        activity = Activity(user=instance.author, active_at=instance.added_at, content_object=instance, activity_type=TYPE_ACTIVITY_ANSWER)\r
-        activity.save()\r
-\r
-def record_comment_event(instance, created, **kwargs):\r
-    if created:\r
-        from django.contrib.contenttypes.models import ContentType\r
-        question_type = ContentType.objects.get_for_model(Question)\r
-        question_type_id = question_type.id\r
-        if (instance.content_type_id == question_type_id):\r
-            type = TYPE_ACTIVITY_COMMENT_QUESTION\r
-        else:\r
-            type = TYPE_ACTIVITY_COMMENT_ANSWER\r
-        activity = Activity(user=instance.user, active_at=instance.added_at, content_object=instance, activity_type=type)\r
-        activity.save()\r
-\r
-def record_revision_question_event(instance, created, **kwargs):\r
-    if created and instance.revision <> 1:\r
-        activity = Activity(user=instance.author, active_at=instance.revised_at, content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_QUESTION)\r
-        activity.save()\r
-\r
-def record_revision_answer_event(instance, created, **kwargs):\r
-    if created and instance.revision <> 1:\r
-        activity = Activity(user=instance.author, active_at=instance.revised_at, content_object=instance, activity_type=TYPE_ACTIVITY_UPDATE_ANSWER)\r
-        activity.save()\r
-\r
-def record_award_event(instance, created, **kwargs):\r
-    """\r
-    After we awarded a badge to user, we need to record this activity and notify user.\r
-    We also recaculate awarded_count of this badge and user information.\r
-    """\r
-    if created:\r
-        activity = Activity(user=instance.user, active_at=instance.awarded_at, content_object=instance,\r
-            activity_type=TYPE_ACTIVITY_PRIZE)\r
-        activity.save()\r
-\r
-        instance.badge.awarded_count += 1\r
-        instance.badge.save()\r
-\r
-        if instance.badge.type == Badge.GOLD:\r
-            instance.user.gold += 1\r
-        if instance.badge.type == Badge.SILVER:\r
-            instance.user.silver += 1\r
-        if instance.badge.type == Badge.BRONZE:\r
-            instance.user.bronze += 1\r
-        instance.user.save()\r
-\r
-def notify_award_message(instance, created, **kwargs):\r
-    """\r
-    Notify users when they have been awarded badges by using Django message.\r
-    """\r
-    if created:\r
-        user = instance.user\r
-        user.message_set.create(message=u"Congratulations, you have received a badge '%s'" % instance.badge.name)\r
-\r
-def record_answer_accepted(instance, created, **kwargs):\r
-    """\r
-    when answer is accepted, we record this for question author - who accepted it.\r
-    """\r
-    if not created and instance.accepted:\r
-        activity = Activity(user=instance.question.author, active_at=datetime.datetime.now(), \\r
-            content_object=instance, activity_type=TYPE_ACTIVITY_MARK_ANSWER)\r
-        activity.save()\r
-\r
-def update_last_seen(instance, created, **kwargs):\r
-    """\r
-    when user has activities, we update 'last_seen' time stamp for him\r
-    """\r
-    user = instance.user\r
-    user.last_seen = datetime.datetime.now()\r
-    user.save()\r
-\r
-def record_vote(instance, created, **kwargs):\r
-    """\r
-    when user have voted\r
-    """\r
-    if created:\r
-        if instance.vote == 1:\r
-            vote_type = TYPE_ACTIVITY_VOTE_UP\r
-        else:\r
-            vote_type = TYPE_ACTIVITY_VOTE_DOWN\r
-\r
-        activity = Activity(user=instance.user, active_at=instance.voted_at, content_object=instance, activity_type=vote_type)\r
-        activity.save()\r
-\r
-def record_cancel_vote(instance, **kwargs):\r
-    """\r
-    when user canceled vote, the vote will be deleted.\r
-    """\r
-    activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_CANCEL_VOTE)\r
-    activity.save()\r
-\r
-def record_delete_question(instance, delete_by, **kwargs):\r
-    """\r
-    when user deleted the question\r
-    """\r
-    if instance.__class__ == "Question":\r
-        activity_type = TYPE_ACTIVITY_DELETE_QUESTION\r
-    else:\r
-        activity_type = TYPE_ACTIVITY_DELETE_ANSWER\r
-\r
-    activity = Activity(user=delete_by, active_at=datetime.datetime.now(), content_object=instance, activity_type=activity_type)\r
-    activity.save()\r
-\r
-def record_mark_offensive(instance, mark_by, **kwargs):\r
-    activity = Activity(user=mark_by, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_MARK_OFFENSIVE)\r
-    activity.save()\r
-\r
-def record_update_tags(question, **kwargs):\r
-    """\r
-    when user updated tags of the question\r
-    """\r
-    activity = Activity(user=question.author, active_at=datetime.datetime.now(), content_object=question, activity_type=TYPE_ACTIVITY_UPDATE_TAGS)\r
-    activity.save()\r
-\r
-def record_favorite_question(instance, created, **kwargs):\r
-    """\r
-    when user add the question in him favorite questions list.\r
-    """\r
-    if created:\r
-        activity = Activity(user=instance.user, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_FAVORITE)\r
-        activity.save()\r
-\r
-def record_user_full_updated(instance, **kwargs):\r
-    activity = Activity(user=instance, active_at=datetime.datetime.now(), content_object=instance, activity_type=TYPE_ACTIVITY_USER_FULL_UPDATED)\r
-    activity.save()\r
-\r
-def post_stored_anonymous_content(sender,user,session_key,signal,*args,**kwargs):\r
-    aq_list = AnonymousQuestion.objects.filter(session_key = session_key)\r
-    aa_list = AnonymousAnswer.objects.filter(session_key = session_key)\r
-    import settings\r
-    if settings.EMAIL_VALIDATION == 'on':#add user to the record\r
-        for aq in aq_list:\r
-            aq.author = user\r
-            aq.save()\r
-        for aa in aa_list:\r
-            aa.author = user\r
-            aa.save()\r
-        #maybe add pending posts message?\r
-    else: #just publish the questions\r
-        for aq in aq_list:\r
-            aq.publish(user)\r
-        for aa in aa_list:\r
-            aa.publish(user)\r
-\r
-#signal for User modle save changes\r
-\r
-pre_save.connect(calculate_gravatar_hash, sender=User)\r
-post_save.connect(record_ask_event, sender=Question)\r
-post_save.connect(record_answer_event, sender=Answer)\r
-post_save.connect(record_comment_event, sender=Comment)\r
-post_save.connect(record_revision_question_event, sender=QuestionRevision)\r
-post_save.connect(record_revision_answer_event, sender=AnswerRevision)\r
-post_save.connect(record_award_event, sender=Award)\r
-post_save.connect(notify_award_message, sender=Award)\r
-post_save.connect(record_answer_accepted, sender=Answer)\r
-post_save.connect(update_last_seen, sender=Activity)\r
-post_save.connect(record_vote, sender=Vote)\r
-post_delete.connect(record_cancel_vote, sender=Vote)\r
-delete_post_or_answer.connect(record_delete_question, sender=Question)\r
-delete_post_or_answer.connect(record_delete_question, sender=Answer)\r
-mark_offensive.connect(record_mark_offensive, sender=Question)\r
-mark_offensive.connect(record_mark_offensive, sender=Answer)\r
-tags_updated.connect(record_update_tags, sender=Question)\r
-post_save.connect(record_favorite_question, sender=FavoriteQuestion)\r
-user_updated.connect(record_user_full_updated, sender=User)\r
-user_logged_in.connect(post_stored_anonymous_content)\r
-\r
-Question = Question\r
-QuestionRevision = QuestionRevision\r
-QuestionView = QuestionView\r
-FavoriteQuestion = FavoriteQuestion\r
-AnonymousQuestion = AnonymousQuestion\r
-\r
-Answer = Answer\r
-AnswerRevision = AnswerRevision\r
-AnonymousAnswer = AnonymousAnswer\r
-\r
-Tag = Tag\r
-Comment = Comment\r
-Vote = Vote\r
-FlaggedItem = FlaggedItem\r
-MarkedTag = MarkedTag\r
-\r
-Badge = Badge\r
-Award = Award\r
-Repute = Repute\r
-\r
-Activity = Activity\r
-EmailFeedSetting = EmailFeedSetting\r
-AnonymousEmail = AnonymousEmail\r
-AuthKeyUserAssociation = AuthKeyUserAssociation\r
-\r
-__all__ = [\r
-        'Question',\r
-        'QuestionRevision',\r
-        'QuestionView',\r
-        'FavoriteQuestion',\r
-        'AnonymousQuestion',\r
-\r
-        'Answer',\r
-        'AnswerRevision',\r
-        'AnonymousAnswer',\r
-\r
-        'Tag',\r
-        'Comment',\r
-        'Vote',\r
-        'FlaggedItem',\r
-        'MarkedTag',\r
-\r
-        'Badge',\r
-        'Award',\r
-        'Repute',\r
-\r
-        'Activity',\r
-        'EmailFeedSetting',\r
-        'AnonymousEmail',\r
-        'AuthKeyUserAssociation',\r
-\r
-        'User'\r
-        ]\r
-\r
-\r
-from forum.modules import get_modules_script_classes\r
-\r
-for k, v in get_modules_script_classes('models', models.Model).items():\r
-    if not k in __all__:\r
-        __all__.append(k)\r
-        exec "%s = v" % k
\ No newline at end of file
+import forum.utils.djangofix
+from question import Question ,QuestionRevision, QuestionSubscription
+from answer import Answer, AnswerRevision
+from tag import Tag, MarkedTag
+from user import User, ValidationHash, AuthKeyUserAssociation, SubscriptionSettings
+from node import Node, NodeRevision, NodeState, NodeMetaClass
+from comment import Comment
+from action import Action, ActionRepute
+from meta import Vote, Flag, Badge, Award
+from utils import KeyValue
+from page import Page
+
+try:
+    from south.modelsinspector import add_introspection_rules
+    add_introspection_rules([], [r"^forum\.models\.\w+\.\w+"])
+except:
+    pass
+
+from base import *
+
+__all__ = [
+        'Node', 'NodeRevision', 'NodeState',  
+        'Question', 'QuestionSubscription', 'QuestionRevision',
+        'Answer', 'AnswerRevision',
+        'Tag', 'Comment', 'MarkedTag', 'Badge', 'Award',
+        'ValidationHash', 'AuthKeyUserAssociation', 'SubscriptionSettings', 'KeyValue', 'User',
+        'Action', 'ActionRepute', 'Vote', 'Flag', 'Page'
+        ]
+
+
+from forum.modules import get_modules_script_classes
+
+for k, v in get_modules_script_classes('models', models.Model).items():
+    if not k in __all__:
+        __all__.append(k)
+        exec "%s = v" % k
+
+NodeMetaClass.setup_relations()
+BaseMetaClass.setup_denormalizes()