Commit bb6c057b44c3b4881ddb4aa41ba6a36ada65a878
1 parent
5945a147
Exists in
master
and in
5 other branches
added models serializes and urls #271 as well as courses app serializers #262
Showing
6 changed files
with
27 additions
and
2 deletions
Show diff stats
core/urls.py
... | ... | @@ -4,9 +4,13 @@ from django.contrib.auth.views import password_reset, password_reset_done,passwo |
4 | 4 | from . import views |
5 | 5 | from rest_framework import routers |
6 | 6 | |
7 | +from users.views import UserViewSet | |
7 | 8 | |
9 | +#API CODE | |
8 | 10 | router = routers.DefaultRouter() |
9 | 11 | router.register(r'logs', views.LogViewSet) |
12 | +router.register(r'usersapi', UserViewSet) | |
13 | + | |
10 | 14 | urlpatterns = [ |
11 | 15 | url(r'^$', views.login, name='home'), |
12 | 16 | url(r'^register/$', views.RegisterUser.as_view(), name='register'), | ... | ... |
courses/serializers.py
1 | 1 | from rest_framework import serializers |
2 | 2 | from .models import Course, Subject, Topic |
3 | +from users.serializers import UserSerializer | |
3 | 4 | |
4 | 5 | class CourseSerializer(serializers.ModelSerializer): |
6 | + #The set comes from the ManyToMany Relationship in django | |
7 | + students = UserSerializer(source='courses_student') | |
8 | + professors = UserSerializer(source='courses_professors') | |
5 | 9 | class Meta: |
6 | 10 | model = Course |
7 | 11 | fields = ('name', 'slug', 'objectivies', 'content, max_students', 'create_date', | ... | ... |
courses/views.py
users/serializers.py
... | ... | @@ -4,6 +4,6 @@ from .models import User |
4 | 4 | class UserSerializer(serializers.ModelSerializer): |
5 | 5 | class Meta: |
6 | 6 | model = User |
7 | - fields = ('username','email','name','city','state','gender','image','birth_date','phone' | |
8 | - 'cpf','type_profile','titration','year_tritation','institution','curriculum','date_created', | |
7 | + fields = ('username','email','name','city','state','gender','image','birth_date','phone', | |
8 | + 'cpf','type_profile','titration','year_titration','institution','curriculum','date_created', | |
9 | 9 | 'is_staff','is_active') | ... | ... |
users/urls.py
users/views.py
... | ... | @@ -20,6 +20,10 @@ from files.models import * |
20 | 20 | from exam.models import * |
21 | 21 | from courses.models import * |
22 | 22 | |
23 | +#API IMPORTS | |
24 | +from rest_framework import viewsets | |
25 | +from .serializers import UserSerializer | |
26 | +from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly | |
23 | 27 | |
24 | 28 | # ================ ADMIN ======================= |
25 | 29 | class UsersListView(HasRoleMixin, LoginRequiredMixin, generic.ListView): |
... | ... | @@ -231,3 +235,12 @@ class SearchView(LoginRequiredMixin, generic.ListView): |
231 | 235 | context['qtd'] = qtd |
232 | 236 | |
233 | 237 | return context |
238 | + | |
239 | + | |
240 | +# API VIEWS | |
241 | + | |
242 | +class UserViewSet(viewsets.ModelViewSet): | |
243 | + queryset = User.objects.all() | |
244 | + serializer_class = UserSerializer | |
245 | + permissions_classes = (IsAuthenticatedOrReadOnly,) | |
246 | + | ... | ... |