コード例 #1
0
ファイル: projects.py プロジェクト: wanghp18/seed
def delete_matching_buildings(project, buildings,
                              select_all, search_params, user):
    """deletes buildings in a project that match search search params

    :param project_slug: str, a slug to get a Project inst.
    :param buildings: list, list of source_facility_id as str to get \
    BuildingSnapshot inst.
    :param select_all: bool, if the select all checkbox was \
    checked. i.e. should we transfer all buildings in a project or just the \
    buildings in the list
    :search_params: dict, params needed to generate a queryset of buildings, \
    with keys (q, other_params, project_slug)
    :user: User inst., django user instance needed for select all queryset

    """
    if select_all:
        # get all the buildings for a project minus unselected ones
        qs = get_search_query(user, search_params)
        ProjectBuilding.objects.filter(
            building_snapshot__in=qs,
            project=project
        ).exclude(building_snapshot__pk__in=buildings).delete()
    else:
        ProjectBuilding.objects.filter(
            building_snapshot__pk__in=buildings,
            project=project
        ).delete()
コード例 #2
0
ファイル: projects.py プロジェクト: wanghp18/seed
def get_transfer_buildings(source_project, target_project, buildings,
                           select_all, search_params, user):
    """generates move or copy buildings queryset

    :param source_project_slug: str, a slug to get a Project inst.
    :param target_project_slug: str, a slug to get a Project inst.
    :param buildings: list, list of source_facility_id as str to get \
    BuildingSnapshot inst.
    :param select_all: bool, if the select all checkbox was \
    checked. i.e. should we transfer all buildings in a project or just the \
    buildings in the list
    :search_params: dict, params needed to generate a queryset of buildings, \
    with keys (q, other_params, project_slug)
    :user: User inst., django user instance needed for select all queryset

    :rtype Queryset: a django queryset of buildings to move or copy

    """
    if select_all:
        # get all the buildings for a project minus unselected ones
        qs = get_search_query(user, search_params)
        pb_queryset = ProjectBuilding.objects.filter(
            building_snapshot__in=qs,
            project=source_project
        ).exclude(building_snapshot__pk__in=buildings)
    else:
        pb_queryset = ProjectBuilding.objects.filter(
            building_snapshot__pk__in=buildings,
            project=source_project
        )
    return pb_queryset
コード例 #3
0
def get_transfer_buildings(source_project, target_project, buildings,
                           select_all, search_params, user):
    """generates move or copy buildings queryset

       :param source_project_slug: str, a slug to get a Project inst.
       :param target_project_slug: str, a slug to get a Project inst.
       :param buildings: list, list of source_facility_id as str to get
       BuildingSnapshot inst.
       :param select_all: bool, if the select all checkbox was
       checked. i.e. should we transfer all buildings in a project or just the
       buildings in the list
       :search_params: dict, params needed to generate a queryset of buildings,
       with keys (q, other_params, project_slug)
       :user: User inst., django user instance needed for select all queryset

       :rtype Queryset: a django queryset of buildings to move or copy

    """
    if select_all:
        # get all the buildings for a project minus unselected ones
        qs = get_search_query(user, search_params)
        pb_queryset = ProjectBuilding.objects.filter(
            building_snapshot__in=qs,
            project=source_project
        ).exclude(building_snapshot__pk__in=buildings)
    else:
        pb_queryset = ProjectBuilding.objects.filter(
            building_snapshot__pk__in=buildings,
            project=source_project
        )
    return pb_queryset
コード例 #4
0
def delete_matching_buildings(project, buildings,
                              select_all, search_params, user):
    """deletes buildings in a project that match search search params

       :param project_slug: str, a slug to get a Project inst.
       :param buildings: list, list of source_facility_id as str to get
       BuildingSnapshot inst.
       :param select_all: bool, if the select all checkbox was
       checked. i.e. should we transfer all buildings in a project or just the
       buildings in the list
       :search_params: dict, params needed to generate a queryset of buildings,
       with keys (q, other_params, project_slug)
       :user: User inst., django user instance needed for select all queryset

    """
    if select_all:
        # get all the buildings for a project minus unselected ones
        qs = get_search_query(user, search_params)
        ProjectBuilding.objects.filter(
            building_snapshot__in=qs,
            project=project
        ).exclude(building_snapshot__pk__in=buildings).delete()
    else:
        ProjectBuilding.objects.filter(
            building_snapshot__pk__in=buildings,
            project=project
        ).delete()
コード例 #5
0
ファイル: projects.py プロジェクト: pipermerriam/seed
def apply_label(project_slug, buildings, select_all, label, search_params,
                user):
    """adds or updates a label for a ProjectBuilding related to a
       project and building in the buildings list of source_facility_ids

       :param project_slug: str, a slug to get a Project inst.
       :param buildings: list, list of source_facility_id as str to get
       BuildingSnapshot inst.
       :param select_all: bool, if the select all checkbox was
       checked. i.e. should we get all buildings or just the ones in the list
       :param label: dict, dict of a info to get a StatusLabel inst., if label
       if an empty dict, apply_label will remove the label
       :search_params: dict, params needed to generate a queryset of buildings,
       with keys (q, other_params, project_slug)

    """
    if 'id' in label:
        label = StatusLabel.objects.get(pk=label['id'])
    else:
        label = None

    if select_all:
        # get all the buildings for a project minus unselected ones
        qs = get_search_query(user, search_params)
        pb_qs = ProjectBuilding.objects.filter(
            building_snapshot__in=qs, project__slug=project_slug).exclude(
                building_snapshot__pk__in=buildings)
    else:
        # just add selected buildings
        pb_qs = ProjectBuilding.objects.filter(
            building_snapshot__pk__in=buildings,
            project__slug=project_slug,
        )
    pb_qs.update(status_label=label)
コード例 #6
0
ファイル: projects.py プロジェクト: gunduru/seed
def apply_label(project_slug, buildings, select_all, label, search_params,
                user):
    """adds or updates a label for a ProjectBuilding related to a
       project and building in the buildings list of source_facility_ids

       :param project_slug: str, a slug to get a Project inst.
       :param buildings: list, list of source_facility_id as str to get
       BuildingSnapshot inst.
       :param select_all: bool, if the select all checkbox was
       checked. i.e. should we get all buildings or just the ones in the list
       :param label: dict, dict of a info to get a StatusLabel inst., if label
       if an empty dict, apply_label will remove the label
       :search_params: dict, params needed to generate a queryset of buildings,
       with keys (q, other_params, project_slug)

    """
    if 'id' in label:
        label = StatusLabel.objects.get(pk=label['id'])
    else:
        label = None

    if select_all:
        # get all the buildings for a project minus unselected ones
        qs = get_search_query(user, search_params)
        pb_qs = ProjectBuilding.objects.filter(
            building_snapshot__in=qs,
            project__slug=project_slug
        ).exclude(
            building_snapshot__pk__in=buildings
        )
    else:
        # just add selected buildings
        pb_qs = ProjectBuilding.objects.filter(
            building_snapshot__pk__in=buildings,
            project__slug=project_slug,
        )
    pb_qs.update(
        status_label=label
    )
