You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
pokercc/news/views.py

203 lines
7.2 KiB

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.template import loader
from django.urls import reverse
from django.core.paginator import Paginator
from django.conf import settings
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout, update_session_auth_hash
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm
from django.db.models import Q
from django.utils.html import strip_tags
from django.utils.text import slugify
from .models import Post, Comment, PostState, Tag
from .forms import PostForm, CustomUserCreationForm, SigninForm
from django.utils import timezone
import logging
import uuid
logger = logging.getLogger(__name__)
#Users
def register(request):
if request.method == 'POST':
f = CustomUserCreationForm(request.POST)
if f.is_valid():
f.save()
messages.success(request, 'Account created successfully')
return HttpResponseRedirect(reverse('news:index'))
else:
f = CustomUserCreationForm()
return render(request, 'news/user/register.html', {'form': f})
def signin(request):
if request.method == 'POST':
f = SigninForm(request.POST)
if f.is_valid():
username = f.cleaned_data['username']
password = f.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse('news:index'))
else:
messages.success(request, 'Sign-in failed')
return HttpResponseRedirect(reverse('news:signin'))
else:
f = SigninForm()
return render(request, 'news/user/signin.html', {'form': f})
@login_required
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse('news:index'))
@login_required
def account(request):
return render(request, 'news/user/account.html', {})
@login_required
def password_change(request):
if request.method == 'POST':
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user) # Important!
messages.success(request, 'Your password was successfully updated!')
return render(request, 'news/user/account.html', {})
else:
messages.error(request, 'Please correct the error below.')
else:
form = PasswordChangeForm(request.user)
return render(request, 'news/user/change_password.html', {'form': form})
#Post
def index(request):
filter1 = Q(state=1)
filter2 = Q(date__lte=timezone.now())
latest_post_list = Post.objects.filter(filter1 & filter2).order_by('-date')
paginator = Paginator(latest_post_list, 25)
page = request.GET.get('page')
posts = paginator.get_page(page)
context = { 'latest_post_list' : posts }
return render(request, 'news/index.html', context)
def post(request, post_id, post_slug):
post = get_object_or_404(Post, pk=post_id)
return render(request, 'news/post.html', {'post': post, 'comments': post.flat_comments(request.user) })
@login_required
def submission(request):
logger.debug('>>> Create post')
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
post = Post.objects.create(author=request.user,date=timezone.now())
post.title = form.cleaned_data['title']
post.body = form.cleaned_data['body']
post.url = form.cleaned_data['url']
post.style = form.cleaned_data['style']
tags = parsed_tags(form.cleaned_data['tags'])
post.tag_set.add(*tags)
post.slug = slugify(post.title)
file = request.FILES.get('image', None)
if file is not None:
filename = str(uuid.uuid4())
handle_uploaded_file(filename, file)
post.image_url = filename
if request.user.is_staff:
post.state = PostState.PUBLISHED.value
pub_date = form.cleaned_data['pub_date']
if pub_date is not None:
post.date = pub_date
else:
post.state = PostState.USER_SUBMITTED.value
post.save()
messages.success(request, 'GG! Thanks for posting!<br/>Instagram text:<br/> %s <br/>.<br/>.<br/>.<br/>.<br/>.<br/>#poker #pokernews #pokerlife #wsop #pokernight #chipporn #pokerlive #pokergrind #pokerrumble' % form.cleaned_data['title'])
#return render(request, 'news/index.html', context)
return HttpResponseRedirect(reverse('news:index'))
#return HttpResponseRedirect(reverse('news:submitted'))
# if a GET (or any other method) we'll create a blank form
else:
form = PostForm()
return render(request, 'news/submission.html', {'form': form})
def parsed_tags(tag_strings):
tags = []
separated_tags_strings = tag_strings.split(",")
for tag_string in separated_tags_strings:
tag, created = Tag.objects.get_or_create(name=tag_string.strip())
tag.save()
tags.append(tag)
return tags
def handle_uploaded_file(filename, f):
with open(settings.MEDIA_ROOT + filename, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
def submitted(request):
return render(request, 'news/submitted.html', {})
@login_required
def comment(request, post_id):
logger.debug('$$$ comment on $post_id, parent comment = $comment_id')
return comment_with_parent(request, post_id, None)
@login_required
def comment_with_parent(request, post_id, comment_id):
logger.debug('>>> comment_with_parent on $post_id, parent comment = $comment_id!')
#comment = Comment.objects.create(author=request.user,date=timezone.now())
comment = Comment(author=request.user,date=timezone.now())
comment.post = get_object_or_404(Post, pk=post_id)
if comment_id is not None:
comment.parent_comment = get_object_or_404(Comment, pk=comment_id)
comment.body = strip_tags(request.POST['body']) # removes HTML tags
comment.save()
comment.voters.add(request.user)
comment.save()
return render(request, 'news/post.html', {'post': comment.post, 'comments': comment.post.flat_comments(request.user) })
def delete_comment(request, comment_id):
comment = get_object_or_404(Comment, pk=comment_id)
post = comment.post
if comment.comment_set.all().count() > 0:
comment.body = "[deleted]"
comment.deleted = True
comment.save()
else:
comment.delete()
return render(request, 'news/post.html', {'post': post, 'comments': post.flat_comments(request.user) })
def upvote(request, comment_id):
comment = get_object_or_404(Comment, pk=comment_id)
comment.voters.add(request.user)
comment.save()
post = comment.post
return HttpResponseRedirect(reverse('news:post', args=(post.id,)))
def empty_view(request):
return HttpResponse('')