# Path: example/server/views.py
# class ExampleJobLogView(LoginRequiredMixin, TemplateView):
#     template_name = 'example_job_log.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(ExampleJobLogView, self).get_context_data(**kwargs)
#         context['job_pk'] = kwargs['job_pk']
#         return context
# 
# class ExampleJobStatusView(LoginRequiredMixin, TemplateView):
#     template_name = 'example_job_status.html'
# 
#     def post(self, request, *args, **kwargs):
#         (interpreter, _) = Interpreter.objects.get_or_create(
#             name='Python',
#             path=settings.EXAMPLE_PYTHON_PATH,
#             arguments=settings.EXAMPLE_PYTHON_ARGUMENTS,
#         )
# 
#         (server, _) = Server.objects.get_or_create(
#             title='Example Server',
#             hostname=settings.EXAMPLE_SERVER_HOSTNAME,
#             port=settings.EXAMPLE_SERVER_PORT,
#         )
# 
#         logger.debug("Running job in {} using {}".format(server, interpreter))
# 
#         num_jobs = len(Job.objects.all())
# 
#         program = textwrap.dedent('''\
#         from __future__ import print_function
#         import time
#         for i in range(10):
#             with open('django_remote_submission_example_out_{}.txt'.format(i), 'wt') as f:
#                 print('Line {}'.format(i), file=f)
#                 print('Line {}'.format(i))
#             time.sleep(1)
#         ''')
# 
#         (job, _) = Job.objects.get_or_create(
#             title='Example Job #{}'.format(num_jobs),
#             program=program,
#             remote_directory=settings.EXAMPLE_REMOTE_DIRECTORY,
#             remote_filename=settings.EXAMPLE_REMOTE_FILENAME,
#             owner=request.user,
#             server=server,
#             interpreter=interpreter,
#         )
# 
#         submit_job_to_server.delay(
#             job_pk=job.pk,
#             password=settings.EXAMPLE_REMOTE_PASSWORD,
#             username=settings.EXAMPLE_REMOTE_USER,
#         )
# 
#         return HttpResponse('success')
# 
# class IndexView(LoginRequiredMixin, TemplateView):
#     template_name = "index.html"
# 
#     def get_context_data(self, **kwargs):
#         context = super(IndexView, self).get_context_data(**kwargs)
#         context['job_list'] = Job.objects.all()
#         context['server_list'] = Server.objects.all()
#         context['Log_list'] = Log.objects.all()
#         return context
# 
# class JobDetail(LoginRequiredMixin, DetailView):
#     model = Job
# 
# class JobList(LoginRequiredMixin, ListView):
#     model = Job
# 
# class ServerDetail(LoginRequiredMixin, DetailView):
#     model = Server
# 
# class ServerList(LoginRequiredMixin, ListView):
#     model = Server


from django.conf.urls import include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
from django.contrib.auth import views as auth_views
from .views import (ExampleJobLogView, ExampleJobStatusView, IndexView,
                    JobDetail, JobList, ServerDetail, ServerList)
"""example URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', IndexView.as_view(), name='index'),
    # Login
    url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
    url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
    # Live job status

# Path: example/server/views.py
# class ExampleJobLogView(LoginRequiredMixin, TemplateView):
#     template_name = 'example_job_log.html'
# 
#     def get_context_data(self, **kwargs):
#         context = super(ExampleJobLogView, self).get_context_data(**kwargs)
#         context['job_pk'] = kwargs['job_pk']
#         return context
# 
# class ExampleJobStatusView(LoginRequiredMixin, TemplateView):
#     template_name = 'example_job_status.html'
# 
#     def post(self, request, *args, **kwargs):
#         (interpreter, _) = Interpreter.objects.get_or_create(
#             name='Python',
#             path=settings.EXAMPLE_PYTHON_PATH,
#             arguments=settings.EXAMPLE_PYTHON_ARGUMENTS,
#         )
# 
#         (server, _) = Server.objects.get_or_create(
#             title='Example Server',
#             hostname=settings.EXAMPLE_SERVER_HOSTNAME,
#             port=settings.EXAMPLE_SERVER_PORT,
#         )
# 
#         logger.debug("Running job in {} using {}".format(server, interpreter))
# 
#         num_jobs = len(Job.objects.all())
# 
#         program = textwrap.dedent('''\
#         from __future__ import print_function
#         import time
#         for i in range(10):
#             with open('django_remote_submission_example_out_{}.txt'.format(i), 'wt') as f:
#                 print('Line {}'.format(i), file=f)
#                 print('Line {}'.format(i))
#             time.sleep(1)
#         ''')
# 
#         (job, _) = Job.objects.get_or_create(
#             title='Example Job #{}'.format(num_jobs),
#             program=program,
#             remote_directory=settings.EXAMPLE_REMOTE_DIRECTORY,
#             remote_filename=settings.EXAMPLE_REMOTE_FILENAME,
#             owner=request.user,
#             server=server,
#             interpreter=interpreter,
#         )
# 
#         submit_job_to_server.delay(
#             job_pk=job.pk,
#             password=settings.EXAMPLE_REMOTE_PASSWORD,
#             username=settings.EXAMPLE_REMOTE_USER,
#         )
# 
#         return HttpResponse('success')
# 
# class IndexView(LoginRequiredMixin, TemplateView):
#     template_name = "index.html"
# 
#     def get_context_data(self, **kwargs):
#         context = super(IndexView, self).get_context_data(**kwargs)
#         context['job_list'] = Job.objects.all()
#         context['server_list'] = Server.objects.all()
#         context['Log_list'] = Log.objects.all()
#         return context
# 
# class JobDetail(LoginRequiredMixin, DetailView):
#     model = Job
# 
# class JobList(LoginRequiredMixin, ListView):
#     model = Job
# 
# class ServerDetail(LoginRequiredMixin, DetailView):
#     model = Server
# 
# class ServerList(LoginRequiredMixin, ListView):
#     model = Server


# Path: example/server/urls.py
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin
from django.contrib.auth import views as auth_views
from .views import (ExampleJobLogView, ExampleJobStatusView, IndexView,
                    JobDetail, JobList, ServerDetail, ServerList)

"""example URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', IndexView.as_view(), name='index'),
    # Login
    url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
    url(r'^logout/$', auth_views.logout, {'template_name': 'logged_out.html'}, name='logout'),
    # Live job status

    url(r'^example/$', ExampleJobStatusView.as_view(), name='example'),