Skip to content

Commit

Permalink
version based on python 3.6 and django 2.0 passing the unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
backmari committed Apr 26, 2024
1 parent 2e8fbcd commit 1c7ac23
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 59 deletions.
30 changes: 7 additions & 23 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
FROM centos:centos7
FROM condaforge/miniforge3:latest

RUN yum update -y
RUN yum install -y \
which \
epel-release
RUN yum install -y \
gcc \
make \
httpd \
mod_wsgi \
python-pip \
postgresql \
postgresql-devel \
python-devel \
vim \
wget

COPY docker-entrypoint.sh /usr/bin/
COPY environment.yml .
RUN mamba env create

WORKDIR /var/www/livedata

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY requirements_dev.txt .
RUN pip install -r requirements_dev.txt
COPY docker-entrypoint.sh /usr/bin/

COPY live_data_server app
RUN mkdir ./static

RUN chmod +x /usr/bin/docker-entrypoint.sh
ENTRYPOINT ["conda", "run", "--no-capture-output", "-n", "livedata", "/usr/bin/docker-entrypoint.sh"]
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
prefix := /var/www/livedata
app_dir := live_data_server
DJANGO_COMPATIBLE:=$(shell python -c "import django;t=0 if django.VERSION[1]<9 else 1; print t")
DJANGO_VERSION:=$(shell python -c "import django;print django.__version__")
DJANGO_COMPATIBLE:=$(shell python -c "import django;t=0 if django.VERSION[1]<9 else 1; print(t)")
DJANGO_VERSION:=$(shell python -c "import django;print(django.__version__)")

# command to run docker compose. change this to be what you have installed
# this can be overriden on the command line
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ make local/docker/up
This command will copy `config/docker-compose.envlocal.yml` into `docker-compose.yml` before composing
all the services.

Note that you may need to authenticate using

```bash
docker login code.ornl.gov:4567 -u <user> --password-stdin
```

to be able to pull docker images from the local storage. For information on how to set up the
personal access token see:
[GitLab two-factor authentication](https://code.ornl.gov/help/user/profile/account/two_factor_authentication#troubleshooting).

Type `make help` to learn about other macros available as make targets.
For instance, `make docker/pruneall` will stop all containers, then remove
all containers, images, networks, and volumes.
Expand Down
4 changes: 2 additions & 2 deletions config/docker-compose.envlocal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ services:
condition: service_healthy

livedata:
expose:
- "8000"
build:
context: .
dockerfile: Dockerfile
Expand All @@ -40,6 +38,8 @@ services:
retries: 5
start_period: 20s
timeout: 10s
ports:
- "8000:8000"
depends_on:
db:
condition: service_healthy
Expand Down
18 changes: 11 additions & 7 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ name: livedata
channels:
- conda-forge
dependencies:
- python=2.7
- python=3.6
- pip
- postgresql=9.5.3
- sphinx
- sphinx_rtd_theme
- pip:
- Django==1.9.12
- django-cors-headers==1.3.1
- psycopg2==2.6.2
- gunicorn==19.10.0
- pytest==4.6.11
- django=2.0
- django-cors-headers
- psycopg2
- gunicorn
# - pip:
# - Django==1.9.12
# - django-cors-headers==1.3.1
# - psycopg2==2.6.2
# - gunicorn==19.10.0
# - pytest==4.6.11
3 changes: 1 addition & 2 deletions live_data_server/live_data_server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@
'corsheaders',
]

