<commit_before># -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models

from django_fixmystreet.fixmystreet.models import Report

def get_contact_user(report):
    if not report.responsible_department:
        return

    memberships = report.responsible_department.memberships.filter(contact_user=True)
    if not memberships:
        return

    return memberships[0].user

class Migration(DataMigration):

    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."

        to_migrate = orm['fixmystreet.Report'].objects.filter(refusal_motivation__isnull=False)

        for report in to_migrate:
            contact_user = get_contact_user(report)

            print 'Report', report.id
            print '   Motivation', report.refusal_motivation
            print '   User', contact_user

            comment        = orm['fixmystreet.ReportComment']()
            comment.text   = report.refusal_motivation

            if contact_user:
                comment.user   = contact_user

            comment.report = report
            comment.save()

            report.refusal_comment = comment
            report.save()

        print 'refusal_comment migrated:', len(to_migrate)

    def backwards(self, orm):
        "Write your backwards methods here."
        to_delete = orm['fixmystreet.Report'].objects.filter(refusal_motivation__isnull=False)

        for report in to_delete:
            contact_user = get_contact_user(report)

            print 'Report', report.id
            print '   Motivation', report.refusal_comment
            print '   User', contact_user

            report.refusal_comment.delete()

        print 'refusal_comment deleted:', len(to_delete)

    models = {
        u'auth.group': {
            'Meta': {'object_name': 'Group'},
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})<commit_msg>FMS-280 - Fix datamigration of refusal<commit_after># -*- coding: utf-8 -*-
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models

from django_fixmystreet.fixmystreet.models import Report

def get_contact_user(report):
    if not report.responsible_department:
        return

    memberships = report.responsible_department.memberships.filter(contact_user=True)
    if not memberships:
        return

    return memberships[0].user

class Migration(DataMigration):

    def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."

        to_migrate = orm['fixmystreet.Report'].objects.filter(refusal_motivation__isnull=False)

        for report in to_migrate:
            contact_user = get_contact_user(report)

            print 'Report', report.id
            print '   Motivation', report.refusal_motivation
            print '   User', contact_user

            if report.refusal_motivation and contact_user:
                comment        = orm['fixmystreet.ReportComment']()
                comment.text   = report.refusal_motivation
                comment.user   = contact_user
                comment.report = report
                comment.save()

                report.refusal_comment = comment
                report.save()

        print 'refusal_comment migrated:', len(to_migrate)

    def backwards(self, orm):
        "Write your backwards methods here."
        to_delete = orm['fixmystreet.Report'].objects.filter(refusal_motivation__isnull=False, refusal_comment__isnull=False)

        for report in to_delete:
            contact_user = get_contact_user(report)

            print 'Report', report.id
            print '   Motivation', report.refusal_comment
            print '   User', contact_user

            report.refusal_comment.delete()

        print 'refusal_comment deleted:', len(to_delete)

    models = {
        u'auth.group': {
            'Meta': {'object_name': 'Group'},
            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
{'fraction_non_alphanumeric': 0.11306001690617075, 'fraction_numerical': 0.0027472527472527475, 'mean_word_length': 2.601978691019787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5940238', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1303, 'n_words': 290}