Skip to content

Commit

Permalink
added js file
Browse files Browse the repository at this point in the history
  • Loading branch information
mnuthan1 committed Apr 20, 2015
1 parent 4c48ba4 commit 0bbb703
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 8 deletions.
Binary file added workflow/users/.views.py.swp
Binary file not shown.
32 changes: 32 additions & 0 deletions workflow/users/controllers/register.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Register controller
* @namespace thinkster.authentication.controllers
*/
(function () {
'use strict';

angular
.module('workflow.authentication.controllers')
.controller('RegisterController', RegisterController);

RegisterController.$inject = ['$location', '$scope', 'Authentication'];

/**
* @namespace RegisterController
*/
function RegisterController($location, $scope, Authentication) {
var vm = this;

vm.register = register;

/**
* @name register
* @desc Register a new user
* @memberOf thinkster.authentication.controllers.RegisterController
*/
function register() {
Authentication.register(vm.email, vm.password, vm.username);
}
}
})();

Binary file modified workflow/users/models.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions workflow/users/permissios.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import permissions


class IsAccountOwner(permissions.BasePermission):
def has_object_permission(self, request, view, account):
if request.user:
return account == request.user
return False
42 changes: 42 additions & 0 deletions workflow/users/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content="Nuthan" />
<title>Workflow - Authentication</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</head>

<body>

<div class="row">
<div class="col-md-4 col-md-offset-4">
<h1>Register</h1>

<div class="well">
<form role="form" ng-submit="vm.register()">
<div class="form-group">
<label for="register__email">Email</label>
<input type="email" class="form-control" id="register__email" ng-model="vm.email" placeholder="ex. [email protected]" />
</div>

<div class="form-group">
<label for="register__username">Username</label>
<input type="text" class="form-control" id="register__username" ng-model="vm.username" placeholder="ex. john" />
</div>

<div class="form-group">
<label for="register__password">Password</label>
<input type="password" class="form-control" id="register__password" ng-model="vm.password" placeholder="ex. thisisnotgoogleplus" />
</div>

<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
37 changes: 37 additions & 0 deletions workflow/users/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

#from django.contrib.auth import update_session_auth_hash

from rest_framework import serializers

from users.models import UserAccount


class UserAccountSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=False)
confirm_password = serializers.CharField(write_only=True, required=False)

class Meta:
model = UserAccount
fields = ('id', 'email', 'username', 'created', 'modified', 'first_name', 'last_name', 'tagline', 'password', 'confirm_password',)
read_only_fields = ('created', 'modified',)

def create(self, validated_data):
return UserAccount.objects.create(**validated_data)

def update(self, instance, validated_data):
instance.username = validated_data.get('username', instance.username)
instance.tagline = validated_data.get('tagline', instance.tagline)

instance.save()

password = validated_data.get('password', None)
confirm_password = validated_data.get('confirm_password', None)

if password and confirm_password and password == confirm_password:
instance.set_password(password)
instance.save()

# this is required to update the seesion otherwise user has to loing again
#update_session_auth_hash(self.context.get('request'), instance)
return instance

Binary file added workflow/users/serializers.pyc
Binary file not shown.
48 changes: 48 additions & 0 deletions workflow/users/services/authentication.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Authentication
* @namespace thinkster.authentication.services
*/
(function () {
'use strict';

angular
.module('workflow.authentication.services')
.factory('Authentication', Authentication);

Authentication.$inject = ['$cookies', '$http'];

/**
* @namespace Authentication
* @returns {Factory}
*/
function Authentication($cookies, $http) {
/**
* @name Authentication
* @desc The Factory to be returned
*/
var Authentication = {
register: register
};

return Authentication;

////////////////////

/**
* @name register
* @desc Try to register a new user
* @param {string} username The username entered by the user
* @param {string} password The password entered by the user
* @param {string} email The email entered by the user
* @returns {Promise}
* @memberOf thinkster.authentication.services.Authentication
*/
function register(email, password, username) {
return $http.post('/api/v1/accounts/', {
username: username,
password: password,
email: email
});
}
}
})();
32 changes: 32 additions & 0 deletions workflow/users/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
from django.shortcuts import render
from rest_framework import permissions, viewsets

from users.models import UserAccount
from users.permissions import IsAccountOwner
from users.serializers import UserAccountSerializer

# Create your views here.

class AccountViewSet(viewsets.ModelViewSet):
lookup_field = 'username'
queryset = Account.objects.all()
serializer_class = UserAccountSerializer

def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.AllowAny(),)

if self.request.method == 'POST':
return (permissions.AllowAny(),)

return (permissions.IsAuthenticated(), IsAccountOwner(),)

def create(self, request):
serializer = self.serializer_class(data=request.data)

if serializer.is_valid():
UserAccount.objects.create_user(**serializer.validated_data)

return Response(serializer.validated_data, status=status.HTTP_201_CREATED)

return Response({
'status': 'Bad request',
'message': 'Account could not be created with received data.'
}, status=status.HTTP_400_BAD_REQUEST)
2 changes: 2 additions & 0 deletions workflow/workflow/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# Application definition

INSTALLED_APPS = (
'rest_framework',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
Expand All @@ -40,6 +41,7 @@
'users',
)


MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
Expand Down
Binary file modified workflow/workflow/settings.pyc
Binary file not shown.
19 changes: 11 additions & 8 deletions workflow/workflow/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from django.conf.urls import patterns, include, url
# .. Imports
from rest_framework_nested import routers

from django.contrib import admin
admin.autodiscover()
from authentication.views import AccountViewSet

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'workflow.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
router = routers.SimpleRouter()
router.register(r'accounts', AccountViewSet)

url(r'^admin/', include(admin.site.urls)),
urlpatterns = patterns(
'',
# ... URLs
url(r'^api/v1/', include(router.urls)),

url('^.*$', IndexView.as_view(), name='index'),
)

0 comments on commit 0bbb703

Please sign in to comment.