Skip to content

Commit affaed7

Browse files
committed
Add a scheduler that updates UI when ...
authors in the database is changed. Also add a REST method to get number of authors (to check if they are changed) and a management command to add a new author.
1 parent 3d3443d commit affaed7

File tree

9 files changed

+727
-906
lines changed

9 files changed

+727
-906
lines changed

books/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.core.management.base import BaseCommand, CommandError
2+
from books.models import Author
3+
import random, string
4+
5+
class Command(BaseCommand):
6+
help = 'Closes the specified poll for voting'
7+
8+
9+
def handle(self, *args, **options):
10+
author = Author.objects.create(
11+
first_name=random.choice(string.ascii_uppercase) + ''.join(random.choice(string.ascii_lowercase) for _ in range(7)),
12+
last_name=random.choice(string.ascii_uppercase) + ''.join(random.choice(string.ascii_lowercase) for _ in range(7)),
13+
)
14+
print "Author {} {} added ".format(author.first_name, author.last_name)

books/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from django.shortcuts import render
22
from django.views.generic import TemplateView
33
from rest_framework import viewsets, filters
4+
from rest_framework.decorators import detail_route, list_route
45
from rest_framework.pagination import PageNumberPagination
6+
from rest_framework.response import Response
57
from books.models import Book, Category, SubCategory, Author
68
from books.serializers import BookSerializer, CategorySerializer, SubCategorySerializer, AuthorSerializer
79

@@ -35,6 +37,12 @@ class AuthorViewSet(viewsets.ModelViewSet):
3537
search_fields = ('last_name', 'first_name', )
3638
ordering_fields = ('id', 'last_name', )
3739
ordering = ('last_name',)
40+
41+
@list_route()
42+
def get_author_number(self, request):
43+
return Response(Author.objects.all().count())
44+
45+
3846

3947
class SubCategoryViewSet(viewsets.ModelViewSet):
4048
queryset = SubCategory.objects.all()

srcjs/components/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import LoadingContainer from './loading';
77
import StatPanel from './StatPanel'
88
import { bindActionCreators } from 'redux'
99

10+
1011
class App extends React.Component {
1112

1213
render() {

srcjs/components/notification.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { hideNotification } from '../actions'
55

66
import * as colors from '../util/colors'
77

8+
89
const NotificationContainer = (props) => {
910
let { message, notification_type } = props.notification;
1011
let { onHide } = props;

srcjs/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import AuthorPanel from './components/AuthorPanel';
1313
import BookForm from './components/BookForm';
1414
import AuthorForm from './components/AuthorForm';
1515

16+
import schedule from './scheduler';
17+
18+
19+
1620
const About = () => {
1721
return <div>
1822
<h2>About</h2>
@@ -48,4 +52,4 @@ render((
4852
)
4953

5054

51-
55+
schedule();

srcjs/scheduler.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import store from './store'
2+
import {loadAuthors, showSuccessNotification} from './actions';
3+
4+
5+
export default () => {
6+
console.log("Starting scheduler")
7+
window.setInterval( () => {
8+
9+
let url = 'http://127.0.0.1:8000/api/authors/get_author_number/';
10+
$.get(url, realAuthorNumber => {
11+
let authorNumber = store.getState().authors.rows.length;
12+
if(authorNumber!=realAuthorNumber) {
13+
store.dispatch(showSuccessNotification("Authors have changed - reloading ..."));
14+
store.dispatch(loadAuthors());
15+
}
16+
});
17+
18+
}, 2000);
19+
};

0 commit comments

Comments
 (0)