Skip to content

Commit

Permalink
Merge pull request RyanNoelk#70 from RyanNoelk/dev
Browse files Browse the repository at this point in the history
dev to master for version 1.0.3
  • Loading branch information
RyanNoelk authored Jan 21, 2017
2 parents e14d758 + 9eca915 commit 0d15033
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 83 deletions.
9 changes: 8 additions & 1 deletion api/base/settings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# Django settings for openeats project.
import os

DEBUG = os.environ.get('DJANGO_DEBUG', True)
# We can't set the debug just using the env var.
# Python with evaluate any string as a True bool.
DEBUG = False
if os.environ.get('DJANGO_DEBUG', None).lower() == 'true':
DEBUG = True

SERVE_MEDIA = True

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Expand Down Expand Up @@ -64,6 +69,7 @@
'django.middleware.locale.LocaleMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
)

Expand All @@ -72,6 +78,7 @@
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.staticfiles',

Expand Down
3 changes: 2 additions & 1 deletion api/v1/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

from django.conf.urls import url
from rest_framework.authtoken.views import obtain_auth_token
from v1.accounts.views import custom_obtain_auth_token

urlpatterns = [url(r'^obtain-auth-token/$', obtain_auth_token)]
urlpatterns = [url(r'^obtain-auth-token/$', custom_obtain_auth_token)]
12 changes: 12 additions & 0 deletions api/v1/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from rest_framework import viewsets
from rest_framework.response import Response
from django.contrib.auth.models import User
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token

from .serializers import UserSerializer

Expand All @@ -18,3 +20,13 @@ def retrieve(self, request, pk=None):
if pk == 'i':
return Response(UserSerializer(request.user, context={'request':request}).data)
return super(UserViewSet, self).retrieve(request, pk)

class CustomObtainAuthToken(ObtainAuthToken):
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({'id': user.id, 'token': token.key})

custom_obtain_auth_token = CustomObtainAuthToken.as_view()
6 changes: 3 additions & 3 deletions frontend/modules/account/actions/AuthActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ export default {
.send({'username': username, 'password': pass})
.end((err, res) => {
if (!err && res) {
this.logUserIn({token: res.body.token});
this.logUserIn(res.body);
} else {
this.logInError({error: true});
console.error(url, err.toString());
}
})
},

