Skip to content

Commit

Permalink
Copy Project added
Browse files Browse the repository at this point in the history
  • Loading branch information
Volodymyr Sergeyev committed Feb 1, 2013
1 parent 0850cd0 commit 7143766
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 5 deletions.
48 changes: 47 additions & 1 deletion ide/controllers/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def project_create():
finally:
fo.close()

return json.dumps(item.json())
return json.dumps(item.json(with_tree=True))


@login_required
Expand Down Expand Up @@ -198,6 +198,52 @@ def project_rename():
return "{}"


@login_required
@post('/project_copy/')
def project_copy():
'''
Copy project dir and record in DB.
'''

user = User()
item = models.Project.find_one({"id": request.POST.get("id")})

if user.id != item.user_id:
return json.dumps({"msg": "User ID not match with project ID!"})

new_title = request.POST.get("new_title")

if not new_title:
return json.dumps({"msg": "Specify new name for project!"})

# New item record in DB
new_item = models.Project()
new_item.user_id = user.id
new_item.title = new_title

try:
path = "%s%i/" % (settings.PROJECTS_ROOT, item.user_id)
shutil.copytree(item.abs_path(), path + new_title)
except:
return json.dumps({"msg": "Error copying project!"})

new_item.save()

try:
fo = open(new_item.abs_path() + "/.liveideproject", "r+")
try:
project_settings = json.loads(fo.read() or "{}")
except:
project_settings = {}
project_settings["title"] = new_item.title
fo.truncate(0)
fo.write(json.dumps(project_settings))
finally:
fo.close()

return json.dumps(new_item.json(with_tree=True))


@login_required
@post('/project_settings/')
def project_settings():
Expand Down
27 changes: 27 additions & 0 deletions ide/static/js/liveide-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
return;
}

v.is_open = true;
that.projects[v.id] = v;
that.helpers.render_project(v);

Expand All @@ -35,6 +36,8 @@
that.active.folder = null;
that.dom.project.active.html(that.active.project.title);
that.add_editor(null, 'main.py', 'print "Hello World!"', true);
that.dom.project.open.parent().find("ul").append("<li><a href='#' class='liveide-project-to-open' data-id='" + v.id
+ "'>" + v.title + "</a></li>");

that.flash("Project created");
});
Expand Down Expand Up @@ -92,6 +95,30 @@
});
},

/* Copy */
copy: function (project) {
bootbox.prompt("Copy " + project.title + " to", function(title) {
if (!title) return;

$.post("/project_copy/", {"id": project.id, "new_title": title}, function (data) {
var v = $.parseJSON(data);

if (v.msg) {
that.flash(v.msg, true);
return;
}

v.is_open = true;
that.projects[v.id] = v;
that.helpers.render_project(v);
that.dom.project.open.parent().find("ul").append("<li><a href='#' class='liveide-project-to-open' data-id='" + v.id
+ "'>" + v.title + "</a></li>");

that.flash("Project copied");
});
});
},

/* Delete project */
remove: function (project) {
if (!project) return;
Expand Down
8 changes: 8 additions & 0 deletions ide/static/js/liveide.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
active: $(".liveide-active-project"),
create: $(".liveide-project-new"),
rename: $(".liveide-project-rename"),
copy: $(".liveide-project-copy"),
open: $(".liveide-project-open"),
close: $(".liveide-project-close"),
remove: $(".liveide-project-remove"),
Expand Down Expand Up @@ -511,6 +512,13 @@
that.project.rename(that.active.project);
});

/* Project -> Copy project... */
this.dom.project.copy.on("click", function (e) {
e.preventDefault();
if (that.active.project)
that.project.copy(that.active.project);
});

/* Project -> Close */
this.dom.project.close.on("click", function (e) {
var project = that.active.project;
Expand Down
10 changes: 6 additions & 4 deletions ide/views/layout.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<ul id="liveide-tree-menu" class="dropdown-menu context-menu liveide-dropdownmenu" role="menu">
<li><a tabindex="-1" href="#" class="liveide-tree-refresh">Refresh</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#" class="liveide-project-upload">Upload project</a></li>
<li><a tabindex="-1" href="#" class="liveide-project-upload">Upload Project</a></li>
</ul>

<ul id="liveide-file-menu" class="dropdown-menu context-menu liveide-dropdownmenu" role="menu">
Expand All @@ -49,6 +49,7 @@
<li><a tabindex="-1" href="#" class="liveide-file-new">New File</a></li>
<li><a tabindex="-1" href="#" class="liveide-file-upload">Upload File</a></li>
<li><a tabindex="-1" href="#" class="liveide-project-rename">Rename...</a></li>
<li><a tabindex="-1" href="#" class="liveide-project-copy">Copy Project...</a></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#" class="liveide-folder-new">New Folder</a></li>
<li class="divider"></li>
Expand All @@ -58,7 +59,7 @@
<li><a tabindex="-1" href="#" class="liveide-project-remove">Delete Project</a></li>
-->
<li class="divider"></li>
<li><a tabindex="-1" href="#" class="liveide-project-download">Download project</a></li>
<li><a tabindex="-1" href="#" class="liveide-project-download">Download Project</a></li>
</ul>

<ul id="liveide-folder-menu" class="dropdown-menu context-menu liveide-dropdownmenu" role="menu">
Expand Down Expand Up @@ -167,15 +168,16 @@
</ul>
</li>
<li><a href="#" class="liveide-project-rename">Rename...</a></li>
<li><a href="#" class="liveide-project-copy">Copy Project...</a></li>
<li class="divider"></li>
<li><a href="#" class="liveide-project-close">Close Project</a></li>
<!--
<li class="divider"></li>
<li><a href="#" class="liveide-project-remove">Delete Project</a></li>
-->
<li class="divider"></li>
<li><a href="#" class="liveide-project-download">Download project</a></li>
<li><a href="#" class="liveide-project-upload">Upload project</a></li>
<li><a href="#" class="liveide-project-download">Download Project</a></li>
<li><a href="#" class="liveide-project-upload">Upload Project</a></li>
<li class="divider"></li>
<li><a href="#" class="liveide-project-settings">Settings</a></li>
</ul>
Expand Down

0 comments on commit 7143766

Please sign in to comment.