MIDDLEWARE_CLASSES = [
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Expand Down
16 changes: 9 additions & 7 deletions live_data_server/live_data_server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
2. Add a URL to urlpatterns: path(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')
2. Add a URL to urlpatterns: path(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'))
2. Add a URL to urlpatterns: path(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.urls import re_path, include
from django.contrib import admin
from django.views.generic.base import RedirectView

app_name = 'live_data_server'

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', RedirectView.as_view(url='/plots/')),
url(r'^plots/', include('plots.urls', namespace="plots")),
re_path(r'^admin/', admin.site.urls),
re_path(r'^$', RedirectView.as_view(url='/plots/')),
re_path(r'^plots/', include('plots.urls', namespace="plots")),
]
6 changes: 3 additions & 3 deletions live_data_server/plots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DataRun(models.Model):
# Optional free-form run identifier
run_id = models.TextField()

instrument = models.ForeignKey(Instrument)
instrument = models.ForeignKey(Instrument, on_delete=models.deletion.CASCADE)
created_on = models.DateTimeField('Timestamp', auto_now_add=True)

def __unicode__(self):
Expand All @@ -42,7 +42,7 @@ class PlotData(models.Model):
Table of plot data. This data can either be json or html
"""
## DataRun this run status belongs to
data_run = models.ForeignKey(DataRun)
data_run = models.ForeignKey(DataRun, on_delete=models.deletion.CASCADE)
## Data type:
## type = 100 for live data, 0 static reduced.
## type += 1 for HTML, 0 for JSON
Expand Down Expand Up @@ -80,7 +80,7 @@ def get_data_type_from_data(cls, data):
Inspect the data to guess what type it is.
@param data: block of text to store
"""
if data.startswith('<div'):
if data.startswith(b'<div'):
return DATA_TYPES['html']
return DATA_TYPES['json']

Expand Down
16 changes: 9 additions & 7 deletions live_data_server/plots/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
"""
Define url structure
"""
from django.conf.urls import url
from django.urls import re_path

from . import views

app_name = 'plots'

urlpatterns = [
url(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/$', views.live_plot, name='live_plot'),
url(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/update/json/$', views.update_as_json, name='update_as_json'),
url(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/update/html/$', views.update_as_html, name='update_as_html'),
url(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/upload_plot_data/$', views.upload_plot_data, name='upload_plot_data'),
url(r'^(?P<user>[\w]+)/upload_user_data/$', views.upload_user_data, name='upload_user_data'),
url(r'^(?P<instrument>[\w]+)/list/$', views.get_data_list, name='get_data_list'),
re_path(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/$', views.live_plot, name='live_plot'),
re_path(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/update/json/$', views.update_as_json, name='update_as_json'),
re_path(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/update/html/$', views.update_as_html, name='update_as_html'),
re_path(r'^(?P<instrument>[\w]+)/(?P<run_id>\d+)/upload_plot_data/$', views.upload_plot_data, name='upload_plot_data'),
re_path(r'^(?P<user>[\w]+)/upload_user_data/$', views.upload_user_data, name='upload_user_data'),
re_path(r'^(?P<instrument>[\w]+)/list/$', views.get_data_list, name='get_data_list'),
]
12 changes: 6 additions & 6 deletions live_data_server/plots/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseNotFound, JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.core.exceptions import PermissionDenied
from django.views.decorators.cache import cache_page
from django.contrib.auth import login, authenticate
Expand All @@ -22,7 +22,7 @@ def _check_credentials(request):
Internal utility method to check whether a user has access to a view
"""
# If we don't allow guests but the user is authenticated, return the function
if request.user.is_authenticated():
if request.user.is_authenticated:
return True


Expand All @@ -34,13 +34,13 @@ def request_processor(request, *args, **kws):
"""
Authentication process
"""
if request.user.is_authenticated():
if request.user.is_authenticated:
return fn(request, *args, **kws)
if request.method == 'POST':
username = request.POST["username"]
password = request.POST["password"]
request_user = authenticate(username=username, password=password)
if request_user is not None and not request_user.is_anonymous():
if request_user is not None and not request_user.is_anonymous:
login(request, request_user)
return fn(request, *args, **kws)
else:
Expand Down Expand Up @@ -113,7 +113,7 @@ def _store(request, instrument, run_id=None, as_user=False):
@param run_id: run number
@param as_user: if True, we will store as user data
"""
if request.user.is_authenticated() and 'file' in request.FILES:
if request.user.is_authenticated and 'file' in request.FILES:
raw_data = request.FILES['file'].read()
data_type_default = PlotData.get_data_type_from_data(raw_data)
data_type = request.POST.get('data_type', default=data_type_default)
Expand Down Expand Up @@ -150,7 +150,7 @@ def get_data_list(request, instrument):
"""
Get a list of user data
"""
if request.user.is_authenticated():
if request.user.is_authenticated:
instrument_object = get_object_or_404(Instrument, name=instrument.lower())
data_list = []
for item in DataRun.objects.filter(instrument=instrument_object):
Expand Down

0 comments on commit 1c7ac23

Please # to comment.