logUserIn: (token) => {
logUserIn: (user) => {
AppDispatcher.dispatch({
actionType: AuthConstants.LOGIN_USER,
token: token.token
user: user
});
},

Expand Down
20 changes: 13 additions & 7 deletions frontend/modules/account/stores/AuthStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const CHANGE_EVENT = 'change';

var _errors = '';

function setUser(token) {
if (!localStorage.getItem('id_token')) {
localStorage.setItem('id_token', token);
function setUser(user) {
if (!localStorage.getItem('user')) {
localStorage.setItem('user', JSON.stringify(user));
}
}

function removeUser() {
localStorage.removeItem('id_token');
localStorage.removeItem('user');
}

class AuthStoreClass extends EventEmitter {
Expand All @@ -31,14 +31,19 @@ class AuthStoreClass extends EventEmitter {
}

isAuthenticated() {
if (localStorage.getItem('id_token')) {
if (localStorage.getItem('user')) {
return true;
}
return false;
}

getToken() {
return localStorage.getItem('id_token');
const user = this.getUser();
return user.token;
}

getUser() {
return JSON.parse(localStorage.getItem('user'));
}

getErrors() {
Expand All @@ -56,7 +61,7 @@ AuthStore.dispatchToken = AppDispatcher.register(action => {
switch(action.actionType) {

case AuthConstants.LOGIN_USER:
setUser(action.token);
setUser(action.user);
AuthStore.emitChange();
browserHistory.push('/');
break;
Expand All @@ -69,6 +74,7 @@ AuthStore.dispatchToken = AppDispatcher.register(action => {
case AuthConstants.LOGOUT_USER:
removeUser();
AuthStore.emitChange();
browserHistory.push('/');
break;

default:
Expand Down
16 changes: 1 addition & 15 deletions frontend/modules/base/components/Nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default injectIntl(React.createClass({
});

return (
<Navbar>
<Navbar collapseOnSelect>
<Navbar.Header>
<Navbar.Brand>
<Link className="navbar-brand" to="/">{formatMessage(messages.brand)}</Link>
Expand All @@ -65,20 +65,6 @@ export default injectIntl(React.createClass({
}
}));

// TODO: I want to wave this code for now,
// since I still kind of want to use it in the future
//import Form from 'react-router-form'
//"react-router-form": "^2.0.0-rc.0",
/*<ul className="nav navbar-nav navbar-right">
<Form className="navbar-form" to={'/browse'} method="GET">
<div className="form-group">
<input type="text" name="search" className="form-control" placeholder="Search"/>
</div>
<button type="submit" className="btn btn-default">Submit</button>
</Form>
</ul>*/


var Account = React.createClass({
getInitialState: function() {
return getAuthState();
Expand Down
4 changes: 2 additions & 2 deletions frontend/modules/browse/components/Browse.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default injectIntl(React.createClass({
return (
<div className="container">
<div className="row">
<div className="col-xs-2 sidebar">
<div className="sidebar col-sm-2 hidden-xs">
<div className="sidebar">
<Filter title="course"
data={ this.state.course }
Expand All @@ -168,7 +168,7 @@ export default injectIntl(React.createClass({
/>
</div>
</div>
<div className="col-xs-10">
<div className="col-sm-10 col-xs-12">
<div className="row">
<SearchBar format="col-xs-12" filter={ this.filter }/>
</div>
Expand Down
47 changes: 33 additions & 14 deletions frontend/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,45 @@ import News from './news/components/News'
import Browse from './browse/components/Browse'
import { RecipeForm } from './recipe_form/components/RecipeForm'
import Recipe from './recipe/components/Recipe'
import AuthStore from './account/stores/AuthStore'

// Load in the base CSS
require("./base/css/footer.css");

const requireAuth = (nextState, replace) => {
if (!AuthStore.isAuthenticated()) {
replace('/browse');
}
};

const routeConfig = [
{ path: '/',
component: App,
indexRoute: { component: News },
childRoutes: [
{ path: 'news', component: News },
{ path: 'about', component: About },
{ path: 'login', component: Login },
{ path: 'browse', component: Browse },
{ path: 'recipe',
childRoutes: [
{ path: 'create', component: RecipeForm, onEnter: requireAuth },
{ path: 'edit/:id', component: RecipeForm, onEnter: requireAuth },
{ path: ':recipe', component: Recipe }
]
},
{ path: '*', component: NotFound }
]
}
]

render((
<IntlProvider locale={process.env.LOCALE} messages={messages}>
<IntlProvider locale={ process.env.LOCALE } messages={ messages }>
<div>
<Router history={browserHistory}>
<Route path="/" component={App}>
<IndexRoute component={News}/>
<Route path="/news" component={News}/>
<Route path="/recipe/create" component={RecipeForm} /*onEnter={requireAuth}*//>
<Route path="/recipe/edit/:recipe" component={RecipeForm} /*onEnter={requireAuth}*//>
<Route path="/recipe/:recipe" component={Recipe} />
<Route path="/about" component={About}/>
<Route path="/login" component={Login}/>
<Route path="/browse" component={Browse}/>
<Route path="*" component={NotFound} />
</Route>
</Router>
<Router
history={ browserHistory }
routes={ routeConfig }
/>
</div>
</IntlProvider>
),
Expand Down
3 changes: 2 additions & 1 deletion frontend/modules/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
"recipe.servings": "Servings",
"recipe.ingredients": "Ingredients",
"recipe.instructions": "Instructions",
"recipe.photo_placeholder": "Generic placeholder thumbnail"
"recipe.photo_placeholder": "Generic placeholder thumbnail",
"recipe.edit_recipe": "Edit recipe"
}
3 changes: 2 additions & 1 deletion frontend/modules/locale/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
"recipe.servings": "Sirve",
"recipe.ingredients": "Ingredientes",
"recipe.instructions": "Instrucciones",
"recipe.photo_placeholder": "Foto generica"
"recipe.photo_placeholder": "Foto generica",
"recipe.edit_recipe": "Modificar receta"
}
Loading

0 comments on commit 0d15033

Please sign in to comment.