Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from pyprism/django
Browse files Browse the repository at this point in the history
Django
  • Loading branch information
pyprism committed Feb 26, 2016
2 parents dd7e5ce + e15b4a3 commit 96d1cd4
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 66 deletions.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"access_token_secret": "",
"db_name": "",
"db_user": "",
"db_pass": ""
"db_pass": "",
"secret_key": ""
}
14 changes: 0 additions & 14 deletions db/models.py

This file was deleted.

11 changes: 11 additions & 0 deletions etc/init/hiren_news.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
description "uWSGI server instance configured to serve Hiren-news"

start on runlevel [2345]
stop on runlevel [!2345]

setuid prism
setgid www-data

env PATH=/home/prism/Hiren-news/.env/bin
chdir /home/prism/Hiren-news/
exec uwsgi --ini hiren.ini
9 changes: 9 additions & 0 deletions hiren.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[uwsgi]
module=hiren.wsgi
master=True
process = 5
pidfile=/tmp/news.pid
socket= /tmp/news.sock
vacuum=True
max-requests=5000
daemonize=/home/prism/Hiren-news/hiren.log
30 changes: 0 additions & 30 deletions hiren.py

This file was deleted.

Empty file added hiren/__init__.py
Empty file.
146 changes: 146 additions & 0 deletions hiren/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""
Django settings for hiren project.
Generated by 'django-admin startproject' using Django 1.9.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""

import os
import json

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

try:
with open(BASE_DIR + '/' + 'config.local.json') as f:
JSON_DATA = json.load(f)
except FileNotFoundError:
with open(BASE_DIR + '/' + 'config.json') as f:
JSON_DATA = json.load(f)

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', JSON_DATA['secret_key'])

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG', False)

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'news'
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

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


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

if 'TRAVIS' in os.environ:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'travisci',
'USER': 'postgres',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}
else:
DATABASES = {
'default': {
'NAME': JSON_DATA['db_name'],
'ENGINE': 'django.db.backends.mysql',
'USER': JSON_DATA['db_user'],
'PASSWORD': JSON_DATA['db_pass'],
'HOST': 'localhost',
'PORT': '',
}
}



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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Dhaka'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
23 changes: 23 additions & 0 deletions hiren/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""hiren URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.9/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from news.views import index

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', index),
]
16 changes: 16 additions & 0 deletions hiren/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for hiren 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/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hiren.settings")

application = get_wsgi_application()
10 changes: 10 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hiren.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
Empty file added news/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions news/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib import admin
from news.models import Bunny
# Register your models here.


class AuthorAdmin(admin.ModelAdmin):
list_display = ['id', 'comment_url', 'time']
ordering = ['id']

admin.site.register(Bunny, AuthorAdmin)
5 changes: 5 additions & 0 deletions news/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class NewsConfig(AppConfig):
name = 'news'
18 changes: 10 additions & 8 deletions hn/rss.py → news/hn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import feedparser
from db.models import Content
from hiren import db
import django
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hiren.settings")
django.setup()

from news.models import Bunny


def rss():
Expand All @@ -11,11 +16,8 @@ def rss():
feed = []
bunny = feedparser.parse('https://news.ycombinator.com/rss')
for i in bunny.entries:
duplicate = Content.query.filter_by(comments_url=i.comments).first()
if duplicate is None:
duplicate = Bunny.objects.filter(comment_url=i.comments)
if duplicate.exists() is False:
feed.append(i.title_detail.value + ' : ' + i.link + " " + "Comments: " + i.comments)
hiren = Content(i.comments)
db.session.add(hiren)
db.session.commit()
Bunny(comment_url=i.comments).save()
return feed

24 changes: 24 additions & 0 deletions news/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-02-26 04:08
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Bunny',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('comment_url', models.URLField(unique=True)),
('time', models.DateTimeField(auto_now_add=True)),
],
),
]
Empty file added news/migrations/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions news/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models


class Bunny(models.Model):
comment_url = models.URLField(unique=True)
time = models.DateTimeField(auto_now_add=True)

def __str__(self):
return 'Comment={0}, Date={1}'.format(self.comment_url, self.time)
3 changes: 3 additions & 0 deletions news/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.
8 changes: 7 additions & 1 deletion twitter/api.py → news/twitter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from hiren import JSON_DATA
import django
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hiren.settings")
django.setup()

from hiren.settings import JSON_DATA
from hn import rss
import tweepy

Expand Down
6 changes: 6 additions & 0 deletions news/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.http import HttpResponse
from news.models import Bunny


def index(request):
return HttpResponse('Number of Bunny(s): ' + str(Bunny.objects.all().count()))
Loading

0 comments on commit 96d1cd4

Please sign in to comment.