Fakultas Ilmu Komputer UI

Commit 828cda39 authored by Zamil Majdy's avatar Zamil Majdy
Browse files

[#140382397] #8 Add Accounts Model (Supervisor, Student, Company)

parent ff116791
from django.contrib import admin
# Register your models here.
from core.models.accounts import Company, Supervisor, Student
admin.site.register(Company)
admin.site.register(Student)
admin.site.register(Supervisor)
\ No newline at end of file
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-19 13:22
from __future__ import unicode_literals
import core.models.accounts
from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Company',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('description', models.TextField()),
('verified', models.BooleanField(default=False)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Student',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('npm', models.IntegerField(unique=True, validators=[django.core.validators.MinValueValidator(100000000), django.core.validators.MaxValueValidator(9999999999L)])),
('resume', models.FileField(blank=True, null=True, upload_to=core.models.accounts.get_file_path)),
('phone_number', models.CharField(blank=True, db_index=True, max_length=100)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Supervisor',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
('nip', models.IntegerField(unique=True, validators=[django.core.validators.MinValueValidator(100000000), django.core.validators.MaxValueValidator(9999999999L)])),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
import os
import uuid
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator, MaxValueValidator
def get_file_path(instance, filename):
extension = filename.split('.')[-1].lower()
filename = "%s.%s" % (uuid.uuid4(), extension)
return os.path.join("student-resume/", filename)
def get_display_name(user, full_name=False):
"""
Return a display name that always works like "Benoit J."
"""
if user.first_name and user.last_name:
if full_name:
last = user.last_name.title()
else:
last = user.last_name[:1].upper() + "."
name = user.first_name.title() + " " + last
elif user.first_name:
name = user.first_name.title()
elif user.last_name:
name = user.last_name.title()
else:
name = user.username
return name.strip()
class Student(models.Model):
"""
A user subclass
"""
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
user = models.OneToOneField(User)
npm = models.IntegerField(validators=[MinValueValidator(100000000), MaxValueValidator(9999999999)], unique=True)
resume = models.FileField(upload_to=get_file_path, null=True, blank=True)
phone_number = models.CharField(max_length=100, blank=True, db_index=True)
def __unicode__(self):
return u"Student {}".format(get_display_name(self.user))
class Company(models.Model):
"""
A user subclass
"""
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
user = models.OneToOneField(User)
description = models.TextField()
verified = models.BooleanField(default=False)
def __unicode__(self):
return u"Company {}".format(get_display_name(self.user))
class Supervisor(models.Model):
"""
A user subclass
"""
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
user = models.OneToOneField(User)
nip = models.IntegerField(validators=[MinValueValidator(100000000), MaxValueValidator(9999999999)], unique=True)
def __unicode__(self):
return u"Supervisor {}".format(get_display_name(self.user))
from django.contrib.auth.models import User
from rest_framework import serializers
from core.models.accounts import Supervisor, Company, Student
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = '__all__'
class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = '__all__'
class SupervisorSerializer(serializers.ModelSerializer):
class Meta:
model = Supervisor
fields = '__all__'
\ No newline at end of file
from django.contrib.auth.models import User
from rest_framework import serializers, viewsets
from rest_framework import viewsets
from rest_framework.decorators import list_route
from rest_framework.response import Response
from core.models.accounts import Student, Company, Supervisor
from core.serializers.accounts import UserSerializer, StudentSerializer, CompanySerializer, SupervisorSerializer
# Serializers define the API representation.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
......@@ -23,3 +18,16 @@ class UserViewSet(viewsets.ModelViewSet):
return Response(serializer.data)
class StudentViewSet(viewsets.ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
class CompanyViewSet(viewsets.ModelViewSet):
queryset = Company.objects.all()
serializer_class = CompanySerializer
class SupervisorViewSet(viewsets.ModelViewSet):
queryset = Supervisor.objects.all()
serializer_class = SupervisorSerializer
......@@ -13,23 +13,32 @@ Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls import include
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from rest_framework import routers
from rest_framework_swagger.views import get_swagger_view
from core import apps
from core.views.user import UserViewSet
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet
schema_view = get_swagger_view()
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
router.register(r'students', StudentViewSet)
router.register(r'companies', CompanyViewSet)
router.register(r'supervisors', SupervisorViewSet)
urlpatterns = [
urlpatterns = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [
url(r'^api/api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api$', schema_view),
url(r"^api/", include(router.urls)),
url(r'^admin/', admin.site.urls),
url(r'', apps.index, name="index"),
]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment