示例#1
0
def copy_monitoring(monitoring_pk):
    m_src = em.Monitoring.objects.get(pk = monitoring_pk)
    #create monitoring
    m_name = '%s_new' % m_src.name
    m = deepcopy(m_src)
    m.pk = None
    m.name = m_name
    m.status = em.MONITORING_RATE
    m.save()
    print m.pk

    organization_map = {}
    parameter_map = {}

    #tasks
    tasks = em.Task.objects.filter(organization__monitoring = m_src, status= em.Task.TASK_APPROVED)
    for t in tasks:
        org = deepcopy(t.organization)
        org.pk = None
        org.monitoring = m
        org.inv_code = em.generate_inv_code(6)
        org.save()
        organization_map[t.organization.pk] = org.pk
        task = deepcopy(t)
        task.pk = None
        task.organization = org
        task.save()
        scores = em.Score.objects.filter(task = t)
        for s in scores:
            if em.Parameter.objects.filter(
                monitoring = m,
                code = s.parameter.code,
            ).exists():
                param = em.Parameter.objects.get(
                    monitoring = m,
                    code = s.parameter.code)
            else:
                param = deepcopy(s.parameter)
                param.pk = None
                param.monitoring = m
                param.save()
                param.exclude.clear()
                parameter_map[s.parameter.pk] = param.pk
            score = deepcopy(s)
            score.pk = None
            score.task = task
            score.parameter = param
            score.save()

            if NEED_COMMENT:
                comments = Comment.objects.for_model(em.Score).filter(object_pk=s.pk)
                for c in comments:
                    comment = deepcopy(c)
                    comment.pk = None
                    comment.object_pk = score.pk
                    comment.save()

    for param_pk, new_param_pk in parameter_map.iteritems():
        param = em.Parameter.objects.get(pk=param_pk)
        new_param = em.Parameter.objects.get(pk=new_param_pk)
        for org_pk in param.exclude.values_list('pk', flat=True):
            if organization_map.has_key(org_pk):
                new_param.exclude.add(em.Organization.objects.get(pk=organization_map[org_pk]))
示例#2
0
    def form_valid(self, form):
        with transaction.commit_on_success():
            origin_monitoring = self.get_object()
            self.monitoring = form.save()
            cd = form.cleaned_data

            if cd.get('add_questionnaire'):
                Questionnaire.objects.create(monitoring=self.monitoring)

            organization_map = {}
            task_map = {}
            parameter_map = {}

            orgs = Organization.objects.filter(monitoring=origin_monitoring)
            for org in orgs:
                copied_org = deepcopy(org)
                copied_org.id = None
                copied_org.monitoring = self.monitoring
                copied_org.inv_status = 'NTS'
                copied_org.inv_code = generate_inv_code(6)
                copied_org.save()
                organization_map[org.id] = copied_org.id

            if 'tasks' in cd.get('donors', []):
                for task in Task.objects.filter(organization__monitoring=origin_monitoring):
                    copied_task = deepcopy(task)
                    copied_task.id = None
                    copied_task.organization_id = organization_map[task.organization_id]
                    copied_task.save()
                    task_map[task.id] = copied_task.id

            if 'parameters' in cd.get('donors', []):
                for param in Parameter.objects.filter(monitoring=origin_monitoring):
                    copied_param = deepcopy(param)
                    copied_param.id = None
                    copied_param.monitoring = self.monitoring
                    copied_param.save()
                    copied_param.exclude.add(*[organization_map[x] for x in param.exclude.values_list('pk', flat=True)])
                    parameter_map[param.id] = copied_param.id

            scores = None
            if 'all_scores' in cd.get('donors', []):
                scores = Score.objects.filter(task__organization__monitoring=origin_monitoring,
                                              parameter__monitoring=origin_monitoring)
            elif 'current_scores' in cd.get('donors', []):
                scores = Score.objects.filter(task__organization__monitoring=origin_monitoring,
                                              parameter__monitoring=origin_monitoring,
                                              revision=Score.FINAL)
            if scores:
                bulk_of_scores = []
                for score in scores:
                    copied_score = deepcopy(score)
                    copied_score.id = None
                    copied_score.task_id = task_map[score.task_id]
                    copied_score.parameter_id = parameter_map[score.parameter_id]
                    bulk_of_scores.append(copied_score)

                # Disable 'auto_now' option to copy 'last_modified' field as is
                for field in Score._meta.local_fields:
                    if field.name == 'last_modified':
                        field.auto_now = False
                # Then create all scores in one query
                Score.objects.bulk_create(bulk_of_scores, batch_size=3000)
                # And restore 'auto_now' option
                for field in Score._meta.local_fields:
                    if field.name == 'last_modified':
                        field.auto_now = True

            if 'representatives' in cd.get('donors', []):
                orgusers = UserProfile.objects.filter(organization__monitoring=origin_monitoring)
                for user in orgusers:
                    org_pks = user.organization.values_list('pk', flat=True)
                    for pk in org_pks:
                        if pk in organization_map:
                            if not OrgUser.objects.filter(organization_id=organization_map[pk], userprofile=user).exists():
                                orguser = OrgUser(organization_id=organization_map[pk], userprofile=user, seen=False)
                                orguser.save()

        return HttpResponseRedirect(self.get_success_url())
示例#3
0
def monitoring_organization_import(request, monitoring_pk):
    """
    Импорт организаций из CSV.

    """
    must_register = False
    if revision.is_registered(Organization):
        revision.unregister(Organization)
        must_register = True

    monitoring = get_object_or_404(Monitoring, pk=monitoring_pk)
    if not request.user.has_perm('exmo2010.edit_monitoring', monitoring):
        raise PermissionDenied
    if 'orgfile' not in request.FILES:
        return HttpResponseRedirect(reverse('exmo2010:monitorings_list'))
    reader = UnicodeReader(request.FILES['orgfile'])
    errors = []
    indexes = {}
    rowOKCount = 0
    row_num = 0
    try:
        for row_num, row in enumerate(reader, start=1):
            if row[0] and row[0].startswith('#'):
                for key in ['name', 'url', 'email', 'phone', 'recommendations_hidden']:
                    for item in row:
                        if item and key in item.lower():
                            indexes[key] = row.index(item)
                errors.append("row %d. Starts with '#'. Skipped" % row_num)
                continue

            if 'name' not in indexes:
                errors.append("header row (csv). Field 'Name' does not exist")
                break

            if row[indexes['name']] == '':
                errors.append("row %d (csv). Empty organization name" % row_num)
                continue
            try:
                organization = Organization.objects.get(monitoring=monitoring, name=row[indexes['name']])
            except Organization.DoesNotExist:
                organization = Organization(name=row[indexes['name']])
                organization.monitoring = monitoring
            except Exception, e:
                errors.append("row %d. %s" % (row_num, e))
                continue
            try:
                if row[indexes['email']]:
                    organization.email = replace_string(row[indexes['email']]).strip()
                if row[indexes['phone']]:
                    organization.phone = replace_string(row[indexes['phone']]).strip()
                if row[indexes['url']]:
                    organization.url = row[indexes['url']].strip()
                if row[indexes['recommendations_hidden']] == 'False':
                    organization.recommendations_hidden = False
                if row[indexes['recommendations_hidden']] == 'True':
                    organization.recommendations_hidden = True

                organization.inv_code = generate_inv_code(6)
                organization.full_clean()
                organization.save()
            except ValidationError, e:
                errors.append("row %d (validation). %s" % (
                    row_num,
                    '; '.join(['%s: %s' % (i[0], ', '.join(i[1])) for i in e.message_dict.items()])))
            except Exception, e:
                errors.append("row %d. %s" % (row_num, e))
示例#4
0
文件: views.py 项目: btbroot/exmo2010
    def form_valid(self, form):
        with transaction.commit_on_success():
            origin_monitoring = self.get_object()
            self.monitoring = form.save()
            cd = form.cleaned_data

            if cd.get('add_questionnaire'):
                Questionnaire.objects.create(monitoring=self.monitoring)

            organization_map = {}
            task_map = {}
            parameter_map = {}

            orgs = Organization.objects.filter(monitoring=origin_monitoring)
            for org in orgs:
                copied_org = deepcopy(org)
                copied_org.id = None
                copied_org.monitoring = self.monitoring
                copied_org.inv_status = 'NTS'
                copied_org.inv_code = generate_inv_code(6)
                copied_org.save()
                organization_map[org.id] = copied_org.id

            if 'tasks' in cd.get('donors', []):
                for task in Task.objects.filter(
                        organization__monitoring=origin_monitoring):
                    copied_task = deepcopy(task)
                    copied_task.id = None
                    copied_task.organization_id = organization_map[
                        task.organization_id]
                    copied_task.save()
                    task_map[task.id] = copied_task.id

            if 'parameters' in cd.get('donors', []):
                for param in Parameter.objects.filter(
                        monitoring=origin_monitoring):
                    copied_param = deepcopy(param)
                    copied_param.id = None
                    copied_param.monitoring = self.monitoring
                    copied_param.save()
                    copied_param.exclude.add(*[
                        organization_map[x]
                        for x in param.exclude.values_list('pk', flat=True)
                    ])
                    parameter_map[param.id] = copied_param.id

            scores = None
            if 'all_scores' in cd.get('donors', []):
                scores = Score.objects.filter(
                    task__organization__monitoring=origin_monitoring,
                    parameter__monitoring=origin_monitoring)
            elif 'current_scores' in cd.get('donors', []):
                scores = Score.objects.filter(
                    task__organization__monitoring=origin_monitoring,
                    parameter__monitoring=origin_monitoring,
                    revision=Score.FINAL)
            if scores:
                bulk_of_scores = []
                for score in scores:
                    copied_score = deepcopy(score)
                    copied_score.id = None
                    copied_score.task_id = task_map[score.task_id]
                    copied_score.parameter_id = parameter_map[
                        score.parameter_id]
                    bulk_of_scores.append(copied_score)

                # Disable 'auto_now' option to copy 'last_modified' field as is
                for field in Score._meta.local_fields:
                    if field.name == 'last_modified':
                        field.auto_now = False
                # Then create all scores in one query
                Score.objects.bulk_create(bulk_of_scores, batch_size=3000)
                # And restore 'auto_now' option
                for field in Score._meta.local_fields:
                    if field.name == 'last_modified':
                        field.auto_now = True

            if 'representatives' in cd.get('donors', []):
                orgusers = UserProfile.objects.filter(
                    organization__monitoring=origin_monitoring)
                for user in orgusers:
                    org_pks = user.organization.values_list('pk', flat=True)
                    for pk in org_pks:
                        if pk in organization_map:
                            if not OrgUser.objects.filter(
                                    organization_id=organization_map[pk],
                                    userprofile=user).exists():
                                orguser = OrgUser(
                                    organization_id=organization_map[pk],
                                    userprofile=user,
                                    seen=False)
                                orguser.save()

        return HttpResponseRedirect(self.get_success_url())