Skip to content

Commit

Permalink
Merge pull request #41 from gwu-libraries/t16-try2
Browse files Browse the repository at this point in the history
refs #16. Fire harvest start message on seed update.
  • Loading branch information
justinlittman committed Oct 23, 2015
2 parents 4fa9de7 + 3f742f1 commit 16d5a08
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 62 deletions.
2 changes: 1 addition & 1 deletion docker/app-dev/invoke.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
echo "Waiting for db"
appdeps.py --wait-secs 30 --port-wait db:5432 --file /opt/sfm-ui
appdeps.py --wait-secs 30 --port-wait db:5432 --file /opt/sfm-ui --port-wait mq:5672
if [ "$?" = "1" ]; then
echo "Problem with application dependencies."
exit 1
Expand Down
9 changes: 9 additions & 0 deletions docker/example.dev.docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
sfmdevrabbit:
image: rabbitmq:3-management
ports:
- "15672:15672"
restart: always
environment:
- RABBITMQ_DEFAULT_USER=sfm_user
- RABBITMQ_DEFAULT_PASS=password
sfmdevdb:
image: gwul/sfm-ui-db
environment:
Expand All @@ -8,6 +16,7 @@ sfmdevapp:
- "8080:80"
links:
- sfmdevdb:db
- sfmdevrabbit:mq
volumes:
- ~/sfm-ui:/opt/sfm-ui
environment:
Expand Down
1 change: 1 addition & 0 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ psycopg2
pytz
django-crispy-forms
appdeps
pika
2 changes: 1 addition & 1 deletion sfm/sfm/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'allauth', # registration
'allauth.account', # registration
'allauth.socialaccount', # registration
'crispy_forms',
'crispy_forms', # for django crispy forms
]

MIDDLEWARE_CLASSES = (
Expand Down
1 change: 1 addition & 0 deletions sfm/ui/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'ui.rabbit.RabbitWorker'
28 changes: 28 additions & 0 deletions sfm/ui/rabbit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.apps import AppConfig
import pika
import os


class RabbitWorker(AppConfig):
name = 'ui'
verbose_name = "ui"
# Create a connection
credentials = pika.PlainCredentials(
username=os.environ['MQ_ENV_RABBITMQ_DEFAULT_USER'],
password=os.environ['MQ_ENV_RABBITMQ_DEFAULT_PASS'])
parameters = pika.ConnectionParameters(host='mq', credentials=credentials)
connection = pika.BlockingConnection(parameters)
# create channel
channel = connection.channel()

def ready(self):
# Declare sfm_exchange
RabbitWorker.channel.exchange_declare(exchange="sfm_exchange",
type="topic", durable=True)
# Declare harvester queue
RabbitWorker.channel.queue_declare(queue="sfm_exchange", durable=True)
# Bind
RabbitWorker.channel.queue_bind(exchange="sfm_exchange",
queue="sfm_exchange",
routing_key="sfm_exchange")
pass # startup code here
7 changes: 4 additions & 3 deletions sfm/ui/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
SeedListView, SeedCreateView, SeedDetailView,
SeedUpdateView, SeedDeleteView)
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse_lazy


