Skip to content

Commit

Permalink
Merge pull request #3 from C3BI-pasteur-fr/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
DamCorreia authored Oct 9, 2017
2 parents 8bfd3a5 + 35e97dd commit 07cf4ae
Show file tree
Hide file tree
Showing 93 changed files with 12,334 additions and 1,345 deletions.
14 changes: 11 additions & 3 deletions NGPhylogeny_fr/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
DEBUG = True

ALLOWED_HOSTS = ["127.0.0.1",]
INTERNAL_IPS = ["127.0.0.1",]

CRISPY_TEMPLATE_PACK = "bootstrap4"

Expand All @@ -46,6 +47,7 @@
'galaxy',
'data',
'tools',
'surveys',
'workflows',
'workspace',
]
Expand All @@ -62,8 +64,8 @@
'debug_toolbar.middleware.DebugToolbarMiddleware'
]

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
SESSION_EXPIRE_AT_BROWSER_CLOSE = False

ROOT_URLCONF = 'NGPhylogeny_fr.urls'

Expand Down Expand Up @@ -158,4 +160,10 @@
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
},
},
}
}

CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
}
13 changes: 12 additions & 1 deletion NGPhylogeny_fr/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
from account import urls as account_urls
from data import urls as data_urls
from galaxy import urls as galaxy_urls
from surveys import urls as surveys_urls
from tools import urls as tool_urls
from workflows import urls as workflows_urls
from workspace import urls as workspace_urls

urlpatterns = [
url(r'^admin/', admin.site.urls, name='admin'),
url(r'^admin/', admin.site.urls, name='admin'), # Django ADMIN URLS
url(r'^about$', TemplateView.as_view(template_name="about.html"), name="about"),
url(r'^about/', include(surveys_urls)),
url(r'^documentation$', TemplateView.as_view(template_name="documentation.html"), name="documentation"),
url(r'^analysis$', TemplateView.as_view(template_name="phylogeny_analysis_choices.html"), name="analysis_list"),
url(r'^galaxy/', include(galaxy_urls)),
Expand All @@ -37,3 +40,11 @@
url(r'^workspace/', include(workspace_urls)),
url(r'^$', TemplateView.as_view(template_name="home.html"), name="home"),
]

from django.conf import settings