コード例 #7
0
ファイル: tasks.py プロジェクト: omidtarh/omidtarh
def remove_buildings(project_slug, project_dict, user_pk):
    """adds buildings to a project. if a user has selected all buildings,
       then the the search parameters within project_dict are used to determine
       the total set of buildings.

       :param str project_slug: a project's slug used to get the project
       :param dict project_dict: contains search params, and browser state
           information
       :user_pk int or str: the user's pk or id
    """
    project = Project.objects.get(slug=project_slug)
    user = User.objects.get(pk=user_pk)
    project.last_modified_by = user
    project.save()

    selected_buildings = project_dict.get('selected_buildings', [])

    set_cache_raw(
        project.removing_buildings_status_percentage_cache_key,
        {'percentage_done': 0, 'numerator': 0, 'denominator': 0}
    )
    i = 0
    denominator = 1
    if not project_dict.get('select_all_checkbox', False):
        for sfid in selected_buildings:
            i += 1
            denominator = len(selected_buildings)
            set_cache_raw(
                project.removing_buildings_status_percentage_cache_key,
                {
                    'percentage_done': (
                        float(i) / max(len(selected_buildings), 1) * 100
                    ),
                    'numerator': i,
                    'denominator': denominator
                }
            )
            ab = BuildingSnapshot.objects.get(pk=sfid)
            ProjectBuilding.objects.get(
                project=project, building_snapshot=ab
            ).delete()
    else:
        query_buildings = get_search_query(user, project_dict)
        denominator = query_buildings.count() - len(selected_buildings)
        set_cache_raw(
            project.adding_buildings_status_percentage_cache_key,
            {
                'percentage_done': 10,
                'numerator': i,
                'denominator': denominator
            }
        )
        for b in query_buildings:
            ProjectBuilding.objects.get(
                project=project, building_snapshot=b
            ).delete()
        set_cache_raw(
            project.adding_buildings_status_percentage_cache_key,
            {
                'percentage_done': 50,
                'numerator': denominator - len(selected_buildings),
                'denominator': denominator
            }
        )
        for building in selected_buildings:
            i += 1
            ab = BuildingSnapshot.objects.get(source_facility_id=building)
            ProjectBuilding.objects.create(
                project=project, building_snapshot=ab
            )
            set_cache_raw(
                project.adding_buildings_status_percentage_cache_key,
                {
                    'percentage_done': (
                        float(denominator - len(selected_buildings) + i) /
                        denominator * 100
                    ),
                    'numerator': denominator - len(selected_buildings) + i,
                    'denominator': denominator
                }
            )

    set_cache_raw(
        project.removing_buildings_status_percentage_cache_key,
        {'percentage_done': 100, 'numerator': i, 'denominator': denominator}
    )
コード例 #8
0
ファイル: tasks.py プロジェクト: omidtarh/omidtarh
def add_buildings(project_slug, project_dict, user_pk):
    """adds buildings to a project. if a user has selected all buildings,
       then the the search parameters within project_dict are used to determine
       the total set
       of buildings.
       also creates a Compliance inst. if satisfying params are present

       :param str project_slug: a project's slug used to get the project
       :param dict project_dict: contains search params, and browser state
       information
       :user_pk int or str: the user's pk or id

    """
    project = Project.objects.get(slug=project_slug)
    user = User.objects.get(pk=user_pk)
    project.last_modified_by = user
    project.save()

    selected_buildings = project_dict.get('selected_buildings', [])

    set_cache_raw(
        project.adding_buildings_status_percentage_cache_key,
        {'percentage_done': 0, 'numerator': 0, 'denominator': 0}
    )
    i = 0
    denominator = 1
    if not project_dict.get('select_all_checkbox', False):
        for sfid in selected_buildings:
            i += 1
            denominator = len(selected_buildings)
            try:
                set_cache_raw(
                    project.adding_buildings_status_percentage_cache_key,
                    {
                        'percentage_done': (
                            float(i) / len(selected_buildings) * 100
                        ),
                        'numerator': i, 'denominator': denominator
                    }
                )
            except ZeroDivisionError:
                pass
            ab = BuildingSnapshot.objects.get(pk=sfid)
            ProjectBuilding.objects.get_or_create(
                project=project, building_snapshot=ab
            )
    else:
        query_buildings = get_search_query(user, project_dict)
        denominator = query_buildings.count() - len(selected_buildings)
        set_cache_raw(
            project.adding_buildings_status_percentage_cache_key,
            {'percentage_done': 10, 'numerator': i, 'denominator': denominator}
        )
        i = 0
        for b in query_buildings:
            # todo: only get back query_buildings pks as a list, and create
            # using the pk,
            #       not the python object
            i += 1
            ProjectBuilding.objects.get_or_create(
                project=project, building_snapshot=b
            )
            set_cache_raw(
                project.adding_buildings_status_percentage_cache_key,
                {
                    'percentage_done': float(i) / denominator * 100,
                    'numerator': i, 'denominator': denominator
                }
            )
        for building in selected_buildings:
            i += 1
            project.building_snapshots.remove(
                BuildingSnapshot.objects.get(pk=building)
            )
            set_cache_raw(
                project.adding_buildings_status_percentage_cache_key,
                {
                    'percentage_done': (
                        float(denominator - len(selected_buildings) + i) /
                        denominator * 100
                    ),
                    'numerator': denominator - len(selected_buildings) + i,
                    'denominator': denominator
                }
            )

    set_cache_raw(
        project.adding_buildings_status_percentage_cache_key,
        {'percentage_done': 100, 'numerator': i, 'denominator': denominator}
    )

    deadline_date = project_dict.get('deadline_date')
    if isinstance(deadline_date, (int, float)):
        deadline_date = datetime.datetime.fromtimestamp(deadline_date / 1000)
    elif isinstance(deadline_date, basestring):
        deadline_date = parser.parse(deadline_date)
    else:
        deadline_date = None
    end_date = project_dict.get('end_date')
    if isinstance(end_date, (int, float)):
        end_date = datetime.datetime.fromtimestamp(end_date / 1000)
    elif isinstance(end_date, basestring):
        end_date = parser.parse(end_date)
    else:
        end_date = None
    if end_date:
        last_day_of_month = calendar.monthrange(
            end_date.year, end_date.month
        )[1]
        end_date = datetime.datetime(
            end_date.year, end_date.month, last_day_of_month
        )

    if project_dict.get('compliance_type'):
        compliance = Compliance.objects.create(
            compliance_type=project_dict.get('compliance_type'),
            end_date=end_date,
            deadline_date=deadline_date,
            project=project
        )
        compliance.save()
コード例 #9
0
ファイル: tasks.py プロジェクト: CalebMuhia/seed
def remove_buildings(project_slug, project_dict, user_pk):
    """adds buildings to a project. if a user has selected all buildings,
       then the the search parameters within project_dict are used to determine
       the total set of buildings.

       :param str project_slug: a project's slug used to get the project
       :param dict project_dict: contains search params, and browser state
           information
       :user_pk int or str: the user's pk or id
    """
    project = Project.objects.get(slug=project_slug)
    user = User.objects.get(pk=user_pk)
    project.last_modified_by = user
    project.save()

    selected_buildings = project_dict.get('selected_buildings', [])
    prog_key = project.removing_buildings_status_percentage_cache_key
    data = {
        'status': 'processing',
        'progress': 0,
        'progress_key': prog_key,
        'numerator': 0,
        'denominator': 0,
    }
    set_cache(prog_key, data['status'], data)
    i = 0
    denominator = 1
    if not project_dict.get('select_all_checkbox', False):
        for sfid in selected_buildings:
            i += 1
            denominator = len(selected_buildings)
            data = {
                'status': 'processing',
                'progress': (float(i) / max(len(selected_buildings), 1) * 100),
                'progress_key': prog_key,
                'numerator': i,
                'denominator': denominator
            }
            set_cache(prog_key, data['status'], data)
            ab = BuildingSnapshot.objects.get(pk=sfid)
            ProjectBuilding.objects.get(project=project,
                                        building_snapshot=ab).delete()
    else:
        query_buildings = get_search_query(user, project_dict)
        denominator = query_buildings.count() - len(selected_buildings)
        data = {
            'status': 'processing',
            'progress': 10,
            'progress_key': prog_key,
            'numerator': i,
            'denominator': denominator
        }
        set_cache(prog_key, data['status'], data)
        for b in query_buildings:
            ProjectBuilding.objects.get(project=project,
                                        building_snapshot=b).delete()
        data = {
            'status': 'processing',
            'progress': 50,
            'progress_key': prog_key,
            'numerator': denominator - len(selected_buildings),
            'denominator': denominator
        }
        set_cache(prog_key, data['status'], data)
        for building in selected_buildings:
            i += 1
            ab = BuildingSnapshot.objects.get(source_facility_id=building)
            ProjectBuilding.objects.create(project=project,
                                           building_snapshot=ab)
            data = {
                'status':
                'processing',
                'progress': (float(denominator - len(selected_buildings) + i) /
                             denominator * 100),
                'progress_key':
                prog_key,
                'numerator':
                denominator - len(selected_buildings) + i,
                'denominator':
                denominator
            }
            set_cache(prog_key, data['status'], data)

    result = {
        'status': 'complete',
        'progress': 100,
        'progress_key': prog_key,
        'numerator': i,
        'denominator': denominator
    }
    set_cache(prog_key, result['status'], result)
