Commit da469dd5 authored by Silje Odland's avatar Silje Odland
Browse files

adds user types, posts, login and logout functionality

parent 316ec013
from django.shortcuts import render
from django.shortcuts import render, redirect
from .forms import PostForm, ChallengeForm
from django.utils import timezone
from apps.users.models import PrivatePerson, Company
# Create your views here.
#def ChallengeUpload(request):
def postpage(request):
if PrivatePerson.objects.filter(user=request.user).first():
if request.method == 'POST':
form = PostForm(request.POST, request.FILES )
form.instance.post_owner = request.user
form.instance.time = timezone.now()
if form.is_valid():
form.save()
return redirect('startpage')
else:
form = PostForm()
return render(request, 'postpage.html', {
'form': form
})
else:
if request.method == 'POST':
form = ChallengeForm(request.POST, request.FILES )
form.instance.post_owner = request.user
form.instance.time = timezone.now()
if form.is_valid():
form.save()
return redirect('challenges')
else:
form = ChallengeForm()
return render(request, 'postpage.html', {
'form': form
})
\ No newline at end of file
from django.shortcuts import render
from ..feed.models import Post
from django.contrib.auth.models import User
from apps.feed.models import Challange
from apps.users.models import PrivatePerson
# Create your views here.
def index(request):
......@@ -12,4 +14,14 @@ def startpage(request):
def myprofile(request,pk):
knitter = User.objects.filter(pk=pk).first()
return render(request, "myprofile.html",{'knitter':knitter})
\ No newline at end of file
user = User.objects.filter(pk = pk).first()
if PrivatePerson.objects.filter(user = user).first():
posts = Post.objects.filter(post_owner= user)
else:
posts = Challange.objects.filter(post_owner= user)
return render(request, "myprofile.html",{'knitter':knitter, 'posts': posts})
def challenges(request):
challengeposts = Challange.objects.all().order_by('time')
return render(request, "challenges.html", {'challengeposts': challengeposts})
from django.contrib import admin
# Register your models here.
from .models import PrivatePerson, Company
admin.site.register(PrivatePerson)
admin.site.register(Company)
\ No newline at end of file
# Generated by Django 3.0.3 on 2020-02-27 10:46
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('users', '0003_delete_knitter'),
]
operations = [
migrations.CreateModel(
name='PrivatePerson',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('profilePicture', models.ImageField(blank=True, null=True, upload_to='')),
('about', models.TextField(blank=True, max_length=400, null=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Company',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('profilePicture', models.ImageField(blank=True, null=True, upload_to='')),
('about', models.TextField(blank=True, max_length=400, null=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class PrivatePerson(models.Model):
user = models.ForeignKey(User, on_delete= models.CASCADE, null= False)
profilePicture = models.ImageField(null=True, blank=True)
about = models.TextField(null=True, blank=True, max_length=400)
class Company(models.Model):
user = models.ForeignKey(User, on_delete= models.CASCADE, null=False)
profilePicture = models.ImageField(null=True, blank=True)
about = models.TextField(null=True, blank=True, max_length=400)
\ No newline at end of file
from django.shortcuts import render, redirect
from django.contrib import messages
from .forms import UserRegisterForm
from django.contrib.auth import logout as django_logout
from .models import PrivatePerson, Company
def logout(request):
django_logout(request)
return redirect('index')
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
is_company = request.POST.get('company')
if form.is_valid():
username = form.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
form.save()
return redirect('startpage/')
user = form.save()
if(is_company):
company = Company.objects.create(user = user)
else:
privateperson = PrivatePerson.objects.create(user = user)
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'register.html', {'form': form})
\ No newline at end of file
return render(request, 'register.html', {'form': form})
def redirect_to_index(request):
return redirect('startpage')
......@@ -19,17 +19,22 @@ from django.urls import path, include
from django.urls import path
from django.conf.urls.static import static
from . import settings
from apps.staticpages.views import index
from apps.staticpages.views import startpage
from apps.staticpages.views import myprofile
from apps.staticpages.views import index, startpage, myprofile
from apps.users import views as user_views
from apps.staticpages.views import challenges
from apps.feed.views import postpage
urlpatterns = [
path('admin/', admin.site.urls),
path('register', user_views.register, name='register'),
path('', index),
path('startpage/', startpage),
path('myprofile/<int:pk>/', myprofile),
path('', index, name='index'),
path('startpage/', startpage, name='startpage'),
path('myprofile/<int:pk>/', myprofile, name='myprofile'),
path('accounts/', include('django.contrib.auth.urls'), name="login"),
path('accounts/profile/', user_views.redirect_to_index, name="profile"),
path('logout/', user_views.logout, name="logout"),
path('challenges/', challenges, name="challenges"),
path('postpage/', postpage, name= "postpage"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Supports Markdown
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