diff --git a/redditclone/.gitignore b/redditclone/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/redditclone/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/redditclone/README.md b/redditclone/README.md new file mode 100644 index 0000000..96bd3af --- /dev/null +++ b/redditclone/README.md @@ -0,0 +1 @@ +# A Reddit clone built with Django 1.11 diff --git a/redditclone/db.sqlite3 b/redditclone/db.sqlite3 new file mode 100644 index 0000000..3146675 Binary files /dev/null and b/redditclone/db.sqlite3 differ diff --git a/redditclone/manage.py b/redditclone/manage.py new file mode 100755 index 0000000..b4dc30a --- /dev/null +++ b/redditclone/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "reddit.settings") + try: + from django.core.management import execute_from_command_line + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + 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?" + ) + raise + execute_from_command_line(sys.argv) diff --git a/redditclone/reddit/__init__.py b/redditclone/reddit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/redditclone/reddit/db.sqlite3 b/redditclone/reddit/db.sqlite3 new file mode 100644 index 0000000..12b61f0 Binary files /dev/null and b/redditclone/reddit/db.sqlite3 differ diff --git a/redditclone/reddit/settings.py b/redditclone/reddit/settings.py new file mode 100644 index 0000000..095c8b9 --- /dev/null +++ b/redditclone/reddit/settings.py @@ -0,0 +1,122 @@ +""" +Django settings for reddit project. + +Generated by 'django-admin startproject' using Django 1.11.2. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/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__))) +STATIC_DIR = os.path.join(BASE_DIR, 'static') +TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates') +MEDIA_DIR = os.path.join(BASE_DIR, 'media') +LOGIN_URL = '/' + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 't7zodh+vpak@@wgt4$kn&&*rt@2k$$dtvb!fn0049^v(l!mjtd' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.admindocs', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'redditapp', +] + +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 = 'reddit.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [TEMPLATE_DIR, ], + '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 = 'reddit.wsgi.application' + +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } +} + +# Password validation +# https://docs.djangoproject.com/en/1.11/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.11/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/1.11/howto/static-files/ +STATICFILES_DIRS = [STATIC_DIR, ] +STATIC_URL = '/static/' +MEDIA_ROOT = MEDIA_DIR +MEDIA_URL = '/media/' diff --git a/redditclone/reddit/urls.py b/redditclone/reddit/urls.py new file mode 100644 index 0000000..60f1fd7 --- /dev/null +++ b/redditclone/reddit/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import url, include +from django.contrib import admin + +urlpatterns = [ + url(r'^', include('redditapp.urls')), + url(r'^admin/', admin.site.urls), +] diff --git a/redditclone/reddit/wsgi.py b/redditclone/reddit/wsgi.py new file mode 100644 index 0000000..08115b1 --- /dev/null +++ b/redditclone/reddit/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for reddit 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.11/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "reddit.settings") + +application = get_wsgi_application() diff --git a/redditclone/redditapp/__init__.py b/redditclone/redditapp/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/redditclone/redditapp/admin.py b/redditclone/redditapp/admin.py new file mode 100644 index 0000000..d1ff898 --- /dev/null +++ b/redditclone/redditapp/admin.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from .models import * + +from django.contrib import admin + + +# Register your models here. +class CategoryAdmin(admin.ModelAdmin): + prepopulated_fields = {'slug': ('name',)} + + +admin.site.register(Category, CategoryAdmin) +admin.site.register(Post) diff --git a/redditclone/redditapp/apps.py b/redditclone/redditapp/apps.py new file mode 100644 index 0000000..b972a0a --- /dev/null +++ b/redditclone/redditapp/apps.py @@ -0,0 +1,8 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class RedditappConfig(AppConfig): + name = 'redditapp' diff --git a/redditclone/redditapp/migrations/0001_initial.py b/redditclone/redditapp/migrations/0001_initial.py new file mode 100644 index 0000000..1e9b467 --- /dev/null +++ b/redditclone/redditapp/migrations/0001_initial.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.1 on 2017-06-20 11:12 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ], + ), + migrations.CreateModel( + name='Post', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=500)), + ('url', models.URLField()), + ('views', models.IntegerField(default=0)), + ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='redditapp.Category')), + ], + ), + ] diff --git a/redditclone/redditapp/migrations/0002_category_slug.py b/redditclone/redditapp/migrations/0002_category_slug.py new file mode 100644 index 0000000..cbf126e --- /dev/null +++ b/redditclone/redditapp/migrations/0002_category_slug.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.1 on 2017-06-20 11:57 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('redditapp', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='category', + name='slug', + field=models.SlugField(blank=True), + ), + ] diff --git a/redditclone/redditapp/migrations/__init__.py b/redditclone/redditapp/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/redditclone/redditapp/models.py b/redditclone/redditapp/models.py new file mode 100644 index 0000000..0e48139 --- /dev/null +++ b/redditclone/redditapp/models.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from django.template.defaultfilters import slugify +from django.db import models + + +# Create your models here. + +class Category(models.Model): + name = models.CharField(max_length=255, unique=True) + slug = models.SlugField(blank=True) + + def save(self, *args, **kwargs): + self.slug = slugify(self.name) + super(Category, self).save(*args, **kwargs) + + def __str__(self): + return self.name + + +class Post(models.Model): + category = models.ForeignKey(Category) + title = models.CharField(max_length=500) + url = models.URLField() + views = models.IntegerField(default=0) + + def __str__(self): + return self.title diff --git a/redditclone/redditapp/tests.py b/redditclone/redditapp/tests.py new file mode 100644 index 0000000..5982e6b --- /dev/null +++ b/redditclone/redditapp/tests.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.test import TestCase + +# Create your tests here. diff --git a/redditclone/redditapp/urls.py b/redditclone/redditapp/urls.py new file mode 100644 index 0000000..8d570ff --- /dev/null +++ b/redditclone/redditapp/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls import url +from . import views + +urlpatterns = [ + url(r'^$', views.index, name='index'), + url(r'^category/(?P[\w\-]+)/$', + views.show_category, name='show_category'), +] diff --git a/redditclone/redditapp/views.py b/redditclone/redditapp/views.py new file mode 100644 index 0000000..54896a2 --- /dev/null +++ b/redditclone/redditapp/views.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals +from .models import * +from django.shortcuts import render + + +# Create your views here. + +def index(request): + category_list = Category.objects.all() + context_dict = {'title': "Reddit-JNTU", 'categories': category_list} + return render(request, 'reddit/index.html', context_dict) + + +def show_category(request, category_name_slug): + context_dict = {} + + try: + category_list = Category.objects.all() + category = Category.objects.get(slug=category_name_slug) + posts = Post.objects.filter(category=category) + context_dict['posts'] = posts + context_dict['category'] = category + context_dict['categories'] = category_list + except Category.DoesNotExist: + context_dict['category'] = None + context_dict['posts'] = None + + return render(request, 'reddit/category.html', context_dict) diff --git a/redditclone/static/css/style.css b/redditclone/static/css/style.css new file mode 100644 index 0000000..e69de29 diff --git a/redditclone/static/images/askreddit.png b/redditclone/static/images/askreddit.png new file mode 100644 index 0000000..3e2e6fe Binary files /dev/null and b/redditclone/static/images/askreddit.png differ diff --git a/redditclone/templates/reddit/base.html b/redditclone/templates/reddit/base.html new file mode 100644 index 0000000..ac514d9 --- /dev/null +++ b/redditclone/templates/reddit/base.html @@ -0,0 +1,45 @@ + +{% load staticfiles %} + + + + + + + + + + + + + + {% block body %} + {% endblock %} + + + + + + + \ No newline at end of file diff --git a/redditclone/templates/reddit/category.html b/redditclone/templates/reddit/category.html new file mode 100644 index 0000000..0e1512d --- /dev/null +++ b/redditclone/templates/reddit/category.html @@ -0,0 +1,15 @@ +{% extends "reddit/base.html" %} +{% block body %} +
+
+
+

{{ category.name }}

+
    +{% for post in posts %} +
  1. {{ post.title }}
  2. +{% endfor %} +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/redditclone/templates/reddit/index.html b/redditclone/templates/reddit/index.html new file mode 100644 index 0000000..44f058e --- /dev/null +++ b/redditclone/templates/reddit/index.html @@ -0,0 +1,4 @@ +{% extends "reddit/base.html" %} +{% block body %} + +{% endblock %} \ No newline at end of file