Skip to content

Commit

Permalink
Added SearchFeild PostManager and Varios functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustamjadara committed Jun 3, 2018
1 parent 512bdbe commit 2ce939c
Show file tree
Hide file tree
Showing 29 changed files with 236 additions and 123 deletions.
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified posts/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file modified posts/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file modified posts/__pycache__/forms.cpython-36.pyc
Binary file not shown.
Binary file modified posts/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file modified posts/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file modified posts/__pycache__/views.cpython-36.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion posts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ class Meta:
fields = [
"title",
"content",
"image"
"image",
"draft",
"publish"
]
7 changes: 6 additions & 1 deletion posts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2018-05-26 07:21
# Generated by Django 1.9 on 2018-06-02 08:58
from __future__ import unicode_literals

from django.db import migrations, models
import posts.models


class Migration(migrations.Migration):
Expand All @@ -18,6 +19,10 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=120)),
('slug', models.SlugField(unique=True)),
('image', models.ImageField(blank=True, height_field='height_field', null=True, upload_to=posts.models.upload_location, width_field='width_field')),
('width_field', models.IntegerField(default=0)),
('height_field', models.IntegerField(default=0)),
('content', models.TextField()),
('updated', models.DateTimeField(auto_now=True)),
('timestamp', models.DateTimeField(auto_now_add=True)),
Expand Down
31 changes: 0 additions & 31 deletions posts/migrations/0002_auto_20180602_0924.py

This file was deleted.

27 changes: 27 additions & 0 deletions posts/migrations/0002_auto_20180602_1605.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2018-06-02 10:35
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('posts', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='post',
options={'ordering': ['-timestamp', '-updated']},
),
migrations.AddField(
model_name='post',
name='user',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
28 changes: 28 additions & 0 deletions posts/migrations/0003_auto_20180602_1633.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2018-06-02 11:03
from __future__ import unicode_literals

import datetime
from django.db import migrations, models
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
('posts', '0002_auto_20180602_1605'),
]

operations = [
migrations.AddField(
model_name='post',
name='draft',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='post',
name='publish',
field=models.DateTimeField(default=datetime.datetime(2018, 6, 2, 11, 3, 15, 124529, tzinfo=utc)),
preserve_default=False,
),
]
Binary file modified posts/migrations/__pycache__/0001_initial.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified posts/migrations/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
85 changes: 64 additions & 21 deletions posts/models.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models.signals import pre_save
from django.utils import timezone

#set upload location Accordingly
def upload_location(instance,filename):
print(instance)
return "%s/%s"%(instance.id,filename)

from django.utils.text import slugify
# Create your models here.
# MVC MODEL VIEW CONTROLLER
class PostManager(models.Manager):
def active(self,*args,**kwargs):
return super(PostManager,self).filter(draft=False).filter(publish__lte=timezone.now())

def upload_location(instance, filename):
#filebase, extension = filename.split(".")
#return "%s/%s.%s" %(instance.id, instance.id, extension)
return "%s/%s" %(instance.id, filename)

class Post(models.Model):
title = models.CharField(max_length=120)
image = models.ImageField(upload_to=upload_location,null=True,
blank=True,width_field="width_field",
height_field="height_field")
width_field = models.IntegerField(default=0)
height_field = models.IntegerField(default=0)
content = models.TextField()
updated = models.DateTimeField(auto_now=True, auto_now_add = False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add = True)

def __str__(self):
return self.title

def get_absolute_url(self):
return reverse("posts:detail",kwargs={"id":self.id})
user = models.ForeignKey(settings.AUTH_USER_MODEL,default=1)
title = models.CharField(max_length=120)
slug = models.SlugField(unique=True)
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
width_field="width_field",
height_field="height_field")
height_field = models.IntegerField(default=0)
width_field = models.IntegerField(default=0)
content = models.TextField()
draft = models.BooleanField(default=False)
publish = models.DateTimeField(auto_now=False, auto_now_add=False)
updated = models.DateTimeField(auto_now=True, auto_now_add=False)
timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
objects = PostManager()
def __unicode__(self):
return self.title

def __str__(self):
return self.title

def get_absolute_url(self):
return reverse("posts:detail", kwargs={"slug": self.slug})

class Meta:
ordering = ["-timestamp", "-updated"]



def create_slug(instance, new_slug=None):
slug = slugify(instance.title)
if new_slug is not None:
slug = new_slug
qs = Post.objects.filter(slug=slug).order_by("-id")
exists = qs.exists()
if exists:
new_slug = "%s-%s" %(slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug


def pre_save_post_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)



pre_save.connect(pre_save_post_receiver, sender=Post)
Empty file added posts/templatetags/__init__.py
Empty file.
Binary file not shown.
Binary file added posts/templatetags/__pycache__/urlify.cpython-36.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions posts/templatetags/urlify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from urllib.parse import quote_plus
from django import template

register = template.Library()

@register.filter
def urlify(value):
return quote_plus(value)
28 changes: 8 additions & 20 deletions posts/urls.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
from django.conf.urls import url
from django.contrib import admin

from .views import (
post_list,
post_create,
post_detail,
post_update,
post_delete
post_delete,
)

urlpatterns = [
url(r'^$', post_list, name='list'),
url(r'^$', post_list, name='list'),
url(r'^create/$', post_create),
url(r'^(?P<id>\d+)/$', post_detail,name='detail'),
url(r'^(?P<id>\d+)/edit/$', post_update,name='update'),
url(r'^(?P<id>\d+)/delete/$', post_delete),


]



# urlpatterns = [
# url(r'^$', "posts.views.post_list"),
# url(r'^create/$', "posts.views.post_create"),
# url(r'^detail/$', "posts.views.post_detail"),
# url(r'^update/$', "posts.views.post_update"),
# url(r'^delete/$', "posts.views.post_delete"),


# ]
url(r'^(?P<slug>[\w-]+)/$', post_detail, name='detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', post_update, name='update'),
url(r'^(?P<slug>[\w-]+)/delete/$', post_delete),
#url(r'^posts/$', "<appname>.views.<function_name>"),
]
Loading

0 comments on commit 2ce939c

Please sign in to comment.