Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Proof of concept Fork functionality #353

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions applications/graphs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
url(r'^graphs/(?P<graph_id>[^/]+)$', views.graph_page, name='graph'),
url(r'^graphs/(?P<email>[^/]+)/(?P<graph_name>[^/]+)$', views.graph_page_by_name, name='graph_by_name'),
url(r'^upload$', views.upload_graph_page, name='upload_graph'),
url(r'^fork_graph$', views.fork_graph, name='fork_graph'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cna you fix the indentation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely, I will fix it.


# AJAX APIs Endpoints

Expand Down
16 changes: 16 additions & 0 deletions applications/graphs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ def upload_graph_page(request):
else:
return render(request, 'upload_graph/index.html', context)

@csrf_exempt
def fork_graph(request):
response=''
if request.method == 'POST':
try:
graph = _add_graph(request, graph={
'name': request.POST.get('name', None),
'owner_email': request.POST.get('owner_email', None),
'is_public': request.POST.get('is_public', None),
'graph_json': json.loads(request.POST.get('graph_json', None)),
'style_json': json.loads(request.POST.get('style_json', None))
})
response = 'Success'
except Exception as e:
response = str(e)
return HttpResponse(json.dumps({'result':response}), content_type="application/json")

def graphs_page(request):
"""
Expand Down
35 changes: 35 additions & 0 deletions static/js/graphs_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,13 @@ var graphPage = {

utils.initializeTabs();

$("#ForkGraph").click(function (e) {
e.preventDefault();
if (!$(this).hasClass('disabled')){
graphPage.fork($(this).data()); // Passing data about parent graph to fork() as argument
}
});

$('#saveOnExitLayoutBtn').click(function () {
graphPage.cyGraph.contextMenus('get').destroy(); // Destroys the cytocscape context menu extension instance.

Expand Down Expand Up @@ -516,6 +523,34 @@ var graphPage = {

graphPage.defaultLayoutWidget.init();
},
fork: function (data) {
//Read the meta_data object and add 'parent_id' & 'parent_email' to the data field.
graph_meta_data = cytoscapeGraph.getNetworkAndViewJSON(graphPage.cyGraph);
graph_meta_data.data['parent_id'] = data.graph_id;
graph_meta_data.data['parent_email'] = data.owner_email;
var graphData = {
'name':data.graph_name,
'is_public':0,
'owner_email':data.uid,
'graph_json':JSON.stringify(graph_meta_data, null, 4),
'style_json':JSON.stringify(cytoscapeGraph.getStyleJSON(graphPage.cyGraph), null, 4)
}
$.ajax({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making an ajax call here. Can you add an API function at the top of the file (checkout Line 5)- just like - apis.graphs.get etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I will make the changes you have requested and send another pull request.

"type": "POST",
"dataType": "json",
"url": "/fork_graph",
"data": graphData,
"success": function (data) {
if(data.result=='Success'){
$("#ForkGraph span").text('Forked Successfully');
$("#ForkGraph").addClass('disabled');
}
else{
alert('Could not fork the Graph due to the following error : ' + data.result);
}
},
});
},
export: function (format) {
cytoscapeGraph.export(graphPage.cyGraph, format, $('#GraphName').val());
},
Expand Down
30 changes: 27 additions & 3 deletions templates/graph/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h4 class="modal-title" id="myModalLabel">Save Layout</h4>
<!--- Modals end here--->
<div class="container-fluid zero-margin zero-padding">
<div class="row zero-margin zero-padding padding-top-2 padding-bottom-2">
<div class="col-md-10">
<div class="col-md-9">
<p class="lead">
{{ title }}
</p>
Expand All @@ -169,16 +169,40 @@ <h4 class="zero-margin d-inline-block">
{% endfor %}
</div>
{% endif %}
{% if 'data' in graph.graph_json and 'parent_email' in graph.graph_json.data%}
<p class="small">
<small> forked from {{ graph.graph_json.data.parent_email }}/ <a class="text-center"
href="{% url 'graphs' %}{{ graph.graph_json.data.parent_id }} "> {{ graph.name }} </a> </small>
</p>
{% endif %}
</div>

<div class="col-md-2">
<div class="col-md-3">
{% if isForked %}
<div class="btn-group" style=": 1em !important; z-index: 20000">
<button type="button" class="btn btn-sm btn-default disabled"
aria-haspopup="true" aria-expanded="false">
<i class="fa fa-code-fork" aria-hidden="true"></i> Forked Successfully
</button>

</div>
{% elif uid != graph.owner_email %}
<div class="btn-group pull-right" style=": 1em !important; z-index: 20000">
<button type="button" id='ForkGraph' class="btn btn-sm btn-default"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ID should be ForkGraphBtn

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I will make the changes.

aria-haspopup="true" aria-expanded="false" data-uid={{ uid }} data-graph_name="{{ title }}"
data-graph_id={{ graph.id }} data-owner_email={{ graph.owner_email }}>
<i class="fa fa-code-fork" aria-hidden="true"></i> <span>Fork</span>
</button>

</div>
{% endif %}
{% if uid and uid == graph.owner_email %}
<a class="btn btn-default pull-right btn-sm" href="#" data-toggle="modal"
data-target="#shareGraphModal">
<i class="fa fa-share-alt fa-lg"></i> Share
</a>
{% endif %}
<div class="btn-group pull-right" style="margin-right: 1em !important; z-index: 20000">
<div class="btn-group pull-right" style="margin-right: 1em !important; z-index: 1001">
<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
<i class="fa fa-download fa-lg"></i> Export <span class="caret"></span>
Expand Down