├── db.sqlite3 ├── djstack ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py ├── manage.py └── stackapi ├── __init__.py ├── admin.py ├── apps.py ├── migrations ├── 0001_initial.py └── __init__.py ├── models.py ├── serializer.py ├── tests.py ├── urls.py └── views.py /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iampawan/DjangoDjStack/56158191dc5247b4945414250decc62897aee702/db.sqlite3 -------------------------------------------------------------------------------- /djstack/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iampawan/DjangoDjStack/56158191dc5247b4945414250decc62897aee702/djstack/__init__.py -------------------------------------------------------------------------------- /djstack/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for djstack project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'ffu)0yf)-5idq==&0y(%-=3z)^el4-wqeu3958#3es+dku2^8c' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'stackapi', 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'djstack.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'djstack.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/2.2/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'Asia/Kolkata' 111 | 112 | USE_I18N = True 113 | 114 | USE_L10N = True 115 | 116 | USE_TZ = True 117 | 118 | 119 | # Static files (CSS, JavaScript, Images) 120 | # https://docs.djangoproject.com/en/2.2/howto/static-files/ 121 | 122 | STATIC_URL = '/static/' 123 | -------------------------------------------------------------------------------- /djstack/urls.py: -------------------------------------------------------------------------------- 1 | """djstack URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path, include 18 | from stackapi import urls 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('stack/', include(urls)), 23 | ] 24 | -------------------------------------------------------------------------------- /djstack/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for djstack project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djstack.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djstack.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /stackapi/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iampawan/DjangoDjStack/56158191dc5247b4945414250decc62897aee702/stackapi/__init__.py -------------------------------------------------------------------------------- /stackapi/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | 5 | from .models import Question 6 | 7 | admin.site.register(Question) 8 | -------------------------------------------------------------------------------- /stackapi/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class StackapiConfig(AppConfig): 5 | name = 'stackapi' 6 | -------------------------------------------------------------------------------- /stackapi/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.2 on 2019-04-21 14:45 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Question', 16 | fields=[ 17 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('question', models.CharField(max_length=300)), 19 | ('vote_count', models.IntegerField(default=0)), 20 | ('views', models.CharField(max_length=50)), 21 | ('tags', models.CharField(max_length=250)), 22 | ], 23 | ), 24 | ] 25 | -------------------------------------------------------------------------------- /stackapi/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iampawan/DjangoDjStack/56158191dc5247b4945414250decc62897aee702/stackapi/migrations/__init__.py -------------------------------------------------------------------------------- /stackapi/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Question(models.Model): 5 | question = models.CharField(max_length=300) 6 | vote_count = models.IntegerField(default=0) 7 | views = models.CharField(max_length=50) 8 | tags = models.CharField(max_length=250) 9 | 10 | def __str__(self): 11 | return self.question 12 | -------------------------------------------------------------------------------- /stackapi/serializer.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from .models import Question 3 | 4 | 5 | class QuestionSerializer(serializers.ModelSerializer): 6 | class Meta: 7 | model = Question 8 | fields = ('__all__') 9 | -------------------------------------------------------------------------------- /stackapi/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /stackapi/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path, include 2 | from .views import index, QuestionAPI, latest 3 | from rest_framework import routers 4 | 5 | 6 | router = routers.DefaultRouter() 7 | router.register("questions", QuestionAPI) 8 | 9 | urlpatterns = [ 10 | path('', index, name="index"), 11 | path('', include(router.urls)), 12 | path('latest', latest, name="latest"), 13 | ] 14 | -------------------------------------------------------------------------------- /stackapi/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from django.http import HttpResponse 3 | from rest_framework import viewsets 4 | from .models import Question 5 | from .serializer import QuestionSerializer 6 | from bs4 import BeautifulSoup 7 | 8 | import requests 9 | import json 10 | 11 | # Create your views here. 12 | 13 | 14 | def index(request): 15 | return HttpResponse("Success") 16 | 17 | 18 | class QuestionAPI(viewsets.ModelViewSet): 19 | queryset = Question.objects.all() 20 | serializer_class = QuestionSerializer 21 | 22 | 23 | def latest(request): 24 | try: 25 | res = requests.get("https://stackoverflow.com/questions") 26 | 27 | soup = BeautifulSoup(res.text, "html.parser") 28 | 29 | questions = soup.select(".question-summary") 30 | for que in questions: 31 | q = que.select_one('.question-hyperlink').getText() 32 | vote_count = que.select_one('.vote-count-post').getText() 33 | views = que.select_one('.views').attrs['title'] 34 | tags = [i.getText() for i in (que.select('.post-tag'))] 35 | 36 | question = Question() 37 | question.question = q 38 | question.vote_count = vote_count 39 | question.views = views 40 | question.tags = tags 41 | 42 | question.save() 43 | return HttpResponse("Latest Data Fetched from Stack Overflow") 44 | except e as Exception: 45 | return HttpResponse(f"Failed {e}") 46 | --------------------------------------------------------------------------------