def create_or_edit_project(request, project_id):
        form = ProjectCreationForm(request.POST)
        if project_id is not None:
            project = Project.objects.get(id=project_id)
        else:
            project = Project.objects.create(
                project_creator=get_request_contributor(request),
                project_name=form.data.get('project_name'),
                project_date_created=timezone.now(),
                is_created=False
            )

        if not is_co_owner_or_staff(request.user, project):
            raise PermissionDenied()

        is_created_original = project.is_created
        read_form_field_boolean(project, form, 'is_created')

        read_form_field_string(project, form, 'project_description')
        read_form_field_string(project, form, 'project_description_solution')
        read_form_field_string(project, form, 'project_description_actions')
        read_form_field_string(project, form, 'project_short_description')
        read_form_field_string(project, form, 'project_location')
        read_form_field_string(project, form, 'project_country')
        read_form_field_string(project, form, 'project_state')
        read_form_field_string(project, form, 'project_city')
        read_form_field_string(project, form, 'project_name')
        read_form_field_string(project, form, 'project_url')

        read_form_fields_point(project, form, 'project_location_coords', 'project_latitude', 'project_longitude')

        read_form_field_tags(project, form, 'project_issue_area')
        read_form_field_tags(project, form, 'project_stage')
        read_form_field_tags(project, form, 'project_technologies')
        read_form_field_tags(project, form, 'project_organization')
        read_form_field_tags(project, form, 'project_organization_type')

        if not request.user.is_staff:
            project.project_date_modified = timezone.now()

        project.save()

        merge_json_changes(ProjectLink, project, form, 'project_links')
        merge_json_changes(ProjectFile, project, form, 'project_files')
        merge_json_changes(ProjectPosition, project, form, 'project_positions')

        merge_single_file(project, form, FileCategory.THUMBNAIL, 'project_thumbnail_location')

        if is_created_original != project.is_created:
            print('notifying project creation')
            send_project_creation_notification(project)

        return project
示例#2
0
    def edit_project(request, project_id):
        project = Project.objects.get(id=project_id)

        if not is_co_owner_or_staff(request.user, project):
            raise PermissionDenied()

        form = ProjectCreationForm(request.POST)
        is_created_original = project.is_created
        read_form_field_boolean(project, form, 'is_created')

        read_form_field_string(project, form, 'project_description')
        read_form_field_string(project, form, 'project_short_description')
        read_form_field_string(project, form, 'project_location')
        read_form_field_string(project, form, 'project_name')
        read_form_field_string(project, form, 'project_url')

        read_form_field_tags(project, form, 'project_issue_area')
        read_form_field_tags(project, form, 'project_stage')
        read_form_field_tags(project, form, 'project_technologies')
        read_form_field_tags(project, form, 'project_organization')

        if not request.user.is_staff:
            project.project_date_modified = timezone.now()

        project.save()

        merge_json_changes(ProjectLink, project, form, 'project_links')
        merge_json_changes(ProjectFile, project, form, 'project_files')
        merge_json_changes(ProjectPosition, project, form, 'project_positions')

        merge_single_file(project, form, FileCategory.THUMBNAIL,
                          'project_thumbnail_location')

        if is_created_original != project.is_created:
            print('notifying project creation')
            send_project_creation_notification(project)

        return project
示例#3
0
    def create_project(request):
        form = ProjectCreationForm(request.POST)
        # TODO: Form validation
        project = Project.objects.create(
            project_creator=get_request_contributor(request),
            project_description=form.data.get('project_description'),
            project_short_description=form.data.get(
                'project_short_description'),
            project_location=form.data.get('project_location'),
            project_name=form.data.get('project_name'),
            project_url=form.data.get('project_url'),
            project_date_created=timezone.now())
        project = Project.objects.get(id=project.id)

        # Tag fields operate like ManyToMany fields, and so cannot
        # be added until after the object is created.
        issue_areas = form.data.get('project_issue_area')
        if issue_areas and len(issue_areas) != 0:
            project.project_issue_area.add(issue_areas)

        project_stage = form.data.get('project_stage')
        if project_stage and len(project_stage) != 0:
            project.project_stage.add(project_stage)

        project_technologies = form.data.get('project_technologies')
        if len(project_technologies) > 0:
            for tech in project_technologies.split(','):
                project.project_technologies.add(tech)

        project_organization = form.data.get('project_organization')
        if len(project_organization) > 0:
            for org in project_organization.split(','):
                project.project_organization.add(org)

        project.save()

        positions_json_text = form.data.get('project_positions')
        if len(positions_json_text) > 0:
            positions_json = json.loads(positions_json_text)
            for position_json in positions_json:
                position = ProjectPosition.create_from_json(
                    project, position_json)

        links_json_text = form.data.get('project_links')
        if len(links_json_text) > 0:
            links_json = json.loads(links_json_text)
            for link_json in links_json:
                link = ProjectLink.from_json(project, link_json)
                link.save()

        files_json_text = form.data.get('project_files')
        if len(files_json_text) > 0:
            files_json = json.loads(files_json_text)
            for file_json in files_json:
                file = ProjectFile.from_json(project, FileCategory.ETC,
                                             file_json)
                file.save()

        project_thumbnail_location = form.data.get(
            'project_thumbnail_location')
        if len(project_thumbnail_location) > 0:
            thumbnail_json = json.loads(project_thumbnail_location)
            thumbnail = ProjectFile.from_json(project, FileCategory.THUMBNAIL,
                                              thumbnail_json)
            thumbnail.save()

        # Notify the admins that a new project has been created
        send_project_creation_notification(project)