Commit 127713ea authored by Hildegunn Elin Olaussen Nedregård's avatar Hildegunn Elin Olaussen Nedregård
Browse files

Added follow button on posts

parent fb00b7d7
......@@ -12,5 +12,12 @@
</value>
</option>
</inspection_tool>
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="rodTraa.apps.users.views.post.post_owner" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
......@@ -10,6 +10,7 @@
<img style="max-width:100%; max-height: 400px"; src="{{ post.image.url }}">
</div>
<div style="display: block; margin-left: auto; margin-right: auto; width: 60% ">{{ post.text }}</div>
<p>{% include 'follow_form.html' with username=user.username is_following=is_following %}</p>
</div>
{% endfor %}
{% endblock %}
\ No newline at end of file
......@@ -7,7 +7,6 @@ from django.contrib.auth.models import User
class Post(models.Model):
print(models.Model)
text = models.TextField(null=True, blank=True,max_length=4000)
image = models.ImageField(null=False)
time = models.DateTimeField(null=True)
......
......@@ -8,20 +8,22 @@ from apps.users.models import PrivatePerson
# Create your views here.
def index(request):
print("index")
return render(request, "index.html")
def startpage(request):
print("startpage")
posts = Post.objects.all()
return render(request, "startpage.html", {'posts':posts})
def myprofile(request,pk):
knitter = User.objects.filter(pk=pk).first()
user = User.objects.filter(pk = pk).first()
if PrivatePerson.objects.filter(user = user).first():
posts = Post.objects.filter(post_owner= user)
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})
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')
......
from django.contrib import admin
# Register your models here.
from .models import PrivatePerson, Company
from .models import PrivatePerson, Company, Profile
admin.site.register(PrivatePerson)
admin.site.register(Company)
\ No newline at end of file
admin.site.register(Company)
admin.site.register(Profile)
\ No newline at end of file
......@@ -3,3 +3,9 @@ from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'apps.users'
class ProfilesConfig(AppConfig):
name = 'profiles'
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import post_save
# Create your models here.
# User= settings.AUTH_USER_MODEL
from apps.feed.models import Post
from django.http import request
class PrivatePerson(models.Model):
......@@ -14,3 +19,39 @@ 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)
class ProfileManager(models.Manager):
def toggle_follow(request_user, username_to_toggle):
print(request_user)
#profile_ = username_to_toggle
user = request_user
profile_ = Profile.objects.get(user=username_to_toggle)
#profile_ = Profile.objects.create(user=username_to_toggle)
is_following = False
if user in profile_.followers.all():
profile_.followers.remove(user)
else:
profile_.followers.add(user)
is_following = True
return profile_, is_following
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
followers = models.ForeignKey(User, on_delete=models.CASCADE, related_name='is_following', blank=True)
objects = ProfileManager()
class Meta:
db_table = 'user_profile'
def post_save_user_receiver(sender, instance, created, *args, **kwargs):
if created:
profile, is_created = Profile.objects.get_or_create(user=instance)
post_save.connect(post_save_user_receiver, sender=User)
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
from django.contrib.auth import logout as django_logout, get_user_model
from .models import PrivatePerson, Company, Profile, ProfileManager
from django.shortcuts import render, get_object_or_404, redirect
from ..feed.models import Post
User = get_user_model()
def logout(request):
django_logout(request)
......@@ -18,11 +23,11 @@ def register(request):
if form.is_valid():
username = form.cleaned_data.get('username')
messages.success(request, f'Account created for {username}!')
user = form.save()
usero = form.save()
if(is_company):
company = Company.objects.create(user=user)
company = Company.objects.create(user=usero)
else:
privateperson = PrivatePerson.objects.create(user=user)
privateperson = PrivatePerson.objects.create(user=usero)
return redirect('login')
else:
form = UserRegisterForm()
......@@ -33,3 +38,9 @@ def redirect_to_index(request):
return redirect('startpage')
def follow(request):
print('postowner')
users = User.objects.all()
toggle = users[2]
profile_, is_following = ProfileManager.toggle_follow(request.user, toggle)
......@@ -13,6 +13,7 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('startpage/', include('startpage.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.urls import path, include
......@@ -21,8 +22,11 @@ from django.conf.urls.static import static
from . import settings
from apps.staticpages.views import index, startpage, myprofile
from apps.users import views as user_views
#from apps.users.views import follow
from apps.staticpages.views import challenges
from apps.feed.views import postpage
#from django.conf.urls.defaults import patterns, include, url
#from apps.users.views import ProfileFollowToggle
urlpatterns = [
path('admin/', admin.site.urls),
......@@ -32,9 +36,9 @@ urlpatterns = [
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('logout/', user_views.follow, name="follow"),
#path('startpage/', user_views.follow, name='follow'),
path('challenges/', challenges, name="challenges"),
path('postpage/', postpage, name= "postpage"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
path('postpage/', postpage, name="postpage"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
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