Skip to content

Instantly share code, notes, and snippets.

@codetricity
Last active August 20, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codetricity/e77ef110912fc5ad29e0e32e53b51c5e to your computer and use it in GitHub Desktop.
Save codetricity/e77ef110912fc5ad29e0e32e53b51c5e to your computer and use it in GitHub Desktop.
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib import auth
from django.http import HttpResponse
def signup(request):
if request.method == "POST":
# user wants to sign up
# check for blank email
if not request.POST['tos'] or (request.POST['tos'] != 'yes'):
return render(request, 'accounts/signup.html', {'error': 'Please agree to Terms of Service'})
elif not request.POST['email']:
return render(request, 'accounts/signup.html', {'error': 'Please fill in email'})
elif request.POST['password1'] == request.POST['password2']:
try:
user = User.objects.get(username=request.POST['username'])
return render(request, 'accounts/signup.html', {'error': 'username has been taken'})
except User.DoesNotExist:
user = User.objects.create_user(request.POST['username'], password=request.POST['password1'],
email=request.POST['email'])
auth.login(request, user)
return redirect('home')
else:
return render(request, 'accounts/signup.html', {'error': 'passwords do not match'})
else:
return render(request, 'accounts/signup.html')
def login(request):
if request.method == "POST":
user = auth.authenticate(
username=request.POST['username'],
password=request.POST['password'])
if user is not None:
auth.login(request, user)
return redirect('home')
else:
return render(request, 'accounts/login.html', {'error': 'username or password is incorrect'})
else:
return render(request, 'accounts/login.html')
def logout(request):
if request.method == "POST":
auth.logout(request)
return redirect('home')
from django.db import models
from django.contrib.auth.models import User
class ThetaPlugins(models.Model):
review_title = models.CharField(max_length=160, default='Title of Your Review')
plugin_name = models.CharField(max_length=80, default='Name of plug-in reviewed ')
url = models.URLField()
pub_date = models.DateField()
votes_total = models.IntegerField(default=1)
image = models.ImageField(upload_to='images/')
icon = models.ImageField(upload_to='images/')
body = models.TextField()
reviewer = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.review_title
def summary(self):
return self.body[:100]
def pub_date_pretty(self):
return self.pub_date.strftime('%b %e %Y')
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import ThetaPlugins
from django.utils import timezone
def home(request):
plugins = ThetaPlugins.objects.order_by('-pk')
return render(request, 'theta_plugins/home.html', {'plugins': plugins})
@login_required(login_url="/accounts/signup")
def create(request):
if request.method == 'POST':
if (
request.POST['review_title'] and
request.POST['body'] and
request.POST['url'] and
request.POST['plugin_name']):
plugin = ThetaPlugins()
plugin.review_title = request.POST['review_title']
plugin.plugin_name = request.POST['plugin_name']
plugin.body = request.POST['body']
if (request.POST['url'].startswith('http://') or
request.POST['url'].startswith('https://')):
plugin.url = request.POST['url']
else:
plugin.url = 'http://' + request.POST['url']
# handle condition when there is no icon uploaded
if not request.FILES.get('icon', False):
plugin.icon = 'theta_logo.png'
else:
plugin.icon = request.FILES['icon']
# handle condition when no image is uploaded
if not request.FILES.get('image', False):
plugin.image = 'invisible.png'
else:
plugin.image = request.FILES['image']
plugin.pub_date = timezone.datetime.now()
plugin.reviewer = request.user
plugin.save()
return redirect('/plugins/' + str(plugin.id))
else:
return render(
request,
'theta_plugins/create.html',
{'error': 'All fields are required'})
return render(request, 'theta_plugins/create.html')
def detail(request, plugin_id):
plugin = get_object_or_404(ThetaPlugins, pk=plugin_id)
return render(request, 'theta_plugins/detail.html', {'plugin': plugin})
@login_required(login_url="/accounts/signup")
def upvote(request, plugin_id):
if request.method == 'POST':
plugin = get_object_or_404(ThetaPlugins, pk=plugin_id)
plugin.votes_total += 1
plugin.save()
return redirect('/plugins/' + str(plugin_id))
@login_required(login_url="/accounts/signup")
def special(request):
return render(request, 'theta_plugins/special.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment