def forwards(self, orm):
        n_count = orm.Node.objects.all().count()
        print "\nReseting %d nodes:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            try:
                d = orm.Action.objects.get(node=n, action_type="delete", canceled=False)
                n.deleted_id = d.id
            except Exception, e:
                n.deleted = None

            if orm.Action.objects.filter(node=n, action_type="revise").count() > 0:
                n.last_edited_id = orm.Action.objects.filter(node=n, action_type="revise").order_by('-action_date')[0].id
            else:
                n.last_edited = None


            if n.node_type == "answer" and n.marked:
                n.extra_action_id = orm.Action.objects.get(node=n, action_type="acceptanswer", canceled=False).id

            if n.node_type == "question" and n.marked:
                n.extra_action_id = orm.Action.objects.get(node=n, action_type="close", canceled=False).id

            n.save()

            progress.update()
    def forwards(self, orm):
        n_count = orm.Node.objects.all().count()
        print "\nReseting %d nodes:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            try:
                d = orm.Action.objects.get(node=n,
                                           action_type="delete",
                                           canceled=False)
                n.deleted_id = d.id
            except Exception, e:
                n.deleted = None

            if orm.Action.objects.filter(node=n,
                                         action_type="revise").count() > 0:
                n.last_edited_id = orm.Action.objects.filter(
                    node=n,
                    action_type="revise").order_by('-action_date')[0].id
            else:
                n.last_edited = None

            if n.node_type == "answer" and n.marked:
                n.extra_action_id = orm.Action.objects.get(
                    node=n, action_type="acceptanswer", canceled=False).id

            if n.node_type == "question" and n.marked:
                n.extra_action_id = orm.Action.objects.get(node=n,
                                                           action_type="close",
                                                           canceled=False).id

            n.save()

            progress.update()
Esempio n. 3
0
    def forwards(self, orm):
        n_count = orm.Node.objects.count()
        print "\nConverting %s node states:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            ss = ""

            if n.deleted:
                s = orm.NodeState(
                    node = n,
                    action = n.deleted,
                    state_type = "deleted"
                )
                s.save()
                ss += "(deleted)"

            if n.wiki:
                try:
                    action = orm.Action.objects.get(node=n, action_type="wikify")
                    s = orm.NodeState(
                        node = n,
                        action = action,
                        state_type = "wiki"
                    )
                    s.save()
                    ss += "(wiki)"
                except:
                    pass

            if n.node_type == "question" and n.extra_action:
                s = orm.NodeState(
                    node = n,
                    action = n.extra_action,
                    state_type = "closed"
                )
                s.save()
                ss += "(closed)"

            if n.node_type == "answer" and n.extra_action:
                s = orm.NodeState(
                    node = n,
                    action = n.extra_action,
                    state_type = "accepted"
                )
                s.save()
                ss += "(accepted)"

            if ss:
                n.state_string = ss
                n.save()

            progress.update()

        print "\n...done\n"
Esempio n. 4
0
    def forwards(self, orm):
        n_count = orm.Node.objects.count()
        print "\nConverting %s node states:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            ss = ""

            if n.deleted:
                s = orm.NodeState(
                    node = n,
                    action = n.deleted,
                    state_type = "deleted"
                )
                s.save()
                ss += "(deleted)"

            if n.wiki:
                try:
                    action = orm.Action.objects.get(node=n, action_type="wikify")
                    s = orm.NodeState(
                        node = n,
                        action = action,
                        state_type = "wiki"
                    )
                    s.save()
                    ss += "(wiki)"
                except:
                    pass

            if n.node_type == "question" and n.extra_action:
                s = orm.NodeState(
                    node = n,
                    action = n.extra_action,
                    state_type = "closed"
                )
                s.save()
                ss += "(closed)"

            if n.node_type == "answer" and n.extra_action:
                s = orm.NodeState(
                    node = n,
                    action = n.extra_action,
                    state_type = "accepted"
                )
                s.save()
                ss += "(accepted)"

            if ss:
                n.state_string = ss
                n.save()

            progress.update()

        print "\n...done\n"
Esempio n. 5
0
    def forwards(self, orm):
        k_count = orm.KeyValue.objects.count()
        print "\nConverting %d keyvalue objects:" % k_count
        progress = ProgressBar(k_count)

        for kv in orm.KeyValue.objects.all():
            try:
                o = loads(kv.value.encode('utf-8'))
            except:
                o = kv.value

            kv.value = dbsafe_encode(o, compress_object=True)
            kv.save()
            progress.update()

        print "\n...done\n"

        a_count = orm.Action.objects.count()
        print "\nConverting %d actions extra fields:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.all():
            a.extra = dbsafe_encode(a.extra, compress_object=True)
            a.save()
            progress.update()

        print "\n...done\n"
    def forwards(self, orm):
        sys.path.append(os.path.join(os.path.dirname(__file__),'../markdownext'))

        count  = orm.Node.objects.count()
        progress = ProgressBar(count)
        
        for node in orm.Node.objects.all():
            rev = node.active_revision

            if not rev:
                try:
                    rev = node.revisions.order_by('-revision')[0]
                except:
                    continue
            node.body = sanitize_html(markdown.markdown(rev.body, ['urlize']))
            node.save()
            progress.update()
Esempio n. 7
0
    def forwards(self, orm):
        r_count = orm.ActionRepute.objects.count()
        print "\nAdding dates to %d repute actions:" % r_count
        progress = ProgressBar(r_count)

        for r in orm.ActionRepute.objects.all():
            a = r.action

            if r.by_canceled:
                r.date = a.canceled_at
            else:
                r.date = a.action_date

            r.save()

            progress.update()

        print "\n...done\n"
Esempio n. 8
0
    def forwards(self, orm):
        r_count = orm.ActionRepute.objects.count()
        print "\nAdding dates to %d repute actions:" % r_count
        progress = ProgressBar(r_count)

        for r in orm.ActionRepute.objects.all():
            a = r.action

            if r.by_canceled:
                r.date = a.canceled_at
            else:
                r.date = a.action_date

            r.save()

            progress.update()

        print "\n...done\n"
Esempio n. 9
0
    def forwards(self, orm):
        def get_abs_par(n):
            if n.parent:
                return get_abs_par(n.parent)
            else:
                return n

        n_count = orm.Node.objects.count()
        print "\nConverting %d nodes:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            if n.parent:
                n.abs_parent = get_abs_par(n)
            else:
                n.abs_parent = None
            n.save()
            progress.update()

        print "\n...done\n"
Esempio n. 10
0
    def forwards(self, orm):
        def get_abs_par(n):
            if n.parent:
                return get_abs_par(n.parent)
            else:
                return n

        n_count = orm.Node.objects.count()
        print "\nConverting %d nodes:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            if n.parent:
                n.abs_parent = get_abs_par(n)
            else:
                n.abs_parent = None
            n.save()
            progress.update()

        print "\n...done\n"
Esempio n. 11
0
    def forwards(self, orm):
        q_count = orm.Node.objects.filter(node_type="question").count()
        print "\nConverting %d questions:" % q_count
        progress = ProgressBar(q_count)

        for n in orm.Node.objects.filter(node_type="question"):
            q = orm.Question.objects.get(node_ptr=n)
            if q.answer_accepted:
                accepted = list(orm.Answer.objects.filter(node_ptr__parent=n, accepted=True))

                if len(accepted):
                    accepted = accepted[0]
                    q.accepted_answer = accepted
                else:
                    q.answer_accepted = False

                q.save()           

            progress.update()

        print "\n...done\n"
Esempio n. 12
0
    def forwards(self, orm):
        k_count = orm.KeyValue.objects.count()
        print "\nConverting %d keyvalue objects:" % k_count
        progress = ProgressBar(k_count)

        for kv in orm.KeyValue.objects.all():
            try:
                o = loads(kv.value.encode("utf-8"))
            except:
                o = kv.value

            kv.value = dbsafe_encode(o, compress_object=True)
            kv.save()
            progress.update()

        print "\n...done\n"

        a_count = orm.Action.objects.count()
        print "\nConverting %d actions extra fields:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.all():
            a.extra = dbsafe_encode(a.extra, compress_object=True)
            a.save()
            progress.update()

        print "\n...done\n"
Esempio n. 13
0
    def forwards(self, orm):
        a_count = orm.Action.objects.filter(action_type="bonusrep").count()
        print "\nConverting %s bonus actions:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.filter(action_type="bonusrep"):
            a.user = orm.User.objects.get(id=a.extra['awarding_user'])
            a.save()

            progress.update()

        print "\n...done\n"


        s_count = orm.Action.objects.filter(action_type="suspend").count()
        print "\nConverting %s suspend actions:" % a_count
        progress = ProgressBar(s_count)

        for a in orm.Action.objects.filter(action_type="suspend"):
            suspended = a.user

            a.user = orm.User.objects.get(id=a.extra['suspender'])
            a.save()

            rep = orm.ActionRepute(user=suspended, action=a, value=0, date=a.action_date)
            rep.save()

            progress.update()

        print "\n...done\n"

        a_count = orm.Action.objects.filter(action_type="award").count()
        print "\nConverting %s award actions:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.filter(action_type="award"):
            a.ip = ''
            a.save()

            progress.update()

        print "\n...done\n"
Esempio n. 14
0
    def forwards(self, orm):
        #Converting question revisions
        qr_count = orm.QuestionRevision.objects.all().count()
        print "\nConverting %d question revisions:" % qr_count
        progress = ProgressBar(qr_count)

        for q in orm.QuestionRevision.objects.order_by('id'):
            revision = orm.NodeRevision(
                node=q.question.node_ptr,
                title=q.title,
                summary=q.summary,
                tagnames=q.tagnames,
                revision=q.revision,
                author=q.author,
                revised_at=q.revised_at,
                body=q.text,
            )

            revision.save()
            progress.update()

        print "\n...done\n"


        #Converting answer revisions
        ar_count = orm.AnswerRevision.objects.all().count()
        print "\nConverting %d answer revisions:" % ar_count
        progress = ProgressBar(ar_count)

        for a in orm.AnswerRevision.objects.order_by('id'):
            revision = orm.NodeRevision(
                summary=a.summary,
                node=a.answer.node_ptr,
                revision=a.revision,
                author=a.author,
                revised_at=a.revised_at,
                body=a.text,
            )

            revision.save()
            progress.update()

        print "\n...done\n"