urlpatterns = patterns('',

url(r'^collections/create/$', # NOQA
url(r'^collections/create/$',
login_required(CollectionCreateView.as_view()),
name="collection_create"),

Expand All @@ -31,7 +32,7 @@
CollectionListView.as_view(),
name="collection_list"),

url(r'^seedsets/create/$', # NOQA
url(r'^seedsets/create/$',
login_required(SeedSetCreateView.as_view()),
name="seedset_create"),

Expand All @@ -51,7 +52,7 @@
SeedSetListView.as_view(),
name="seedset_list"),

url(r'^seeds/create/$', # NOQA
url(r'^seeds/create/$',
login_required(SeedCreateView.as_view()),
name="seed_create"),

Expand Down
112 changes: 55 additions & 57 deletions sfm/ui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.list import ListView
from .models import Collection, SeedSet, Seed
from .models import Credential
from .forms import CollectionForm, SeedSetForm, SeedForm
from django.core.urlresolvers import reverse_lazy, reverse

# Create your views here.

import json
from .rabbit import RabbitWorker

class CollectionListView(ListView):
model = Collection
template_name = 'ui/collection_list.html'
paginate_by = 20
context_object_name = 'collection_list'
allow_empty = True
page_kwarg = 'page'
paginate_orphans = 0


class CollectionDetailView(DetailView):
model = Collection
template_name = 'ui/collection_detail.html'
context_object_name = 'collection'
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'


class CollectionCreateView(CreateView):
Expand All @@ -38,11 +32,6 @@ class CollectionUpdateView(UpdateView):
model = Collection
form_class = CollectionForm
template_name = 'ui/collection_update.html'
initial = {}
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'
context_object_name = 'collection'

def get_success_url(self):
return reverse("collection_detail", args=(self.object.pk,))
Expand All @@ -51,32 +40,20 @@ def get_success_url(self):
class CollectionDeleteView(DeleteView):
model = Collection
template_name = 'ui/collection_delete.html'
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'
context_object_name = 'collection'

def get_success_url(self):
return reverse('collection_list')
success_url = reverse_lazy('collection_list')


class SeedSetListView(ListView):
model = SeedSet
template_name = 'ui/seedset_list.html'
paginate_by = 20
context_object_name = 'seedset_list'
allow_empty = True
page_kwarg = 'page'
paginate_orphans = 0


class SeedSetDetailView(DetailView):
model = SeedSet
template_name = 'ui/seedset_detail.html'
context_object_name = 'seedset'
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'


class SeedSetCreateView(CreateView):
Expand All @@ -90,11 +67,55 @@ class SeedSetUpdateView(UpdateView):
model = SeedSet
form_class = SeedSetForm
template_name = 'ui/seedset_update.html'
initial = {}
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'
context_object_name = 'seedset'

def post(self, request, *args, **kwargs):
# To get value of collection id
for idval in list(Collection.objects.filter(
id=self.request.POST.get('collection')).values('id')):
if 'id' in idval:
value = idval['id']
# To get value of token in credentials. Where we pass our secret and key
for token in list(Credential.objects.filter(
id=self.request.POST.get('credential')).values('token')):
if 'token' in token:
credential = token['token']
# To get value of platform, which is used in routing key later
for platform in list(Credential.objects.filter(
id=self.request.POST.get('credential')).values('platform')):
if 'platform' in platform:
media = platform['platform']
# To get list of seeds
seeds = list(Seed.objects.filter(
seed_set=self.get_object().id).select_related('seeds').values(
'token', 'uid'))
# To Remove empty token values from the list of seeds
for item in seeds:
if item['token'] == '':
item.pop('token', None)
# To get value of seedset id
seedset = self.get_object()
# To be updated later
credential = json.loads(str(credential))
options = json.loads(str(self.request.POST.get('harvest_options')))
# Routing key
key = ''.join(['harvest.start.',str(media),'.',
self.request.POST.get('harvest_type')])
m = {
'id': str(seedset.id),
'type': self.request.POST.get('harvest_type'),
'options': options,
'credentials': credential,
'collection': {
'id': str(value),
'path': '/tmp/collection/'+str(value)
},
'seeds': seeds
}
RabbitWorker.channel.basic_publish(exchange='sfm_exchange',
routing_key=key,
body=json.dumps(m))
self.object = self.get_object()
return super(UpdateView, self).post(request, *args, **kwargs)

def get_success_url(self):
return reverse("seedset_detail", args=(self.object.pk,))
Expand All @@ -103,32 +124,20 @@ def get_success_url(self):
class SeedSetDeleteView(DeleteView):
model = SeedSet
template_name = 'ui/seedset_delete.html'
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'
context_object_name = 'seedset'

def get_success_url(self):
return reverse('seedset_list')
success_url = reverse_lazy('seedset_list')


class SeedListView(ListView):
model = Seed
template_name = 'ui/seed_list.html'
paginate_by = 20
context_object_name = 'seed_list'
allow_empty = True
page_kwarg = 'page'
paginate_orphans = 0


class SeedDetailView(DetailView):
model = Seed
template_name = 'ui/seed_detail.html'
context_object_name = 'seed'
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'


class SeedCreateView(CreateView):
Expand All @@ -142,11 +151,6 @@ class SeedUpdateView(UpdateView):
model = Seed
form_class = SeedForm
template_name = 'ui/seed_update.html'
initial = {}
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'
context_object_name = 'seed'

def get_success_url(self):
return reverse("seed_detail", args=(self.object.pk,))
Expand All @@ -155,10 +159,4 @@ def get_success_url(self):
class SeedDeleteView(DeleteView):
model = Seed
template_name = 'ui/seed_delete.html'
slug_field = 'slug'
slug_url_kwarg = 'slug'
pk_url_kwarg = 'pk'
context_object_name = 'seed'

def get_success_url(self):
return reverse('seed_list')
success_url = reverse_lazy('seed_list')

0 comments on commit 16d5a08

Please sign in to comment.