Kaydet (Commit) 3c979907 authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Post class and rendering

üst 981ab284
...@@ -124,3 +124,6 @@ STATIC_URL = '/static/' ...@@ -124,3 +124,6 @@ STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/' LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/' LOGOUT_REDIRECT_URL = '/'
AUTH_USER_MODEL = 'social.SocialUser' AUTH_USER_MODEL = 'social.SocialUser'
# Extra Settings
MAX_POST_LENGTH = 144 * 2
...@@ -3,7 +3,7 @@ from django.contrib.auth import get_user_model ...@@ -3,7 +3,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.admin import UserAdmin
from social.forms import SocialUserCreationForm, SocialUserChangeForm from social.forms import SocialUserCreationForm, SocialUserChangeForm
from social.models import SocialUser from social.models import SocialUser, Post
class SocialUserAdmin(UserAdmin): class SocialUserAdmin(UserAdmin):
add_form = SocialUserCreationForm add_form = SocialUserCreationForm
...@@ -12,3 +12,4 @@ class SocialUserAdmin(UserAdmin): ...@@ -12,3 +12,4 @@ class SocialUserAdmin(UserAdmin):
list_display = ['email', 'username',] list_display = ['email', 'username',]
admin.site.register(SocialUser, SocialUserAdmin) admin.site.register(SocialUser, SocialUserAdmin)
admin.site.register(Post)
# Generated by Django 2.2.1 on 2019-05-04 15:09 # Generated by Django 2.2.1 on 2019-05-04 15:47
from django.conf import settings from django.conf import settings
import django.contrib.auth.models import django.contrib.auth.models
...@@ -47,7 +47,12 @@ class Migration(migrations.Migration): ...@@ -47,7 +47,12 @@ class Migration(migrations.Migration):
name='Post', name='Post',
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField(max_length=288)),
('pub_date', models.TimeField(auto_now=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
], ],
options={
'ordering': ['pub_date'],
},
), ),
] ]
...@@ -6,7 +6,17 @@ class SocialUser(AbstractUser): ...@@ -6,7 +6,17 @@ class SocialUser(AbstractUser):
pass pass
class Post(models.Model): class Post(models.Model):
class Meta:
ordering = ['pub_date']
author = models.ForeignKey( author = models.ForeignKey(
settings.AUTH_USER_MODEL, settings.AUTH_USER_MODEL,
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
text = models.TextField(max_length=settings.MAX_POST_LENGTH)
pub_date = models.TimeField(auto_now=True)
def __str__(self):
return f"{self.author}#{self.id}"
...@@ -3,11 +3,15 @@ ...@@ -3,11 +3,15 @@
{% block title %}Home{% endblock %} {% block title %}Home{% endblock %}
{% block content %} {% block content %}
{% if user.is_authenticated %} {% if user.is_authenticated %}
Hi {{ user.username }}! Hi {{ user.username }}!
<p><a href="{% url 'logout' %}">logout</a></p> <p><a href="{% url 'logout' %}">logout</a></p>
{% else %} {% load renderer %}
<p>You are not logged in</p> {% for item in feed %}
<a href="{% url 'login' %}">login</a> {% as_html item %}
{% endif %} {% endfor %}
{% else %}
<p>You are not logged in</p>
<a href="{% url 'login' %}">login</a>
{% endif %}
{% endblock %} {% endblock %}
<div>
<fieldset>
<legend>{{ item.author }}</legend>
<p> {{ item.text }} </p><br>
-- wrote this at {{ item.pub_date }};
</fieldset>
</div>
from django import template
from django.template.loader import render_to_string
register = template.Library()
@register.simple_tag
def as_html(item):
result = render_to_string(f"repr/{item.__class__.__name__.lower()}.html", {'item': item})
return result
from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic import CreateView from django.views.generic import CreateView, ListView
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from social.models import Post
from operator import or_
from functools import wraps, reduce
class Home(TemplateView): def ret_or_super(func):
template_name = 'home.html' def wrapper(*args, **kwargs):
try:
res = func(*args, **kwargs)
except:
self = args[0]
res = getattr(super(self.__class__, self), func.__name__)(*args, **kwargs)
return res
return wrapper
class ExtendedListView(ListView):
@ret_or_super
def get_queryset(self):
queryset = reduce(or_, map(lambda model: model._default_manager.all(), self.models))
return queryset
class Home(ExtendedListView):
models = Post,
template_name = 'home.html'
context_object_name = 'feed'
class Register(CreateView): class Register(CreateView):
form_class = UserCreationForm form_class = UserCreationForm
success_url = reverse_lazy('login') success_url = reverse_lazy('login')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment