-
Notifications
You must be signed in to change notification settings - Fork 0
Backend Assignment #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iishagupta
wants to merge
12
commits into
master
Choose a base branch
from
develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7bd689a
second commit
iishagupta d53906b
Update serializers.py
iishagupta c4d8d5a
Update views.py
iishagupta 5792632
Delete db.sqlite3
iishagupta 4535426
Update urls.py
iishagupta d13924d
Update views.py
iishagupta b897eba
second commit
iishagupta d926729
resolve conflicts
iishagupta e285b58
Update views.py
iishagupta f59a295
Update serializers.py
iishagupta b2ea966
Update serializers.py
iishagupta 2400787
Delete tests.py
iishagupta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| """ | ||
| Django settings for Team project. | ||
|
|
||
| Generated by 'django-admin startproject' using Django 2.2.6. | ||
|
|
||
| For more information on this file, see | ||
| https://docs.djangoproject.com/en/2.2/topics/settings/ | ||
|
|
||
| For the full list of settings and their values, see | ||
| https://docs.djangoproject.com/en/2.2/ref/settings/ | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| # Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
| BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
|
||
|
|
||
| # Quick-start development settings - unsuitable for production | ||
| # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ | ||
|
|
||
| # SECURITY WARNING: keep the secret key used in production secret! | ||
| SECRET_KEY = 'nzi^1z_nf@68_8w#a=a%1fnm$$zxlxl&04&%me1f%vvk+3ccna' | ||
|
|
||
| # SECURITY WARNING: don't run with debug turned on in production! | ||
| DEBUG = True | ||
|
|
||
| ALLOWED_HOSTS = [] | ||
|
|
||
|
|
||
| # Application definition | ||
|
|
||
| INSTALLED_APPS = [ | ||
| 'teammembers.apps.TeammembersConfig', | ||
| 'django.contrib.admin', | ||
| 'django.contrib.auth', | ||
| 'django.contrib.contenttypes', | ||
| 'django.contrib.sessions', | ||
| 'django.contrib.messages', | ||
| 'django.contrib.staticfiles', | ||
| ] | ||
|
|
||
| MIDDLEWARE = [ | ||
| 'django.middleware.security.SecurityMiddleware', | ||
| 'django.contrib.sessions.middleware.SessionMiddleware', | ||
| 'django.middleware.common.CommonMiddleware', | ||
| 'django.middleware.csrf.CsrfViewMiddleware', | ||
| 'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
| 'django.contrib.messages.middleware.MessageMiddleware', | ||
| 'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
| ] | ||
|
|
||
| ROOT_URLCONF = 'Team.urls' | ||
|
|
||
| TEMPLATES = [ | ||
| { | ||
| 'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
| 'DIRS': [], | ||
| 'APP_DIRS': True, | ||
| 'OPTIONS': { | ||
| 'context_processors': [ | ||
| 'django.template.context_processors.debug', | ||
| 'django.template.context_processors.request', | ||
| 'django.contrib.auth.context_processors.auth', | ||
| 'django.contrib.messages.context_processors.messages', | ||
| ], | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| WSGI_APPLICATION = 'Team.wsgi.application' | ||
|
|
||
|
|
||
| # Database | ||
| # https://docs.djangoproject.com/en/2.2/ref/settings/#databases | ||
|
|
||
| DATABASES = { | ||
| 'default': { | ||
| 'ENGINE': 'django.db.backends.mysql', | ||
| 'NAME': 'instawork', | ||
| 'HOST': 'localhost', | ||
| 'PORT': '3306', | ||
| 'USER': 'root', | ||
| 'PASSWORD': 'isha1234' | ||
| } | ||
| } | ||
|
|
||
|
|
||
| # Password validation | ||
| # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators | ||
|
|
||
| # AUTH_PASSWORD_VALIDATORS = [ | ||
| # { | ||
| # 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | ||
| # }, | ||
| # { | ||
| # 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
| # }, | ||
| # { | ||
| # 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | ||
| # }, | ||
| # { | ||
| # 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | ||
| # }, | ||
| # ] | ||
|
|
||
|
|
||
| # Internationalization | ||
| # https://docs.djangoproject.com/en/2.2/topics/i18n/ | ||
|
|
||
| LANGUAGE_CODE = 'en-us' | ||
|
|
||
| TIME_ZONE = 'UTC' | ||
|
|
||
| USE_I18N = True | ||
|
|
||
| USE_L10N = True | ||
|
|
||
| USE_TZ = True | ||
|
|
||
|
|
||
| # Static files (CSS, JavaScript, Images) | ||
| # https://docs.djangoproject.com/en/2.2/howto/static-files/ | ||
|
|
||
| STATIC_URL = '/static/' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
|
|
||
| from django.contrib import admin | ||
| from django.urls import include, path | ||
|
|
||
| urlpatterns = [ | ||
| path('api/', include('teammembers.urls')), | ||
| path('admin/', admin.site.urls), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| """ | ||
| WSGI config for Team project. | ||
|
|
||
| It exposes the WSGI callable as a module-level variable named ``application``. | ||
|
|
||
| For more information on this file, see | ||
| https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from django.core.wsgi import get_wsgi_application | ||
|
|
||
| os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Team.settings') | ||
|
|
||
| application = get_wsgi_application() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/usr/bin/env python | ||
| """Django's command-line utility for administrative tasks.""" | ||
| import os | ||
| import sys | ||
|
|
||
|
|
||
| def main(): | ||
| os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Team.settings') | ||
| try: | ||
| from django.core.management import execute_from_command_line | ||
| except ImportError as exc: | ||
| raise ImportError( | ||
| "Couldn't import Django. Are you sure it's installed and " | ||
| "available on your PYTHONPATH environment variable? Did you " | ||
| "forget to activate a virtual environment?" | ||
| ) from exc | ||
| execute_from_command_line(sys.argv) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from django.contrib import admin | ||
| from .models import TeamMember | ||
|
|
||
| admin.site.register(TeamMember) | ||
|
|
||
| # Register your models here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class TeammembersConfig(AppConfig): | ||
| name = 'teammembers' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Generated by Django 2.2.6 on 2019-10-14 20:54 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='TeamMember', | ||
| fields=[ | ||
| ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
| ('firstName', models.CharField(max_length=200)), | ||
| ('lastName', models.CharField(max_length=200)), | ||
| ('email', models.CharField(max_length=200)), | ||
| ('mobile', models.CharField(max_length=14)), | ||
| ('role', models.IntegerField(choices=[(1, 'Admin'), (2, 'Regular')])), | ||
| ], | ||
| ), | ||
| ] |
Empty file.
Binary file added
BIN
+815 Bytes
Team/teammembers/migrations/__pycache__/0001_initial.cpython-37.pyc
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from django.db import models | ||
|
|
||
| class TeamMember(models.Model): | ||
| firstName = models.CharField(max_length=200, null=False, blank=False) | ||
| lastName = models.CharField(max_length=200, null=False, blank=False) | ||
| email = models.CharField(max_length=200, null=False, blank=False) | ||
| mobile = models.CharField(max_length=14, null=False, blank=False) | ||
| # Using choices field for standardization. A string other than "Admin" or "Regular" should not be entered | ||
| ROLE_CHOICES = ( | ||
| (1, ("Admin")), | ||
| (2, ("Regular")), | ||
| ) | ||
| role = models.IntegerField(choices=ROLE_CHOICES, null=False, blank=False) | ||
|
|
||
| def __str__(self): | ||
| return self.firstName + " " + self.lastName | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from rest_framework_api import LimitOffsetPagination, PageNumberPagination | ||
|
|
||
| class Pagination(pagination.PageNumberPagination): | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """ | ||
| Serializers allow querysets and model instances | ||
| to be converted to python datatypes | ||
| that can be rendered into JSON/ XML | ||
| """ | ||
| from rest_framework import serializers | ||
| from .models import TeamMember | ||
| from collections import OrderedDict | ||
| import re | ||
|
|
||
| # Stack Overlflow | ||
| class RoleChoicesField(serializers.Field): | ||
| def __init__(self, choices, **kwargs): | ||
| self._choices = OrderedDict(choices) | ||
| super(RoleChoicesField, self).__init__(**kwargs) | ||
|
|
||
| def to_representation(self, obj): | ||
| return self._choices[obj] | ||
|
|
||
| def to_internal_value(self, data): | ||
| for i in self._choices: | ||
| if self._choices[i] == data: | ||
| return i | ||
| raise serializers.ValidationError( | ||
| "Acceptable values are {0}.".format(list(self._choices.values()))) | ||
|
|
||
| class TeamMemberSerializer(serializers.Serializer): | ||
| # id = serializers.IntegerField(required=False) | ||
| id = serializers.IntegerField(required=False) | ||
| firstName = serializers.CharField(max_length=200) | ||
| lastName = serializers.CharField(max_length=200) | ||
| email = serializers.CharField(max_length=200) | ||
| mobile = serializers.CharField(max_length=14) | ||
| role = RoleChoicesField(TeamMember.ROLE_CHOICES) | ||
|
|
||
| def validate_data(self, validated_data): | ||
| mobile = validated_data.get('mobile') | ||
| email = validated_data.get('email') | ||
| # 1) Begins with 0 or 91 | ||
| # 2) Then contains 7 or 8 or 9. | ||
| # 3) Then contains 9 digits | ||
| pattern = re.compile("(0/91)?[7-9][0-9]{9}") | ||
| if not pattern.match(mobile): | ||
| raise serializers.ValidationError('Invalid Phone Number') | ||
| epattern = re.compile("^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$") | ||
| if not epattern.match(email): | ||
| raise serializers.ValidationError('Invalid Email') | ||
|
|
||
| def create(self, validated_data): | ||
| self.validate_data(validated_data) | ||
| return TeamMember.objects.create(**validated_data) | ||
|
|
||
| def update(self, instance, validated_data): | ||
iishagupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.validate_data(validated_data) | ||
| instance.firstName = validated_data.get('firstName', instance.firstName) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anyone can change data to any other user, there is not authorization here |
||
| instance.lastName = validated_data.get('lastName', instance.lastName) | ||
| instance.email = validated_data.get('email', instance.email) | ||
| instance.mobile = validated_data.get('mobile', instance.mobile) | ||
| instance.role = validated_data.get('role', instance.role) | ||
| instance.save() | ||
| return instance | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| from django.urls import path | ||
| from .views import TeamMemberListView | ||
|
|
||
| app_name = "teammembers" # helps do a reverse lookup later | ||
iishagupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| urlpatterns = [ | ||
| path('teammembers/', TeamMemberListView.as_view()), | ||
iishagupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| path('teammembers/<int:pk>/', TeamMemberListView.as_view()), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from django.http import JsonResponse, HttpResponse | ||
| from rest_framework.views import APIView | ||
| from rest_framework.response import Response | ||
| from .serializers import TeamMemberSerializer | ||
| from django.shortcuts import get_object_or_404 | ||
| from rest_framework.settings import api_settings | ||
|
|
||
| import json | ||
|
|
||
| from .models import TeamMember | ||
|
|
||
|
|
||
| class TeamMemberListView(APIView): | ||
| queryset = TeamMember.objects.all() | ||
| serializer_class = TeamMemberSerializer | ||
| pagination_class = api_settings.DEFAULT_PAGINATION_CLASS | ||
|
|
||
| def get(self, request): | ||
| page = self.paginate_queryset(self.queryset) | ||
| if page is not None: | ||
| serializer = self.serializer_class(page, many=True) | ||
| return self.get_paginated_response(serializer.data) | ||
|
|
||
| def post(self, request): | ||
iishagupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| team_member = request.data.get('team_member') | ||
| serializer = TeamMemberSerializer(data=team_member) | ||
| if serializer.is_valid(raise_exception=True): | ||
| team_member_saved = serializer.save() | ||
| return JsonResponse(TeamMemberSerializer(team_member_saved).data) | ||
|
|
||
| def put(self, request, pk): | ||
| saved_team_member = get_object_or_404(TeamMember, id=pk) | ||
iishagupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| data = request.data.get('team_member') | ||
| serializer = TeamMemberSerializer(instance=saved_team_member, data=data, partial=True) | ||
| if serializer.is_valid(raise_exception=True): | ||
| team_member_saved = serializer.save() | ||
| return JsonResponse(TeamMemberSerializer(team_member_saved).data) | ||
|
|
||
| def delete(self, request, pk): | ||
iishagupta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| team_member = get_object_or_404(TeamMember.objects.all(), id=pk) | ||
| team_member.delete() | ||
| return JsonResponse({"team_member": []}) | ||
|
|
||
| @property | ||
| def paginator(self): | ||
| """ | ||
| The paginator instance associated with the view, or `None`. | ||
| """ | ||
| if not hasattr(self, '_paginator'): | ||
| if self.pagination_class is None: | ||
| self._paginator = None | ||
| else: | ||
| self._paginator = self.pagination_class() | ||
|
|
||
| def paginate_queryset(self, queryset): | ||
| """ | ||
| Return a single page of results, or `None` if pagination is disabled. | ||
| """ | ||
| if self.paginator is None: | ||
| return None | ||
| return self.paginator.paginate_queryset(queryset, self.request, view=self) | ||
|
|
||
| def get_paginated_response(self, data): | ||
| """ | ||
| Return a paginated style `Response` object for the given output data. | ||
| """ | ||
| assert self.paginator is not None | ||
| return self.paginator.get_paginated_response(data) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.