コード例 #10
0
def add_buildings(project_slug, project_dict, user_pk):
    """adds buildings to a project. if a user has selected all buildings,
       then the the search parameters within project_dict are used to determine
       the total set
       of buildings.
       also creates a Compliance inst. if sastifying params are present

       :param str project_slug: a project's slug used to get the project
       :param dict project_dict: contains search params, and browser state
       infomation
       :user_pk int or str: the user's pk or id

    """
    project = Project.objects.get(slug=project_slug)
    user = User.objects.get(pk=user_pk)
    project.last_modified_by = user
    project.save()

    selected_buildings = project_dict.get('selected_buildings', [])

    cache.set(
        project.adding_buildings_status_percentage_cache_key,
        {'percentage_done': 0, 'numerator': 0, 'denominator': 0}
    )
    i = 0
    denominator = 1
    if not project_dict.get('select_all_checkbox', False):
        for sfid in selected_buildings:
            i += 1
            denominator = len(selected_buildings)
            try:
                cache.set(
                    project.adding_buildings_status_percentage_cache_key,
                    {
                        'percentage_done': (
                            float(i) / len(selected_buildings) * 100
                        ),
                        'numerator': i, 'denominator': denominator
                    }
                )
            except ZeroDivisionError:
                pass
            ab = BuildingSnapshot.objects.get(pk=sfid)
            ProjectBuilding.objects.get_or_create(
                project=project, building_snapshot=ab
            )
    else:
        query_buildings = get_search_query(user, project_dict)
        denominator = query_buildings.count() - len(selected_buildings)
        cache.set(
            project.adding_buildings_status_percentage_cache_key,
            {'percentage_done': 10, 'numerator': i, 'denominator': denominator}
        )
        i = 0
        for b in query_buildings:
            # todo: only get back query_buildings pks as a list, and create
            # using the pk,
            #       not the python object
            i += 1
            ProjectBuilding.objects.get_or_create(
                project=project, building_snapshot=b
            )
            cache.set(
                project.adding_buildings_status_percentage_cache_key,
                {
                    'percentage_done': float(i) / denominator * 100,
                    'numerator': i, 'denominator': denominator
                }
            )
        for building in selected_buildings:
            i += 1
            project.building_snapshots.remove(
                BuildingSnapshot.objects.get(pk=building)
            )
            cache.set(
                project.adding_buildings_status_percentage_cache_key,
                {
                    'percentage_done': (
                        float(denominator - len(selected_buildings) + i) /
                        denominator * 100
                    ),
                    'numerator': denominator - len(selected_buildings) + i,
                    'denominator': denominator
                }
            )

    cache.set(
        project.adding_buildings_status_percentage_cache_key,
        {'percentage_done': 100, 'numerator': i, 'denominator': denominator}
    )

    deadline_date = project_dict.get('deadline_date')
    if isinstance(deadline_date, (int, float)):
        deadline_date = datetime.datetime.fromtimestamp(deadline_date / 1000)
    elif isinstance(deadline_date, basestring):
        deadline_date = parser.parse(deadline_date)
    else:
        deadline_date = None
    end_date = project_dict.get('end_date')
    if isinstance(end_date, (int, float)):
        end_date = datetime.datetime.fromtimestamp(end_date / 1000)
    elif isinstance(end_date, basestring):
        end_date = parser.parse(end_date)
    else:
        end_date = None
    if end_date:
        last_day_of_month = calendar.monthrange(
            end_date.year, end_date.month
        )[1]
        end_date = datetime.datetime(
            end_date.year, end_date.month, last_day_of_month
        )

    if project_dict.get('compliance_type'):
        compliance = Compliance.objects.create(
            compliance_type=project_dict.get('compliance_type'),
            end_date=end_date,
            deadline_date=deadline_date,
            project=project
        )
        compliance.save()
コード例 #11
0
ファイル: main.py プロジェクト: MagnusCheifetz/seed
def export_buildings(request):
    """
    Begins a building export process.

    Payload::

        {
          "export_name": "My Export",
          "export_type": "csv",
          "selected_building": [1234,], (optional list of building ids)
          "selected_fields": optional list of fields to export
          "select_all_checkbox": True // optional, defaults to False
        }

    Returns::

        {
            "success": True,
            "status": "success",
            "export_id": export_id; see export_buildings_download,
            "total_buildings": count of buildings,
        }
    """
    body = json.loads(request.body)

    export_name = body.get('export_name')
    export_type = body.get('export_type')

    building_ids = body.get('building_ids')

    selected_fields = body.get('selected_fields', [])

    selected_building_ids = body.get('selected_buildings', [])

    project_id = body.get('project_id')

    if not body.get('select_all_checkbox', False):
        selected_buildings = get_search_query(request.user, {})
        selected_buildings = selected_buildings.filter(
            pk__in=selected_building_ids
        )
    else:
        selected_buildings = get_search_query(request.user, body)
        selected_buildings = selected_buildings.exclude(
            pk__in=selected_building_ids
        )

    export_id = str(uuid.uuid4())

    # If we receive a project ID, we don't actually want to export buildings,
    # we want to export ProjectBuildings -- but the frontend doesn't know that,
    # so we change the fieldnames on the backend instead so the exporter can
    # resolve them correctly
    if project_id:
        export_model = 'seed.ProjectBuilding'

        # Grab the project buildings associated with the given project id and
        # buildings list
        selected_building_ids = [
            x[0] for x in selected_buildings.values_list('pk')
        ]
        selected_buildings = ProjectBuilding.objects.filter(
            project_id=project_id,
            building_snapshot__in=selected_building_ids)

        # Swap the requested fieldnames to reflect the new point of reference
        _selected_fields = []
        for field in selected_fields:
            components = field.split("__", 1)
            if (components[0] == 'project_building_snapshots'
                    and len(components) > 1):
                _selected_fields.append(components[1])
            else:
                _selected_fields.append("building_snapshot__%s" % field)
        selected_fields = _selected_fields
    else:
        export_model = 'seed.BuildingSnapshot'

    building_ids = [x[0] for x in selected_buildings.values_list('pk')]

    cache.set("export_buildings__%s" % export_id, 0)

    tasks.export_buildings.delay(export_id,
                                 export_name,
                                 export_type,
                                 building_ids,
                                 export_model,
                                 selected_fields)

    return {
        "success": True,
        "status": "success",
        "export_id": export_id,
        "total_buildings": selected_buildings.count(),
    }