if settings.DEBUG:
import debug_toolbar
urlpatterns = [
url(r'^__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# NG-Phylogeny.fr
# NGPhylogeny.fr


A Django web application to make Phylogeny analysis.
2 changes: 1 addition & 1 deletion account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import login, logout

from galaxy.views import *
from galaxy.views import UpdateApiKey

urlpatterns = [
url(r'^login$', login, {'template_name': "account/login.html" }, name='login'),
Expand Down
37 changes: 34 additions & 3 deletions data/forms.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
# -*- coding: utf-8 -*-
from crispy_forms.bootstrap import FormActions
from crispy_forms.bootstrap import StrictButton
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout
from django import forms


class UploadForm(forms.Form):
file = forms.FileField(widget=forms.FileInput(attrs={'multiple': 'true'}))
input_file = forms.FileField(widget=forms.FileInput())

def __init__(self, *args, **kwargs):
super(UploadForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_method = 'POST'
self.helper.layout = Layout('input_file',
FormActions(
Submit('submit', 'Submit')))

class Media:

js = ("js/jquery.filer.min.js",)
css= {
'all':("css/jquery.filer.css",)
}


class PastedContentForm(forms.Form):
pasted_text = forms.CharField(widget=forms.Textarea)
textarea_id = "id_pasted_text"
helper = FormHelper()
helper.form_method = 'POST'
helper.layout = Layout('file',
FormActions(Submit('submit', 'Submit')))
helper.layout = Layout('pasted_text',
FormActions(
Submit('submit', 'Submit'),
StrictButton( "<span class='glyphicon glyphicon-question-sign'></span> Example",
css_class="btn btn-info",
onclick="populateExample('"+textarea_id+"', FASTA_AA);"),
StrictButton("<span class='glyphicon glyphicon-trash'></span>",
css_class="btn btn-default",
onclick="document.getElementById('"+textarea_id+"').value='';" ),
))
class Media:
js = ('js/form_examples.js',)
2 changes: 1 addition & 1 deletion data/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from views import *

urlpatterns = [
url(r'^upload/$', UploadView.as_view(), name='upload'),
url(r'^upload/$', ImportPastedContentView.as_view(), name='upload'),

url(r'^display/(?P<file_id>[\w-]+)$', display_file, name="display_file"),
url(r'^displaytree/(?P<file_id>[\w-]+)$', tree_visualization, name="display_tree"),
Expand Down
71 changes: 56 additions & 15 deletions data/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,83 @@
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.generic import FormView
from django.views.generic.edit import FormView

from forms import UploadForm
from forms import UploadForm, PastedContentForm
from galaxy.decorator import connection_galaxy
from workspace.views import get_or_create_history


class UploadMixin(object):

def upload_content(self, content, history_id=None):
"""
send content into galaxy history: return galaxy response
"""
if history_id:
self.history_id = history_id
else:
self.history_id = get_or_create_history(self.request)

return self.request.galaxy.tools.paste_content(content=content, file_name="pasted_data",
history_id=self.history_id)


def upload_file(self, file, history_id=None):
"""
upload file into galaxy history: return galaxy response
"""

tmpfile = tempfile.NamedTemporaryFile()
for chunk in file.chunks():
tmpfile.write(chunk)
tmpfile.flush()

if history_id:
self.history_id = history_id
else:
self.history_id = get_or_create_history(self.request)

return self.request.galaxy.tools.upload_file(path=tmpfile.name,file_name=file.name, history_id=self.history_id)



@method_decorator(connection_galaxy, name="dispatch")
class UploadView(FormView):
class UploadView(UploadMixin, FormView):
"""
Upload file into Galaxy Server
Upload file into Galaxy Server
"""

template_name = 'upload_form.html'
form_class = UploadForm
success_url = reverse_lazy("home")

def upload_file(self, form):
"""upload file into current galaxy history: return galaxy response
"""
def form_valid(self, form):

myfile = form.cleaned_data['file']
tmpfile = tempfile.NamedTemporaryFile()
for chunk in myfile.chunks():
tmpfile.write(chunk)
tmpfile.flush()
outputs = self.upload_file(myfile)
self.success_url = reverse_lazy("history_detail", kwargs={'history_id': self.history_id}, )

self.history_id = get_or_create_history(self.request)
return self.request.galaxy.tools.upload_file(tmpfile.name, self.history_id, file_name=myfile.name)
return super(UploadView, self).form_valid()


@method_decorator(connection_galaxy, name="dispatch")
class ImportPastedContentView(UploadMixin, FormView):
"""
Import user pasted content into Galaxy Server
"""

form_class = PastedContentForm
success_url = reverse_lazy("home")

def form_valid(self, form):

outputs = self.upload_file(form)
p_content = form.cleaned_data['pasted_text']
outputs = self.upload_content(p_content)
self.success_url = reverse_lazy("history_detail", kwargs={'history_id': self.history_id}, )

return super(UploadView, self).form_valid(form)
return super(ImportPastedContentView, self).form_valid()



@connection_galaxy
Expand Down
25 changes: 19 additions & 6 deletions galaxy/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import requests
from django.contrib import admin
from django.contrib import messages
from django.core.validators import ValidationError

from tools.models import Tool
from .models import *
from .models import Server, GalaxyUser


class GalaxyServerAdmin(admin.ModelAdmin):
Expand All @@ -11,7 +13,18 @@ class GalaxyServerAdmin(admin.ModelAdmin):
"""
list_display = ('name', 'url', 'current')
list_display_links = ('name', 'url')
actions = ['activate_configuration','import_new_tools' ]
actions = ['activate_configuration', 'import_new_tools']

def save_model(self, request, obj, form, change):
# get automatically Galaxy version if it not set
if not obj.version:
try:
r = requests.get(obj.url + '/api/version')
obj.version = r.json().get('version_major')
except Exception as e:
raise ValidationError(message="Please set the version of Galaxy, or make sure that URL is working")

super(GalaxyServerAdmin, self).save_model(request, obj, form, change)

def import_new_tools(self, request, queryset):
"""
Expand All @@ -32,14 +45,14 @@ def activate_configuration(self, request, queryset):
galaxy_server = queryset[0]
galaxy_server.current = True
galaxy_server.save()

self.message_user(request, "%s configuration is now activated and usable by the application" %(galaxy_server.name),
level=messages.SUCCESS )
# TODO reset history session
self.message_user(request,
"%s configuration is now activated and usable by the application" % (galaxy_server.name),
level=messages.SUCCESS)


class GalaxyUserAdmin(admin.ModelAdmin):
"""
"""
list_display = ('user', 'galaxy_server', 'anonymous')
list_filter = ('user', 'galaxy_server', 'anonymous')
Expand Down
15 changes: 11 additions & 4 deletions galaxy/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.shortcuts import redirect

from galaxy.models import Server, GalaxyUser

logger = logging.getLogger(__name__)


Expand All @@ -17,7 +18,12 @@ def wrapper(request, *args, **kwargs):
try:
if hasattr(request, 'galaxy_server'):
galaxy_server = request.galaxy_server

elif request.session.get('galaxy_server'):
galaxy_server = Server.objects.get(id=request.session.get('galaxy_server'))

else:
#by default use the current Galaxy server
galaxy_server = Server.objects.get(current=True)

except Server.DoesNotExist :
Expand All @@ -31,6 +37,7 @@ def wrapper(request, *args, **kwargs):
raise HttpResponseGone()

request.galaxy_server = galaxy_server
request.session['galaxy_server'] = galaxy_server.id

if request.user.is_authenticated():
"""Try to use related Galaxy user information"""
Expand All @@ -41,7 +48,7 @@ def wrapper(request, *args, **kwargs):

"""If the key api is not defined, prompts the user to define it"""
if gu.api_key:
request.galaxy = gu.get_galaxy_instance()
request.galaxy = gu.get_galaxy_instance
else:
return redirect('galaxy_account')

Expand All @@ -60,7 +67,7 @@ def wrapper(request, *args, **kwargs):
"""If user is not an authenticated, use the anonymous Galaxy user set"""
try:
gu = GalaxyUser.objects.get(anonymous=True, galaxy_server=galaxy_server)
request.galaxy = gu.get_galaxy_instance()
request.galaxy = gu.get_galaxy_instance

except GalaxyUser.DoesNotExist :
msg = "NGPhylogeny server is not properly configured, " \
Expand All @@ -69,8 +76,8 @@ def wrapper(request, *args, **kwargs):
logger.exception("Anonymous user not set")
raise Http404(msg)

except:
logger.exception("Galaxy account Error")
except Exception as e:
logger.exception("Galaxy anonymous account Error: "+e)
return HttpResponseGone()

return view_function(request, *args, **kwargs)
Expand Down
Loading

0 comments on commit 07cf4ae

Please sign in to comment.