Skip to content

Commit

Permalink
v0.2 released & updates focused on Blogs timestamp & pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanpattnaik committed Jun 6, 2021
0 parents commit 95eef09
Show file tree
Hide file tree
Showing 41 changed files with 649 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.pyc
__pycache
migrations
venv
env
.vscode
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Anshuman Pattnaik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<img src="thumbnail/thumbnail.png"/>

## Technical Overview
The implementation of HackbotOne REST API is done using django-rest-framework and internally it also implements PostgreSQL to store the data in the database. Three different types of microservices have been designed to store and retrieve the data from the database ([api/v1/blogs/](blogs), [api/v1/portfolio/](portfolio), [api/v1/youtube/](youtube)).

### Installation
````````````````````````````````````````````````````````````````
git clone https://github.com/anshumanpattnaik/hackbotone-api.git
cd hackbotone-api
python3 -m venv env
source env/bin/activate
pip3 install -r requirements.txt
python3 manage.py runserver 8002
````````````````````````````````````````````````````````````````

### Redoc API Documentation
The redoc API docs can be accessible via http://127.0.0.1:8002/api/v1/redoc for all the services and there is required of authentication to access the endpoints.

<img src="thumbnail/redoc.png"/>

### Database Setup using pgAdmin
Make sure you have installed [PostgreSQL](https://www.postgresql.org/download/) & [pgAdmin](https://www.pgadmin.org/download/) software and then please follow the below steps to set up the database in your local machine.

### pgAdmin Database setup steps
````````````````````````````````````````````````````````````````
1. Create "hgit_admin" user with password "hgit_admin"
2. Create database "hgit" and assign the role to "hgit_admin"
````````````````````````````````````````````````````````````````

### Django Migrations
There are three different types of migration required for each table, so please follow the below steps for the migrations
````````````````````````````````````````````````````````````````
1. python3 manage.py makemigrations
2. python3 manage.py migrate
3. python3 manage.py makemigrations blogs
4. python3 manage.py migrate blogs
5. python3 manage.py makemigrations portfolio
6. python3 manage.py migrate portfolio
7. python3 manage.py makemigrations youtube
8. python3 manage.py migrate youtube
````````````````````````````````````````````````````````````````

### Import data using pgAdmin
There are three different CSV files that can be found under [databases](databases) folder in the repository and choose the respective file to import the data into the tables.

1. [blogs.csv](databases/blogs.csv)
2. [portfolio.csv](databases/portfolio.csv)
3. [youtube.csv](databases/youtube.csv)

### Note
If you are facing any issues or problems then feel free to raise an issue.

### Youtube Explantation
1. https://youtu.be/TRaejKHVhj8

### License
This project is licensed under the [MIT License](LICENSE)
Empty file added api/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions api/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for api project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')

application = get_asgi_application()
140 changes: 140 additions & 0 deletions api/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "2x$e%!k_u_0*gq0s4!_u(2(^lpy&gir0hg)q&5nurj0-sseuav"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

APPEND_SLASH = True

ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blogs',
'portfolio',
'youtube',
'drf_yasg',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
'django_filters'
]

MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'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 = 'api.urls'
SECURE_BROWSER_XSS_FILTER = True
CORS_ALLOW_ALL_ORIGINS = True

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 = 'api.wsgi.application'

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication'
],
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'SEARCH_PARAM': 'q',
'PAGE_SIZE': 12
}

# Database

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "hgit",
"USER": "hgit_admin",
"PASSWORD": "hgit_admin",
"HOST": "127.0.0.1",
"PORT": "5432",
}
}


# Password validation
# https://docs.djangoproject.com/en/3.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/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

DATE_INPUT_FORMATS = ['%d-%m-%Y']

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
29 changes: 29 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
from django.contrib import admin
from django.urls import path, include, re_path

from rest_framework import status, generics
from rest_framework.response import Response
from rest_framework.authtoken.views import obtain_auth_token
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
openapi.Info(
title="HackbotOne API",
default_version='v1',
description="HackbotOne website full api reference",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="admin@hackbotone.com"),
license=openapi.License(name="BSD License"),
),
public=True
)

urlpatterns = [
re_path('api/v1/redoc', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),
re_path('api/v1/blogs/', include('blogs.urls'), name='Blogs'),
re_path('api/v1/portfolio/', include('portfolio.urls'), name='Portfolio'),
re_path('api/v1/youtube/', include('youtube.urls'), name='YouTube')
]
16 changes: 16 additions & 0 deletions api/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for api 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/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')

application = get_wsgi_application()
Empty file added blogs/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions blogs/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions blogs/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BlogsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'blogs'
23 changes: 23 additions & 0 deletions blogs/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import models
from django.contrib.postgres.fields import ArrayField
from django.utils.timezone import now

class Blog(models.Model):
title = models.CharField(max_length=100, blank=True)
blog_id = models.CharField(max_length=100, blank=True)
author = models.CharField(max_length=100, blank=True)
published_date = models.DateTimeField(auto_now_add=True)
last_updated_date = models.DateTimeField(auto_now=True)
seo_thumbnail = models.CharField(max_length=500, blank=True)
small_thumbnail = models.CharField(max_length=500, blank=True)
thumbnail = models.CharField(max_length=500, blank=True)
keywords = ArrayField(models.CharField(max_length=100), blank=True)
highlights = models.TextField(blank=True)
description = models.TextField(blank=True)
visibility = models.BooleanField(default=False)
is_featured = models.BooleanField(default=False)
featured_board = models.BooleanField(default=False)
featured = models.TextField(blank=True)

class Meta:
ordering = ['-published_date']
11 changes: 11 additions & 0 deletions blogs/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class Pagination(PageNumberPagination):
def get_paginated_response(self, data):
return Response({
'count': self.page.paginator.count,
'total_pages': self.page.paginator.num_pages,
'results': data
})
10 changes: 10 additions & 0 deletions blogs/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rest_framework import serializers
from .models import Blog


class BlogSerializer(serializers.ModelSerializer):
class Meta:
model = Blog
fields = ['title', 'blog_id', 'author', 'published_date', 'last_updated_date', 'seo_thumbnail', 'small_thumbnail', 'thumbnail',
'keywords', 'highlights', 'description', 'visibility',
'is_featured', 'featured_board', 'featured']
3 changes: 3 additions & 0 deletions blogs/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.
10 changes: 10 additions & 0 deletions blogs/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
from django.urls import re_path, path
from .views import *

urlpatterns = [
re_path('list', FetchBlogsList.as_view()),
re_path('search', SearchBlogs.as_view()),
path('<str:blog_id>/', FindBlogById.as_view()),
path('featured-board', FeaturedBoard.as_view())
]

0 comments on commit 95eef09

Please sign in to comment.