Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
Source: https://github.com/github/gitignore/blob/master/Python.gitignore

# User generated
ENV/
.vscode
.idea
.DS_Store
.history


# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.pytest_cache/

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
.static_storage/
.media/
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

# js
node_modules/
.next/

# poetry
poetry.lock
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Python version
FROM python:3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Set work directory
WORKDIR /code

# Install dependencies
COPY requirements.txt /code/
RUN pip install -r requirements.txt

# Copy project
COPY . /code/
17 changes: 16 additions & 1 deletion accounts/serializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db.models import fields
from rest_framework import serializers
from .models import Account
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

class AddSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True)

Expand Down Expand Up @@ -31,4 +33,17 @@ class Meta:
'email','password','age','blood_type','phone_number','location',
'chronic_diseases','data','donate','group','image'
)


class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
def validate(self, attrs):
data = super().validate(attrs)
refresh = self.get_token(self.user)
data['refresh'] = str(refresh)
data['access'] = str(refresh.access_token)

# Add extra responses here
data['id'] = self.user.id
data['username'] = self.user.username

return data

6 changes: 3 additions & 3 deletions accounts/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from django.urls import path
from .views import AddListView,DetailAddView,ListView
from .views import AddListView,DetailAddView,ListView,CustomObtainAuthToken

urlpatterns = [
path('',AddListView.as_view(),name= 'add_data'),
path('signup',AddListView.as_view(),name= 'add_data'),
path('<str:username>',DetailAddView.as_view(),name = 'detail_data'),
# path('<int:pk>',DetailAddView.as_view(),name = 'detail_dataint'),
path("auth/", CustomObtainAuthToken.as_view()),
path('view',ListView.as_view(),name = 'list_data')
]
6 changes: 5 additions & 1 deletion accounts/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from django.shortcuts import render
from rest_framework import generics, permissions
from .serializer import AddSerializer
from .serializer import AddSerializer,MyTokenObtainPairSerializer
from .models import Account
from rest_framework import permissions
from django.shortcuts import get_object_or_404
from rest_framework_simplejwt.views import TokenObtainPairView
from .models import User
# Create your views here.

class CustomObtainAuthToken(TokenObtainPairView):
serializer_class = MyTokenObtainPairSerializer

class AddListView(generics.CreateAPIView):
serializer_class = AddSerializer
queryset = Account.objects.all()
Expand Down
145 changes: 145 additions & 0 deletions api_tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import fire
import requests

API_HOST = "https://django34.herokuapp.com/"
RESOURCE_URI = "cookie_stands"
USERNAME = "yahialabib"
PASSWORD = "123123"


class ApiTester:
"""CLI for testing API"""

def __init__(self, host=API_HOST):
self.host = host

def fetch_tokens(self):
"""Fetches access and refresh JWT tokens from api
Returns:
tuple: access,refresh
"""

token_url = f"{self.host}/api/token/"

response = requests.post(
token_url, json={"username": USERNAME, "password": PASSWORD}
)

data = response.json()

tokens = data["access"], data["refresh"]

return tokens

def get_all(self):
"""get list of all resources from api
Usage: python api_tester.py get_all
Returns: JSON
"""
access_token = self.fetch_tokens()[0]

url = f"{self.host}/api/all/"

headers = {
"Authorization": f"Bearer {access_token}",
}

response = requests.get(url, headers=headers)

return response.json()

def get_one(self, id):
"""get 1 resource by id from api
Usage:
python api_tester.py get_one 1
Returns: JSON
"""
access_token = self.fetch_tokens()[0]

url = f"{self.host}/api/{id}"

headers = {
"Authorization": f"Bearer {access_token}",
}

response = requests.get(url, headers=headers)

return response.json()

# TODO adjust parameter names to match API
def create(self, name, description=None, owner=None):
"""creates a resource in api
Usage:
python api_tester.py create /
--name=required --description=optional --owner=optional
Returns: JSON
"""

access_token = self.fetch_tokens()[0]

url = f"{self.host}/api/all"

headers = {
"Authorization": f"Bearer {access_token}",
}

data = {
"name": name,
"description": description,
"owner": owner,
}

response = requests.post(url, json=data, headers=headers)

return response.json()

def update(self, id, name=None, description=None, owner=None):
"""updates a resource in api
Usage:
python api_tester.py update 1 /
--name=optional --description=optional --owner=optional
Returns: JSON
"""

access_token = self.fetch_tokens()[0]

url = f"{self.host}/api/{id}/"

headers = {
"Authorization": f"Bearer {access_token}",
}

original = self.get_cookiestand(id)

data = {
"name": name or original["name"],
"description": description or original["description"],
"owner": owner or original["owner"],
}

response = requests.put(url, json=data, headers=headers)

return response.text

def delete(self, id):
"""deletes a resource in api
Usage:
python api_tester.py delete 1
Returns: Empty string if no error
"""

access_token = self.fetch_tokens()[0]

url = f"{self.host}/api/{id}/"

headers = {
"Authorization": f"Bearer {access_token}",
}

response = requests.delete(url, headers=headers)

return response.text


if __name__ == "__main__":
fire.Fire(ApiTester)
Binary file modified db.sqlite3
Binary file not shown.
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '4'

services:
web:
build: .
command: gunicorn cookie_project.wsgi:application --bind 0.0.0.0:8000 --workers 4
volumes:
- .:/code
ports:
- 8000:8000
7 changes: 7 additions & 0 deletions heroku.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build:
docker:
web: Dockerfile
release:
image: web
run:
web: gunicorn cookie_project.wsgi --workers 4
18 changes: 18 additions & 0 deletions hospital/migrations/0003_alter_customuser_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0 on 2021-12-30 19:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('hospital', '0002_customuser_image'),
]

operations = [
migrations.AlterField(
model_name='customuser',
name='image',
field=models.ImageField(upload_to='image'),
),
]
Loading