Exemplo n.º 1
0
def show(object_id):
    form_class = form_manager.get_form_class("config_form")
    form = form_class()
    entity = Project.get(object_id)
    if entity is None:
        flash("That project doesn't exist", "danger")
        return redirect(url_for(".projects"))

    owner = False
    if current_user.is_authenticated and object_id in current_user.projects:
        owner = True
        for attr in entity.__dict__:
            if attr != "_id" and attr != "_type":
                form[attr].data = entity.__dict__[attr]
            # Workaround for image upload required, set the label
            # to the currently used image and disable it as a required field
            if attr == "image":
                form[
                    attr].label.text = "Stored image is: " + unique_name_decode(
                        entity.__dict__[attr])
                form[attr].flags = None
    else:
        # Translate area keys to values
        if hasattr(entity, "area"):
            entity.area = [
                area[1] for area in form.area.choices if area[0] in entity.area
            ]
    return render_template("projects/project.html",
                           object=entity,
                           owner=owner,
                           form=form)
Exemplo n.º 2
0
def project_edit(request, pk):
    form = ProjectForm(request.POST or None, instance=Project.get(pk))
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse("projects-list"))
    return direct_to_template(request, "project_edit.html", extra_context={
        "form": form,
        "nav": {"selected": "projects",},
    })
Exemplo n.º 3
0
 def test_project_save(self):
     project = Project(
         name="nasiodas",
         description="sdifosdfios",
         image="/Usdufsi/sdfs.png",
         tags="sdfo,sdfo,sdf",
     )
     project_id = project.save()
     new_project = Project.get(project_id)
     [
         self.assertEqual(project.__dict__[key], new_project.__dict__[key])
         for key in project.__dict__.keys()
     ]
Exemplo n.º 4
0
def project_edit(request, pk):
    form = ProjectForm(request.POST or None, instance=Project.get(pk))
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse("projects-list"))
    return direct_to_template(request,
                              "project_edit.html",
                              extra_context={
                                  "form": form,
                                  "nav": {
                                      "selected": "projects",
                                  },
                              })
Exemplo n.º 5
0
def project_url_add(request, pk):
    project = Project.get(pk)
    form = ProjectURLForm(request.POST or None)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.project = project
        obj.save()
        return HttpResponseRedirect(reverse("projects-list"))
    return direct_to_template(request, "project_url_add.html", extra_context={
        "form": form,
        "project": project,
        "nav": {"selected": "projects",},
    })
Exemplo n.º 6
0
def delete(object_id):
    entity = Project.get(object_id)
    if entity is None:
        flash("That entity dosen't exist", "danger")
        return redirect(url_for("projects.projects"))
    if object_id in current_user.projects:
        Project.remove(object_id)
        current_user.projects.remove(object_id)
        os.remove(
            os.path.join(config.get("PROJECTS", "upload_folder"),
                         entity.image))
        current_user.save()
        flash("Entity: " + entity.name + " has been deleted", "success")
    else:
        flash("Your trying to delete an entity you don't own", "danger")
    return redirect(url_for("projects.projects"))
Exemplo n.º 7
0
def project_url_add(request, pk):
    project = Project.get(pk)
    form = ProjectURLForm(request.POST or None)
    if form.is_valid():
        obj = form.save(commit=False)
        obj.project = project
        obj.save()
        return HttpResponseRedirect(reverse("projects-list"))
    return direct_to_template(request,
                              "project_url_add.html",
                              extra_context={
                                  "form": form,
                                  "project": project,
                                  "nav": {
                                      "selected": "projects",
                                  },
                              })
Exemplo n.º 8
0
def update(object_id):
    entity = Project.get(object_id)
    if entity is None:
        flash("That entity dosen't exist", "danger")
        return redirect(url_for("projects.projects"))

    if object_id not in current_user.projects:
        flash("Your trying to update an entity that's not yours", "danger")
        return redirect(url_for("projects.projects"))

    form_class = form_manager.get_form_class("config_form")
    form = form_class(CombinedMultiDict((request.files, request.form)))
    # Strip image upload validation on upload (Optional)
    form.image.validators = [
        validator for validator in form.image.validators
        if type(validator) is not FileRequired
    ]

    if form.validate_on_submit():
        # Only save the image if a new was submitted, else keep the old name
        f = form.image.data
        if f and hasattr(f, "filename") and f.filename != "":
            filename = secure_filename(unique_name_encoding(f.filename))
            f.save(
                os.path.join(config.get("PROJECTS", "upload_folder"),
                             filename))
            # Remove old
            os.remove(
                os.path.join(config.get("PROJECTS", "upload_folder"),
                             entity.image))
        else:
            filename = entity.image

        # Update every attribute except _id, _type, and image
        disabled_updates = ["_id", "_type", "image"]
        for attr in entity.__dict__:
            if attr not in disabled_updates:
                entity.__dict__[attr] = form[attr].data
        entity.__dict__["image"] = filename
        entity_id = entity.save()

        url = url_for("projects.show", object_id=entity_id, _external=True)
        flash("Update Success, your data can be found at: " + url, "success")
        return redirect(url)
    form.image.flags = None
    return render_template("projects/project.html", object=entity, form=form)
Exemplo n.º 9
0
Arquivo: views.py Projeto: kpx13/solar
def project(request, slug):
    c = get_common_context(request)
    proj = Project.get(slug, c['lang'])
    c['proj'] = proj
    c['its_mine'] = proj['participant'].user == request.user
    if request.method == 'POST':
        if request.POST.get('action') == 'review':
            Project.objects.get(slug=slug).add_review(Expert.objects.get(user=request.user), 
                            request.POST.get('content'))
            return HttpResponseRedirect('/project/%s/' % slug)
        elif request.POST.get('action') == 'comment':
            Project.objects.get(slug=slug).add_comment(request.user, 
                            request.POST.get('content'))
            return HttpResponseRedirect('/project/%s/' % slug)
        elif request.POST.get('action') == 'like':
            Project.objects.get(slug=slug).add_like(request.user)
            return HttpResponseRedirect('/project/%s/' % slug)
    return render_to_response('project.html', c, context_instance=RequestContext(request))
Exemplo n.º 10
0
    def test_project_update(self):
        # Pre cleanup
        self.user.projects.clear()
        self.user.save()
        # Save a new project
        project = Project(
            name="nasiodas",
            description="sdifosdfios",
            image=os.path.join("Usdufsi", "sdfs.png"),
            tags="sdfo,sdfo,sdf",
        )
        project_id = project.save()
        new_project = Project.get(project_id)
        [
            self.assertEqual(project.__dict__[key], new_project.__dict__[key])
            for key in project.__dict__.keys()
        ]

        # Authenticate
        with self.client as client:
            self.login(client)
            # Update project without being the correct owner
            new_data = {
                "name": "My Awesome project",
                "description": "nice_description",
                "tags": "Robots,Evolution,Individual Learning",
                "image": os.path.join("sdfsfs", "sdfsf.png"),
                "csrf_token": self.csrf_token,
            }
            rv = client.post(
                "/update/" + project_id, data=new_data, follow_redirects=True
            )
            test_response = "Your trying to update an entity that"
            self.assertIn(bytes(test_response, encoding="utf8"), rv.data)
            self.user.projects.append(project_id)
            self.user.save()
            rv = client.post(
                "/update/" + project_id, data=new_data, follow_redirects=True
            )
            test_response = "Update Success, your data can be found at: "
            self.assertIn(bytes(test_response, encoding="utf8"), rv.data)