Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge branch 'group' into features/kulogin
Browse files Browse the repository at this point in the history
  • Loading branch information
whs committed Oct 4, 2015
2 parents d1ac184 + 6a3c434 commit f4fbacb
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ db.sqlite3
*.pyo
.idea/
docs/_build
.DS_Store
8 changes: 7 additions & 1 deletion User/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from django.contrib import admin

from models import UserProfile
# Register your models here.
class UserProfileAdmin(admin.ModelAdmin):
list_display = (
'user', 'birthday', 'gender',
'faculty', 'major', 'types')

admin.site.register(UserProfile, UserProfileAdmin)
30 changes: 30 additions & 0 deletions User/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='UserProfile',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('birthday', models.DateField(null=True, blank=True)),
('gender', models.CharField(max_length=5, choices=[(b'M', b'Male'), (b'F', b'Female')])),
('faculty', models.CharField(max_length=30)),
('major', models.CharField(max_length=30)),
('types', models.CharField(max_length=30)),
('user', models.OneToOneField(related_name='profile', to=settings.AUTH_USER_MODEL)),
],
options={
'db_table': 'youniversity_profile',
},
),
]
10 changes: 8 additions & 2 deletions User/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class CooklyProfile(models.Model):

GENDER = (
('M', 'Male'),
('F', 'Female'),
)

# Create your models here.1
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
birthday = models.DateField(blank=True, null=True)
gender = models.CharField(max_length=5, choices=GENDER)
Expand Down
3 changes: 2 additions & 1 deletion authapi/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import NotAuthenticated

class UserViewSet(APIView):
"""Validate current user session
Expand All @@ -22,4 +23,4 @@ def get(self, request):
if request.user.is_authenticated():
return Response(request.user.get_username())
else:
return Response('', 403)
raise NotAuthenticated()
Empty file added group/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions group/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.contrib import admin
from models import *


class GroupAdmin(admin.ModelAdmin):
list_display = (
'name', 'type', 'description', 'long_description',
'logo', 'header', 'permisssion')

class GroupMemberAdmin(admin.ModelAdmin):
list_display = (
'group_id','user_id', 'role')


admin.site.register(Group, GroupAdmin)
admin.site.register(GroupMember, GroupMemberAdmin)
41 changes: 41 additions & 0 deletions group/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
]

operations = [
migrations.CreateModel(
name='Group',
fields=[
('id', models.IntegerField(serialize=False, primary_key=True)),
('name', models.CharField(max_length=25)),
('type', models.IntegerField(default=0)),
('description', models.CharField(max_length=50)),
('long_description', models.CharField(max_length=200)),
('logo', models.CharField(max_length=25)),
('header', models.CharField(max_length=25)),
('permisssion', models.IntegerField(default=0)),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='Group_Member',
fields=[
('id', models.IntegerField(serialize=False, primary_key=True)),
('group_id', models.IntegerField(default=0)),
('user_id', models.IntegerField(default=0)),
('role', models.CharField(max_length=50)),
],
options={
},
bases=(models.Model,),
),
]
26 changes: 26 additions & 0 deletions group/migrations/0002_auto_20150920_1117.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('group', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='group',
name='id',
field=models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True),
preserve_default=True,
),
migrations.AlterField(
model_name='group_member',
name='id',
field=models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True),
preserve_default=True,
),
]
28 changes: 28 additions & 0 deletions group/migrations/0003_auto_20150921_1425.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('group', '0002_auto_20150920_1117'),
]

operations = [
migrations.CreateModel(
name='GroupMember',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('role', models.CharField(max_length=50)),
('group_id', models.ForeignKey(default=0, to='group.Group')),
('user_id', models.ForeignKey(default=0, to=settings.AUTH_USER_MODEL)),
],
),
migrations.DeleteModel(
name='Group_Member',
),
]
Empty file added group/migrations/__init__.py
Empty file.
21 changes: 21 additions & 0 deletions group/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.db import models
from django.contrib.auth.models import User


# Create your models here.

class Group(models.Model):
name = models.CharField(max_length=25)
type = models.IntegerField(default=0)
description = models.CharField(max_length=50)
long_description = models.CharField(max_length=200)
logo = models.CharField(max_length=25)
#logo_image = ImageField(upload_to=get_image_path, blank=True, null=True)
header = models.CharField(max_length=25)
#header_image = ImageField(upload_to=get_image_path, blank=True, null=True)
permisssion = models.IntegerField(default=0)

class GroupMember(models.Model):
group_id = models.ForeignKey(Group, default=0)
user_id = models.ForeignKey(User, default=0)
role = models.CharField(max_length=50)
7 changes: 7 additions & 0 deletions group/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework import serializers
from models import *

class GroupMemberSerializer(serializers.ModelSerializer):
class Meta:
model = GroupMember
fields = ('user_id', 'role')
3 changes: 3 additions & 0 deletions group/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
6 changes: 6 additions & 0 deletions group/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.conf.urls import include, url
from . import views

urlpatterns = [
url(r'^(?P<group_id>[0-9]+)/member', views.MemberViewSet.as_view(), name='GroupMember'),
]
40 changes: 40 additions & 0 deletions group/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from rest_framework.response import Response
from rest_framework.generics import ListCreateAPIView
from rest_framework.exceptions import ValidationError, NotAuthenticated, NotFound

from models import *
from serializers import *

class MemberViewSet(ListCreateAPIView):
serializer_class = GroupMemberSerializer

def get_group_id(self):
try:
return int(self.kwargs['group_id'])
except ValueError:
return ValidationError('group id cannot be parsed')

def get_queryset(self):
return GroupMember.objects.filter(group_id=self.get_group_id())

def create(self, *args, **kwargs):
if not self.request.user.is_authenticated():
raise NotAuthenticated

try:
group = Group.objects.get(id=self.get_group_id())
except Group.DoesNotExist:
raise NotFound('no such group')

member, created = GroupMember.objects.get_or_create(
group_id = Group.objects.get(id = self.get_group_id),
user_id = User.objects.get( id = self.request.user.id),
defaults = {
'role': 0
}
)

if not created:
raise ValidationError('request already exists')

return Response(GroupMemberSerializer(member).data)
6 changes: 6 additions & 0 deletions social/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework.authentication import SessionAuthentication

class SessionCsrfExemptAuthentication(SessionAuthentication):

def enforce_csrf(self, request):
return # To not perform the csrf check previously happening
9 changes: 9 additions & 0 deletions social/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
'authapi',
'ui',
'newsfeed'
'group',
"User"
)

MIDDLEWARE_CLASSES = (
Expand Down Expand Up @@ -116,3 +118,10 @@

SITE_ID = 2
LOGIN_REDIRECT_URLNAME = '/static/index.html'

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'social.auth.SessionCsrfExemptAuthentication',
)
}
1 change: 1 addition & 0 deletions social/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
url(r'^accounts/', include('allauth.urls')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api/auth/', include('authapi.urls')),
url(r'^api/group/', include('group.urls')),
]
11 changes: 10 additions & 1 deletion ui/static/app/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@

var app = angular.module('app.group', []);

app.controller('GroupController', function(){
app.controller('GroupController', function($scope, $stateParams, Restangular){
$scope.GroupApi = Restangular.one('group', $stateParams.id);
$scope.joinStatus = 0;
$scope.joinGroup = function(){
$scope.GroupApi.all('member').post().then(function(){
$scope.joinStatus = 1;
}, function(xhr){
alert(xhr.data);
});
};
});

app.controller('GroupInfoController', function(){
Expand Down
4 changes: 3 additions & 1 deletion ui/static/templates/group.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<div class="cover">
<div class="cover-in">
<div class="pull-right toolbar">
<div class="btn btn-default">Join group</div>
<div class="btn btn-default" ng-click="joinGroup()" ng-if="joinStatus==0">Join group</div>
<div class="btn btn-default" ng-if="joinStatus==1">Request pending</div>
<div class="btn btn-default" ng-if="joinStatus==2">Leave group</div>
</div>
<h3>Group name goes here</h3>
</div>
Expand Down

0 comments on commit f4fbacb

Please sign in to comment.