Esempio n. 15
0
    def forwards(self, orm):
        #Converting question revisions
        qr_count = orm.QuestionRevision.objects.all().count()
        print "\nConverting %d question revisions:" % qr_count
        progress = ProgressBar(qr_count)

        for q in orm.QuestionRevision.objects.order_by('id'):
            revision = orm.NodeRevision(
                node=q.question.node_ptr,
                title=q.title,
                summary=q.summary,
                tagnames=q.tagnames,
                revision=q.revision,
                author=q.author,
                revised_at=q.revised_at,
                body=q.text,
            )

            revision.save()
            progress.update()

        print "\n...done\n"


        #Converting answer revisions
        ar_count = orm.AnswerRevision.objects.all().count()
        print "\nConverting %d answer revisions:" % ar_count
        progress = ProgressBar(ar_count)

        for a in orm.AnswerRevision.objects.order_by('id'):
            revision = orm.NodeRevision(
                summary=a.summary,
                node=a.answer.node_ptr,
                revision=a.revision,
                author=a.author,
                revised_at=a.revised_at,
                body=a.text,
            )

            revision.save()
            progress.update()

        print "\n...done\n"
Esempio n. 16
0
    def forwards(self, orm):
        a_count = orm.Action.objects.filter(action_type="bonusrep").count()
        print "\nConverting %s bonus actions:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.filter(action_type="bonusrep"):
            a.user = orm.User.objects.get(id=a.extra['awarding_user'])
            a.save()

            progress.update()

        print "\n...done\n"

        s_count = orm.Action.objects.filter(action_type="suspend").count()
        print "\nConverting %s suspend actions:" % a_count
        progress = ProgressBar(s_count)

        for a in orm.Action.objects.filter(action_type="suspend"):
            suspended = a.user

            a.user = orm.User.objects.get(id=a.extra['suspender'])
            a.save()

            rep = orm.ActionRepute(user=suspended,
                                   action=a,
                                   value=0,
                                   date=a.action_date)
            rep.save()

            progress.update()

        print "\n...done\n"

        a_count = orm.Action.objects.filter(action_type="award").count()
        print "\nConverting %s award actions:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.filter(action_type="award"):
            a.ip = ''
            a.save()

            progress.update()

        print "\n...done\n"
    def forwards(self, orm):
        c_count = orm.Comment.objects.count()
        print "\nConverting %d comments:" % c_count
        progress = ProgressBar(c_count)

        id_map = {}

        for c in orm.Comment.objects.all():
            node = orm.Node(
                node_type='comment',
                author=c.user,
                added_at=c.added_at,
                score=c.score,
                vote_up_count=c.score,
                vote_down_count=0,
                offensive_flag_count=0,
                last_edited_at=c.added_at,
                last_edited_by=c.user,
                body=c.comment,
                deleted=c.deleted,
                deleted_by=c.deleted_by,
                deleted_at=c.deleted_at,
                parent=c.node,
            )
            node.save()

            id_map[c.id] = node

            revision = orm.NodeRevision(
                summary="Initial revsion",
                revision=1,
                revised_at=c.added_at,
                body=c.comment,
                author=c.user,
                node=node,
            )

            revision.save()

            node.active_revision = revision
            node.save()

            for v in orm.LikedComment.objects.filter(comment=c):
                vote = orm.Vote(
                    node=node,
                    vote=1,
                    voted_at=v.added_at,
                    canceled=v.canceled,
                    user=v.user,
                )

                vote.save()

            progress.update()

        print "\n...done\n"

        ctype = orm['contenttypes.ContentType'].objects.get(name="comment").id
        ntype = orm['contenttypes.ContentType'].objects.get(name="node").id

        #Converting activity
        activities = orm.Activity.objects.filter(content_type__id=ctype)
        activity_count = activities.count()
        print "Converting %d activity references:" % activity_count
        progress = ProgressBar(activity_count)

        for a in activities:
            node = id_map.get(a.object_id, None)
            if node:
                a.content_type_id = ntype
                a.object_id = node.id
                a.save()

            progress.update()

        print "\n...done\n"
    def forwards(self, orm):
        rephist = dict([(t, []) for t in range(-8, 6) if t != 0])

        r_count = orm.Repute.objects.count()
        print "\nCalculating rep gain/losses history through %d records:" % r_count
        progress = ProgressBar(r_count)

        for r in orm.Repute.objects.all():
            l = rephist.get(r.reputation_type, None)
            if l is None: continue

            if (len(l) == 0) or (l[-1][1] != r.value):
                l.append((r.reputed_at, r.value))

            progress.update()

        print "\n...done\n"


        def repval_at(reptype, repdate, default):
            l = rephist.get(reptype, None)

            if l is None: return 0
            if len(l) == 0: return default

            for r in l:
                if r[0] <= repdate:
                    return r[1] or default


        q_count = orm.Question.objects.count()
        print "\nConverting %d questions:" % q_count
        progress = ProgressBar(q_count)

        for q in orm.Question.objects.all():
            n = q.node_ptr
            n.last_activity_at = q.last_activity_at
            n.last_activity_by = q.last_activity_by

            if q.accepted_answer:
                n.extra_ref = q.accepted_answer.node_ptr
                
            n.extra_count = q.view_count

            n.marked = q.closed
            n.wiki = q.wiki

            n.save()

            ask = orm.Action(
                user = n.author,
                action_date = n.added_at,
                node = n,
                action_type = "ask",
                extra = ''
            )

            ask.save()

            if n.deleted:
                action = orm.Action(
                    user = n.deleted_by,
                    node = n,
                    action_type = "delete",
                    action_date = n.deleted_at or datetime.datetime.now(),
                    extra = ''
                )

                action.save()


            if n.marked:
                action = orm.Action(
                    user = q.closed_by,
                    node = n,
                    action_type = "close",
                    extra = q.close_reason,
                    action_date = q.closed_at or datetime.datetime.now(),
                )

                action.save()

            if n.wiki:
                action = orm.Action(
                    user = n.author,
                    node = n,
                    action_type = "wikify",
                    action_date = q.wikified_at or datetime.datetime.now(),
                    extra = ''
                )

                action.save()

            progress.update()

        print "\n...done\n"

        a_count = orm.Answer.objects.count()
        print "\nConverting %d answers:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Answer.objects.all():
            n = a.node_ptr

            n.marked = a.accepted
            n.wiki = a.wiki

            n.save()

            ans = orm.Action(
                user = n.author,
                action_date = n.added_at,
                node = n,
                action_type = "answer",
                extra = ''
            )

            ans.save()

            if n.deleted:
                action = orm.Action(
                    user = n.deleted_by,
                    node = n,
                    action_type = "delete",
                    action_date = n.deleted_at or datetime.datetime.now(),
                    extra = ''
                )

                action.save()

            if a.accepted:
                action = orm.Action(
                    user = a.accepted_by,
                    node = n,
                    action_type = "acceptanswer",
                    action_date = a.accepted_at or datetime.datetime.now(),
                    extra = ''
                )

                action.save()

                if not a.wiki or a.wikified_at > action.action_date:
                    if action.user == n.author:
                        rep = orm.ActionRepute(
                            action = action,
                            user = action.user,
                            value = repval_at(GAIN_BY_ACCEPTING_ANSWER, action.action_date, 2)
                        )
                        rep.save()

                    if n.author != n.parent.author:
                        rep = orm.ActionRepute(
                            action = action,
                            user = n.author,
                            value = repval_at(GAIN_BY_ANSWER_ACCEPTED, action.action_date, 15)
                        )
                        rep.save()

            if n.wiki:
                action = orm.Action(
                    user = n.author,
                    node = n,
                    action_type = "wikify",
                    action_date = a.wikified_at or datetime.datetime.now(),
                    extra = ''
                )

                action.save()

            progress.update()

        print "\n...done\n"

        v_count = orm.Vote.objects.count()
        print "\nConverting %d votes:" % v_count
        progress = ProgressBar(v_count)

        for v in orm.Vote.objects.exclude(canceled=True):
            a = orm.Action(
                action_type = (v.vote == 1) and ((v.node.node_type == "comment") and "voteupcomment" or "voteup") or "votedown",
                user = v.user,
                node = v.node,
                action_date = v.voted_at,
                canceled = v.canceled,
                extra = ''
            )

            a.save()

            def impl(node):
                if node.node_type == "question":
                    return orm.Question.objects.get(node_ptr=node)
                else:
                    return orm.Answer.objects.get(node_ptr=node)

            if a.node.node_type in ("question", "answer") and (not a.node.wiki or impl(a.node).wikified_at > a.action_date):
                reptype, default = (v.vote == 1) and (GAIN_BY_UPVOTED, 10) or (LOST_BY_DOWNVOTED, 2)
                rep = orm.ActionRepute(
                    action = a,
                    user = a.node.author,
                    value = repval_at(reptype, a.action_date, default) or default
                )
                rep.save()

                if v.vote == -1:
                    rep = orm.ActionRepute(
                        action = a,
                        user = a.node.author,
                        value = repval_at(LOST_BY_DOWNVOTING, a.action_date, 1) or default
                    )
                    rep.save()

            progress.update()

        print "\n...done\n"

        f_count = orm.FlaggedItem.objects.count()
        print "\nConverting %d flags:" % f_count
        progress = ProgressBar(f_count)

        for f in orm.FlaggedItem.objects.all():
            a = orm.Action(
                action_type = "flag",
                user = f.user,
                node = f.node,
                action_date = f.flagged_at,
                extra = f.reason or ''
            )

            a.save()

            rep = orm.ActionRepute(
                action = a,
                user = a.node.author,
                value = repval_at(LOST_BY_FLAGGED, a.action_date, 2) or 2
            )
            rep.save()

            progress.update()

        print "\n...done\n"

        n_count = orm.Node.objects.all().count()
        print "\nChecking flag count of %d nodes:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            flags = list(orm.Action.objects.filter(action_type="flag", node=n, canceled=False).order_by('-action_date'))

            if len(flags) >= 3:
                a = flags[2]
                rep = orm.ActionRepute(
                    action = a,
                    user = n.author,
                    value = repval_at(LOST_BY_FLAGGED_3_TIMES, a.action_date, 30)
                )
                rep.save()


            if len(flags) >= 5:
                a = flags[4]
                rep = orm.ActionRepute(
                    action = a,
                    user = n.author,
                    value = repval_at(LOST_BY_FLAGGED_5_TIMES, a.action_date, 100)
                )
                rep.save()

            progress.update()

        print "\n...done\n"

        c_count = orm.Node.objects.filter(node_type="comment").count()
        print "\nCreating %d comment actions:" % c_count
        progress = ProgressBar(c_count)

        for c in orm.Node.objects.filter(node_type="comment").all():
            a = orm.Action(
                action_type = "comment",
                user = c.author,
                node = c,
                action_date = c.added_at,
                extra = ''
            )

            a.save()

            if c.deleted:
                action = orm.Action(
                    user = c.deleted_by,
                    node = c,
                    action_type = "delete",
                    action_date = c.deleted_at or datetime.datetime.now(),
                    extra = ''
                )

                action.save()

            progress.update()

        print "\n...done\n"


        r_count = orm.NodeRevision.objects.exclude(revision=1).count()
        print "\nCreating %d edit actions:" % r_count
        progress = ProgressBar(r_count)

        for r in orm.NodeRevision.objects.exclude(revision=1):
            a = orm.Action(
                action_type = "revise",
                user = r.author,
                node = r.node,
                action_date = r.revised_at,
                extra = r.revision
            )

            a.save()
            progress.update()

        print "\n...done\n"

        u_count = orm.User.objects.all().count()
        print "\nCreating %d user join actions and reputation recalculation:" % u_count
        progress = ProgressBar(u_count)

        for u in orm.User.objects.all():
            a = orm.Action(
                user = u,
                action_date = u.date_joined,
                action_type = "userjoins",
            )

            a.save()

            rep = orm.ActionRepute(
                action = a,
                user = u,
                value = 1
            )
            rep.save()

            new_rep = orm.ActionRepute.objects.filter(user=u).aggregate(reputation=models.Sum('value'))['reputation']

            if new_rep < 0:
                new_rep = 1

            u.reputation = new_rep
            u.save()

            progress.update()

        print "\n...done\n"
    def forwards(self, orm):
        rephist = dict([(t, []) for t in range(-8, 6) if t != 0])

        r_count = orm.Repute.objects.count()
        print "\nCalculating rep gain/losses history through %d records:" % r_count
        progress = ProgressBar(r_count)

        for r in orm.Repute.objects.all():
            l = rephist.get(r.reputation_type, None)
            if l is None: continue

            if (len(l) == 0) or (l[-1][1] != r.value):
                l.append((r.reputed_at, r.value))

            progress.update()

        print "\n...done\n"

        def repval_at(reptype, repdate, default):
            l = rephist.get(reptype, None)

            if l is None: return 0
            if len(l) == 0: return default

            for r in l:
                if r[0] <= repdate:
                    return r[1] or default

        q_count = orm.Question.objects.count()
        print "\nConverting %d questions:" % q_count
        progress = ProgressBar(q_count)

        for q in orm.Question.objects.all():
            n = q.node_ptr
            n.last_activity_at = q.last_activity_at
            n.last_activity_by = q.last_activity_by

            if q.accepted_answer:
                n.extra_ref = q.accepted_answer.node_ptr

            n.extra_count = q.view_count

            n.marked = q.closed
            n.wiki = q.wiki

            n.save()

            ask = orm.Action(user=n.author,
                             action_date=n.added_at,
                             node=n,
                             action_type="ask",
                             extra='')

            ask.save()

            if n.deleted:
                action = orm.Action(user=n.deleted_by,
                                    node=n,
                                    action_type="delete",
                                    action_date=n.deleted_at
                                    or datetime.datetime.now(),
                                    extra='')

                action.save()

            if n.marked:
                action = orm.Action(
                    user=q.closed_by,
                    node=n,
                    action_type="close",
                    extra=q.close_reason,
                    action_date=q.closed_at or datetime.datetime.now(),
                )

                action.save()

            if n.wiki:
                action = orm.Action(user=n.author,
                                    node=n,
                                    action_type="wikify",
                                    action_date=q.wikified_at
                                    or datetime.datetime.now(),
                                    extra='')

                action.save()

            progress.update()

        print "\n...done\n"

        a_count = orm.Answer.objects.count()
        print "\nConverting %d answers:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Answer.objects.all():
            n = a.node_ptr

            n.marked = a.accepted
            n.wiki = a.wiki

            n.save()

            ans = orm.Action(user=n.author,
                             action_date=n.added_at,
                             node=n,
                             action_type="answer",
                             extra='')

            ans.save()

            if n.deleted:
                action = orm.Action(user=n.deleted_by,
                                    node=n,
                                    action_type="delete",
                                    action_date=n.deleted_at
                                    or datetime.datetime.now(),
                                    extra='')

                action.save()

            if a.accepted:
                action = orm.Action(user=a.accepted_by,
                                    node=n,
                                    action_type="acceptanswer",
                                    action_date=a.accepted_at
                                    or datetime.datetime.now(),
                                    extra='')

                action.save()

                if not a.wiki or a.wikified_at > action.action_date:
                    if action.user == n.author:
                        rep = orm.ActionRepute(action=action,
                                               user=action.user,
                                               value=repval_at(
                                                   GAIN_BY_ACCEPTING_ANSWER,
                                                   action.action_date, 2))
                        rep.save()

                    if n.author != n.parent.author:
                        rep = orm.ActionRepute(action=action,
                                               user=n.author,
                                               value=repval_at(
                                                   GAIN_BY_ANSWER_ACCEPTED,
                                                   action.action_date, 15))
                        rep.save()

            if n.wiki:
                action = orm.Action(user=n.author,
                                    node=n,
                                    action_type="wikify",
                                    action_date=a.wikified_at
                                    or datetime.datetime.now(),
                                    extra='')

                action.save()

            progress.update()

        print "\n...done\n"

        v_count = orm.Vote.objects.count()
        print "\nConverting %d votes:" % v_count
        progress = ProgressBar(v_count)

        for v in orm.Vote.objects.exclude(canceled=True):
            a = orm.Action(action_type=(v.vote == 1) and
                           ((v.node.node_type == "comment") and "voteupcomment"
                            or "voteup") or "votedown",
                           user=v.user,
                           node=v.node,
                           action_date=v.voted_at,
                           canceled=v.canceled,
                           extra='')

            a.save()

            def impl(node):
                if node.node_type == "question":
                    return orm.Question.objects.get(node_ptr=node)
                else:
                    return orm.Answer.objects.get(node_ptr=node)

            if a.node.node_type in ("question", "answer") and (
                    not a.node.wiki
                    or impl(a.node).wikified_at > a.action_date):
                reptype, default = (v.vote == 1) and (GAIN_BY_UPVOTED, 10) or (
                    LOST_BY_DOWNVOTED, 2)
                rep = orm.ActionRepute(
                    action=a,
                    user=a.node.author,
                    value=repval_at(reptype, a.action_date, default)
                    or default)
                rep.save()

                if v.vote == -1:
                    rep = orm.ActionRepute(
                        action=a,
                        user=a.node.author,
                        value=repval_at(LOST_BY_DOWNVOTING, a.action_date, 1)
                        or default)
                    rep.save()

            progress.update()

        print "\n...done\n"

        f_count = orm.FlaggedItem.objects.count()
        print "\nConverting %d flags:" % f_count
        progress = ProgressBar(f_count)

        for f in orm.FlaggedItem.objects.all():
            a = orm.Action(action_type="flag",
                           user=f.user,
                           node=f.node,
                           action_date=f.flagged_at,
                           extra=f.reason or '')

            a.save()

            rep = orm.ActionRepute(
                action=a,
                user=a.node.author,
                value=repval_at(LOST_BY_FLAGGED, a.action_date, 2) or 2)
            rep.save()

            progress.update()

        print "\n...done\n"

        n_count = orm.Node.objects.all().count()
        print "\nChecking flag count of %d nodes:" % n_count
        progress = ProgressBar(n_count)

        for n in orm.Node.objects.all():
            flags = list(
                orm.Action.objects.filter(
                    action_type="flag", node=n,
                    canceled=False).order_by('-action_date'))

            if len(flags) >= 3:
                a = flags[2]
                rep = orm.ActionRepute(action=a,
                                       user=n.author,
                                       value=repval_at(LOST_BY_FLAGGED_3_TIMES,
                                                       a.action_date, 30))
                rep.save()

            if len(flags) >= 5:
                a = flags[4]
                rep = orm.ActionRepute(action=a,
                                       user=n.author,
                                       value=repval_at(LOST_BY_FLAGGED_5_TIMES,
                                                       a.action_date, 100))
                rep.save()

            progress.update()

        print "\n...done\n"

        c_count = orm.Node.objects.filter(node_type="comment").count()
        print "\nCreating %d comment actions:" % c_count
        progress = ProgressBar(c_count)

        for c in orm.Node.objects.filter(node_type="comment").all():
            a = orm.Action(action_type="comment",
                           user=c.author,
                           node=c,
                           action_date=c.added_at,
                           extra='')

            a.save()

            if c.deleted:
                action = orm.Action(user=c.deleted_by,
                                    node=c,
                                    action_type="delete",
                                    action_date=c.deleted_at
                                    or datetime.datetime.now(),
                                    extra='')

                action.save()

            progress.update()

        print "\n...done\n"

        r_count = orm.NodeRevision.objects.exclude(revision=1).count()
        print "\nCreating %d edit actions:" % r_count
        progress = ProgressBar(r_count)

        for r in orm.NodeRevision.objects.exclude(revision=1):
            a = orm.Action(action_type="revise",
                           user=r.author,
                           node=r.node,
                           action_date=r.revised_at,
                           extra=r.revision)

            a.save()
            progress.update()

        print "\n...done\n"

        u_count = orm.User.objects.all().count()
        print "\nCreating %d user join actions and reputation recalculation:" % u_count
        progress = ProgressBar(u_count)

        for u in orm.User.objects.all():
            a = orm.Action(
                user=u,
                action_date=u.date_joined,
                action_type="userjoins",
            )

            a.save()

            rep = orm.ActionRepute(action=a, user=u, value=1)
            rep.save()

            new_rep = orm.ActionRepute.objects.filter(user=u).aggregate(
                reputation=models.Sum('value'))['reputation']

            if new_rep < 0:
                new_rep = 1

            u.reputation = new_rep
            u.save()

            progress.update()

        print "\n...done\n"
    def forwards(self, orm):
        c_count = orm.Comment.objects.count()
        print "\nConverting %d comments:" % c_count
        progress = ProgressBar(c_count)

        id_map = {}

        for c in orm.Comment.objects.all():
            node = orm.Node(
                node_type='comment',
                author=c.user,
                added_at=c.added_at,
                score=c.score,
                vote_up_count=c.score,
                vote_down_count=0,
                offensive_flag_count=0,
                last_edited_at=c.added_at,
                last_edited_by=c.user,
                body=c.comment,
                deleted=c.deleted,
                deleted_by=c.deleted_by,
                deleted_at=c.deleted_at,
                parent=c.node,
            )
            node.save()

            id_map[c.id] = node

            revision = orm.NodeRevision(
                summary="Initial revsion",
                revision=1,
                revised_at=c.added_at,
                body=c.comment,
                author=c.user,
                node=node,
            )

            revision.save()

            node.active_revision = revision
            node.save()

            for v in orm.LikedComment.objects.filter(comment=c):
                vote = orm.Vote(
                    node=node,
                    vote=1,
                    voted_at=v.added_at,
                    canceled=v.canceled,
                    user=v.user,
                )

                vote.save()

            progress.update()

        print "\n...done\n"

        if 'contenttypes.ContentType' in orm:
            ctype = orm['contenttypes.ContentType'].objects.get(name="comment").id
            ntype = orm['contenttypes.ContentType'].objects.get(name="node").id

            if ctype:
                #Converting activity
                activities = orm.Activity.objects.filter(content_type__id=ctype)
                activity_count = activities.count()
                print "Converting %d activity references:" % activity_count
                progress = ProgressBar(activity_count)

                for a in activities:
                    node = id_map.get(a.object_id, None)
                    if node:
                        a.content_type_id = ntype
                        a.object_id = node.id
                        a.save()

                    progress.update()

        print "\n...done\n"
    def forwards(self, orm):
        #Converting questions
        question_count = orm.Question.objects.all().count()
        print "\nConverting %d questions:" % question_count
        progress = ProgressBar(question_count)

        question_id_map = {}

        for q in orm.Question.objects.order_by('id'):
            node = orm.Node(
                node_type='question',
                author=q.author,
                added_at=q.added_at,
                score=q.score,
                vote_up_count=q.vote_up_count,
                vote_down_count=q.vote_down_count,
                offensive_flag_count=q.offensive_flag_count,
                last_edited_at=q.last_edited_at,
                last_edited_by=q.last_edited_by,
                title=q.title,
                body=q.html,
                deleted=q.deleted,
                deleted_by=q.deleted_by,
                deleted_at=q.deleted_at,
                tagnames=q.tagnames,
                comment_count=q.comment_count,
            )

            node.save()
            q.node_ptr = node
            q.save()
            node.tags = q.tags.all()

            question_id_map[q.id] = node
            progress.update()

        print "\n...done\n"

        #Converting answers
        answer_count = orm.Answer.objects.all().count()
        print "Converting %d answers:" % answer_count
        progress = ProgressBar(answer_count)

        answer_id_map = {}

        for a in orm.Answer.objects.order_by('id'):
            node = orm.Node(
                node_type='answer',
                parent=question_id_map[a.question.id],
                author=a.author,
                added_at=a.added_at,
                score=a.score,
                vote_up_count=a.vote_up_count,
                vote_down_count=a.vote_down_count,
                offensive_flag_count=a.offensive_flag_count,
                last_edited_at=a.last_edited_at,
                last_edited_by=a.last_edited_by,
                body=a.html,
                deleted=a.deleted,
                deleted_by=a.deleted_by,
                deleted_at=a.deleted_at,
                comment_count=a.comment_count,
            )

            node.save()
            a.node_ptr = node
            a.save()
            answer_id_map[a.id] = node
            progress.update()

        print "\n...done\n"

        ctypes = dict([(ct.name, ct.id) for ct in orm['contenttypes.ContentType'].objects.all()])

        #Converting votes
        vote_count = orm.Vote.objects.all().count()
        print "Converting %d vote references:" % vote_count
        progress = ProgressBar(vote_count)
        orfan_count = 0

        for v in orm.Vote.objects.all():
            node = (v.content_type.id == ctypes['question']) and question_id_map.get(v.object_id, None) \
                                                             or answer_id_map.get(v.object_id, None)

            if node:
                v.node = node
                v.save()
            else:
                v.delete()
                orfan_count += 1

            progress.update()

        if orfan_count:
            print "Deleted %d orfan votes"

        print "\n...done\n"

        #Converting flags
        flag_count = orm.FlaggedItem.objects.all().count()
        print "Converting %d flag references:" % flag_count
        progress = ProgressBar(flag_count)
        orfan_count = 0

        for f in orm.FlaggedItem.objects.all():
            node = (f.content_type.id == ctypes['question']) and question_id_map.get(f.object_id, None) \
                                                             or answer_id_map.get(f.object_id, None)

            if node:
                f.node = node
                f.save()
            else:
                f.delete()
                orfan_count += 1

            progress.update()

        if orfan_count:
            print "Deleted %d orfan votes"

        print "\n...done\n"

        #Converting comments
        comment_count = orm.Comment.objects.all().count()
        print "Converting %d comment references:" % comment_count
        progress = ProgressBar(comment_count)
        orfan_count = 0

        for c in orm.Comment.objects.all():
            node = (c.content_type.id == ctypes['question']) and question_id_map.get(c.object_id, None) \
                                                             or answer_id_map.get(c.object_id, None)

            if node:
                c.node = node
                c.save()
            else:
                c.delete()
                orfan_count += 1

            progress.update()

        if orfan_count:
            print "Deleted %d orfan comments"

        print "\n...done\n"


        #Converting awards
        awards = orm.Award.objects.filter(content_type__id__in=(ctypes['question'], ctypes['answer']))
        award_count = awards.count()
        print "Converting %d award references:" % award_count
        progress = ProgressBar(award_count)

        for a in awards:
            node = (a.content_type.id == ctypes['question']) and question_id_map.get(a.object_id, None) \
                                                             or answer_id_map.get(a.object_id, None)

            if node:
                a.object_id = node.id
                a.save()

            progress.update()

        print "\n...done\n"


        #Converting activity
        activities = orm.Activity.objects.filter(content_type__id__in=(ctypes['question'], ctypes['answer']))
        activity_count = activities.count()
        print "Converting %d activity references:" % activity_count
        progress = ProgressBar(activity_count)

        for a in activities:
            node = (a.content_type.id == ctypes['question']) and question_id_map.get(a.object_id, None) \
                                                             or answer_id_map.get(a.object_id, None)

            if node:
                a.object_id = node.id
                a.save()

            progress.update()

        print "\n...done\n"
Esempio n. 22
0
    def forwards(self, orm):
        b_count = orm.Badge.objects.count()
        print "\nConverting %d badges:" % b_count
        progress = ProgressBar(b_count)

        for b in orm.Badge.objects.all():
            b.cls = "".join([s[0].upper() + s[1:] for s in b.slug.split('-')])
            b.save()

            progress.update()

        print "\n...done\n"

        ctypes = dict([(ct.name, ct.id) for ct in orm['contenttypes.ContentType'].objects.all()])

        a_count = orm.Award.objects.count()
        print "\nConverting %d awards:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Award.objects.all():
            if a.content_type.id == ctypes['user']:
                a.node = None
            else:
                try:
                    a.node = orm.Node.objects.get(id=a.object_id)
                except:
                    a.delete()
                    continue

            action = orm.Action(
                user = a.user,
                node = a.node,
                action_type = "award",
                action_date = a.awarded_at,
            )

            action.save()

            a.action = action
            a.save()

            progress.update()

        print "\n...done\n"


        a_count = orm.Action.objects.filter(action_type__in=("voteup", "votedown", "voteupcomment")).count()
        print "\nConverting %d votes:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.filter(action_type__in=("voteup", "votedown", "voteupcomment"), canceled=False):
            v = orm.Vote(
                user = a.user,
                node = a.node,
                value = (a.action_type in ("voteup", "voteupcomment")) and 1 or -1,
                action = a,
                voted_at = a.action_date
            )

            v.save()

            progress.update()

        print "\n...done\n"

        a_count = orm.Action.objects.filter(action_type__in=("voteup", "votedown", "voteupcomment")).count()
        print "\nConverting %d votes:" % a_count
        progress = ProgressBar(a_count)

        for a in orm.Action.objects.filter(action_type="flag", canceled=False):
            f = orm.Flag(
                user = a.user,
                node = a.node,
                reason = a.extra,
                action = a,
                flagged_at = a.action_date
            )

            f.save()

            progress.update()

        print "\n...done\n"