mirror of
https://github.com/Rushilwiz/NewViewsNews.git
synced 2025-04-11 07:30:17 -04:00
added more fields to article so that it's more of an article object
This commit is contained in:
parent
2129f28909
commit
2d92621026
BIN
media/default-header.jpg
Normal file
BIN
media/default-header.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 84 KiB |
Before Width: | Height: | Size: 7.6 KiB After Width: | Height: | Size: 7.6 KiB |
BIN
media/profile_pics/uhuh.jpg
Normal file
BIN
media/profile_pics/uhuh.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
18
news/migrations/0002_article_header.py
Normal file
18
news/migrations/0002_article_header.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.1 on 2020-08-15 14:49
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('news', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='article',
|
||||
name='header',
|
||||
field=models.ImageField(default='default.jpg', upload_to='article-headers'),
|
||||
),
|
||||
]
|
23
news/migrations/0003_auto_20200815_1130.py
Normal file
23
news/migrations/0003_auto_20200815_1130.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 3.1 on 2020-08-15 15:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('news', '0002_article_header'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='article',
|
||||
name='header_caption',
|
||||
field=models.TextField(default=''),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='article',
|
||||
name='header',
|
||||
field=models.ImageField(default='default-header.jpg', upload_to='article-headers'),
|
||||
),
|
||||
]
|
19
news/migrations/0004_article_headline.py
Normal file
19
news/migrations/0004_article_headline.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 3.1 on 2020-08-15 15:33
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('news', '0003_auto_20200815_1130'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='article',
|
||||
name='headline',
|
||||
field=models.TextField(default='Headline'),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
|
@ -4,16 +4,31 @@ from django.utils import timezone
|
|||
from django.contrib.auth.models import User
|
||||
from django.urls import reverse
|
||||
|
||||
from PIL import Image
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class Article(models.Model):
|
||||
headline = models.TextField()
|
||||
content = models.TextField()
|
||||
date_published = models.DateTimeField(auto_now_add=True)
|
||||
author = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
likes = models.IntegerField(default=0)
|
||||
header = models.ImageField(default='default-header.jpg', upload_to='article-headers')
|
||||
header_caption = models.TextField(default="")
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.author}'s article on {self.date_published}"
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse('article-detail', kwargs={'pk': self.pk})
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
img = Image.open(self.header.path)
|
||||
|
||||
if img.width > 1280 or img.height > 768:
|
||||
output_size = (1280, 768)
|
||||
img.thumbnail(output_size)
|
||||
img.save(self.header.path)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{% extends 'news/base.html' %}
|
||||
{% block content %}
|
||||
<h1>home</h1>
|
||||
<h1>This is the NVN about page.</h1>
|
||||
{% endblock content %}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
<div class="article-metadata">
|
||||
<a class="mr-2" href="{% url 'user-articles' object.author.username %}">{{ object.author.get_full_name }}</a>
|
||||
<small class="text-muted">@{{ object.author }} · {{ object.date_published }}</small>
|
||||
|
||||
{% if object.author == user %}
|
||||
<div>
|
||||
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{% url 'article-update' object.id %}">Edit article</a>
|
||||
|
@ -13,6 +14,8 @@
|
|||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<img class="" src="{{ object.header.url }}">
|
||||
<p class="article-content font-weight-light">{{ article.content }}</p>
|
||||
</div>
|
||||
</article>
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
<a class="nav-item nav-link" href="{% url 'logout' %}">Logout</a>
|
||||
{% else %}
|
||||
<a class="nav-item nav-link" href="{% url 'login' %}">Login</a>
|
||||
<a class="nav-item nav-link" href="{% url 'register' %}">Register</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
<div class="media-body">
|
||||
<div class="article-metadata">
|
||||
<a class="mr-2" href="{% url 'user-articles' article.author.username %}">{{ article.author.get_full_name }}</a>
|
||||
<small class="text-muted">@{{ article.author }} · {{ article.date_published }}</small>
|
||||
<a href="{% url 'user-articles' article.author.username %}"><small class="text-muted">@{{ article.author }} · {{ article.date_published }}</small></a>
|
||||
</div>
|
||||
<p class="article-content font-weight-light"><a class="nounderline" href="{% url 'article-detail' article.id %}">{{ article.content }}</a></p>
|
||||
<p class="article-content font-weight-light"><a class="nounderline" href="{% url 'article-detail' article.id %}">{{ article.headline }}</a></p>
|
||||
</div>
|
||||
</article>
|
||||
{% endfor %}
|
||||
|
|
|
@ -16,11 +16,12 @@ from users.models import Profile
|
|||
# Create your views here.
|
||||
|
||||
def about (request):
|
||||
createProfileIfNotExist(request)
|
||||
|
||||
return render (request, 'news/about.html')
|
||||
|
||||
def home (request):
|
||||
if request.user.is_authenticated and Profile.objects.filter(user=request.user).count() < 1:
|
||||
Profile.objects.create(user=request.user).save()
|
||||
createProfileIfNotExist(request)
|
||||
|
||||
context = {
|
||||
'articles': Article.objects.all()
|
||||
|
@ -29,6 +30,7 @@ def home (request):
|
|||
return render (request, 'news/home.html', context)
|
||||
|
||||
class ArticleListView(ListView):
|
||||
|
||||
model = Article
|
||||
template_name = "news/home.html"
|
||||
context_object_name='articles'
|
||||
|
@ -50,7 +52,7 @@ class ArticleDetailView(DetailView):
|
|||
|
||||
class ArticleCreateView(LoginRequiredMixin, CreateView):
|
||||
model = Article
|
||||
fields=['content']
|
||||
fields=['headline','header','header_caption','content']
|
||||
|
||||
def form_valid(self, form):
|
||||
form.instance.author = self.request.user
|
||||
|
@ -58,7 +60,7 @@ class ArticleCreateView(LoginRequiredMixin, CreateView):
|
|||
|
||||
class ArticleUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
|
||||
model = Article
|
||||
fields=['content']
|
||||
fields=['headline','header','header_caption','content']
|
||||
|
||||
def form_valid(self, form):
|
||||
form.instance.author = self.request.user
|
||||
|
@ -75,3 +77,7 @@ class ArticleDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
|
|||
def test_func(self):
|
||||
article = self.get_object()
|
||||
return self.request.user == article.author
|
||||
|
||||
def createProfileIfNotExist (request):
|
||||
if request.user.is_authenticated and Profile.objects.filter(user=request.user).count() < 1:
|
||||
Profile.objects.create(user=request.user).save()
|
||||
|
|
|
@ -17,7 +17,7 @@ class UserUpdateForm(forms.ModelForm):
|
|||
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ['username', 'email']
|
||||
fields = ['username', 'email', 'first_name', 'last_name']
|
||||
|
||||
|
||||
class ProfileUpdateForm(forms.ModelForm):
|
||||
|
|
18
users/migrations/0004_auto_20200815_1130.py
Normal file
18
users/migrations/0004_auto_20200815_1130.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 3.1 on 2020-08-15 15:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('users', '0003_auto_20200815_0302'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='profile',
|
||||
name='profile_pic',
|
||||
field=models.ImageField(default='default-pfp.jpg', upload_to='profile_pics'),
|
||||
),
|
||||
]
|
|
@ -1,5 +1,3 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
from django.db import models
|
||||
|
@ -9,7 +7,7 @@ from PIL import Image
|
|||
|
||||
class Profile(models.Model):
|
||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||
profile_pic = models.ImageField(default='default.jpg', upload_to='profile_pics')
|
||||
profile_pic = models.ImageField(default='default-pfp.jpg', upload_to='profile_pics')
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.user.username} Profile'
|
||||
|
|
|
@ -6,11 +6,12 @@
|
|||
<img class="rounded-circle account-img" src="{{ user.profile.profile_pic.url }}">
|
||||
<div class="media-body">
|
||||
<h2 class="account-heading">{{ user.username }}</h2>
|
||||
<p class="text-secondary">{{ user.first_name }} {{ user.last_name }}</p>
|
||||
<p class="text-secondary">{{ user.email }}</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h2>Settings</h2>
|
||||
<hr>
|
||||
<h2>Social Settings</h2>
|
||||
|
||||
<h3>GitHub</h3>
|
||||
{% if github_login %}
|
||||
|
@ -18,7 +19,7 @@
|
|||
{% if can_disconnect %}
|
||||
<form method="post" action="{% url 'social:disconnect' 'github' %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit">Disconnect from GitHub</button>
|
||||
<button type="submit" class="btn btn-outline-info">Disconnect from GitHub</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<button type="button" disabled>Disconnect from GitHub</button>
|
||||
|
@ -34,7 +35,7 @@
|
|||
{% if can_disconnect %}
|
||||
<form method="post" action="{% url 'social:disconnect' 'google-oauth2' %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit">Disconnect from Google</button>
|
||||
<button type="submit" class="btn btn-outline-info">Disconnect from Google</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<button type="button" disabled>Disconnect from Google</button>
|
||||
|
@ -50,7 +51,7 @@
|
|||
{% if can_disconnect %}
|
||||
<form method="post" action="{% url 'social:disconnect' 'facebook' %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit">Disconnect from Facebook</button>
|
||||
<button type="submit" class="btn btn-outline-info">Disconnect from Facebook</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<button type="button" disabled>Disconnect from Facebook</button>
|
||||
|
@ -60,6 +61,7 @@
|
|||
<a href="{% url 'social:begin' 'facebook' %}">Connect to Facebook</a>
|
||||
{% endif %}
|
||||
|
||||
<hr>
|
||||
|
||||
<form method="post" enctype="multipart/form-data">
|
||||
{% csrf_token %}
|
||||
|
@ -72,6 +74,12 @@
|
|||
<button type="submit" class="btn btn-outline-info">Update</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<hr>
|
||||
<form method="post">
|
||||
{% csrf_token %}
|
||||
<legend class="border-bottom mb-4"> Update your password </legend>
|
||||
{{ passwordForm|crispy }}
|
||||
<button type="submit" class="btn btn-outline-info">Save changes</button>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
|
|
@ -9,8 +9,9 @@ from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
|
|||
from social_django.models import UserSocialAuth
|
||||
from .models import Profile
|
||||
|
||||
|
||||
def register(request):
|
||||
createProfileIfNotExist(request)
|
||||
|
||||
if request.method == 'POST':
|
||||
form = UserRegisterForm(request.POST)
|
||||
if form.is_valid():
|
||||
|
@ -25,12 +26,13 @@ def register(request):
|
|||
|
||||
@login_required
|
||||
def profile(request):
|
||||
createProfileIfNotExist(request)
|
||||
|
||||
user = request.user
|
||||
|
||||
if Profile.objects.filter(user=user).count() < 1:
|
||||
Profile.objects.create(user=user).save()
|
||||
|
||||
|
||||
if user.has_usable_password() is False:
|
||||
return redirect ('password')
|
||||
|
||||
|
@ -51,24 +53,42 @@ def profile(request):
|
|||
|
||||
can_disconnect = (user.social_auth.count() > 1 or user.has_usable_password())
|
||||
|
||||
|
||||
if request.user.has_usable_password():
|
||||
PasswordForm = PasswordChangeForm
|
||||
else:
|
||||
PasswordForm = AdminPasswordChangeForm
|
||||
|
||||
if request.method == "POST":
|
||||
userForm = UserUpdateForm(request.POST, instance=request.user)
|
||||
profileForm = ProfileUpdateForm(request.POST,
|
||||
request.FILES,
|
||||
instance=request.user.profile)
|
||||
|
||||
passwordForm = PasswordForm(request.user, request.POST)
|
||||
|
||||
if userForm.is_valid() and profileForm.is_valid():
|
||||
userForm.save()
|
||||
profileForm.save()
|
||||
messages.success(request, "Your account has been updated!")
|
||||
return redirect('profile')
|
||||
elif passwordForm.is_valid():
|
||||
passwordForm.save()
|
||||
update_session_auth_hash(request, passwordForm.user)
|
||||
messages.success(request, 'Your password was successfully updated!')
|
||||
return redirect('profile')
|
||||
else:
|
||||
messages.error(request, 'Please correct the errors below.')
|
||||
|
||||
else:
|
||||
userForm = UserUpdateForm(instance=request.user)
|
||||
profileForm = ProfileUpdateForm(instance=request.user.profile)
|
||||
passwordForm = PasswordForm(request.user)
|
||||
|
||||
context = {
|
||||
'userForm': userForm,
|
||||
'profileForm': profileForm,
|
||||
'passwordForm': passwordForm,
|
||||
'github_login': github_login,
|
||||
'google_login': google_login,
|
||||
'facebook_login': facebook_login,
|
||||
|
@ -79,6 +99,8 @@ def profile(request):
|
|||
|
||||
@login_required
|
||||
def password(request):
|
||||
createProfileIfNotExist(request)
|
||||
|
||||
if request.user.has_usable_password():
|
||||
PasswordForm = PasswordChangeForm
|
||||
else:
|
||||
|
@ -96,3 +118,8 @@ def password(request):
|
|||
else:
|
||||
form = PasswordForm(request.user)
|
||||
return render(request, 'users/password.html', {'form': form})
|
||||
|
||||
|
||||
def createProfileIfNotExist (request):
|
||||
if request.user.is_authenticated and Profile.objects.filter(user=request.user).count() < 1:
|
||||
Profile.objects.create(user=request.user).save()
|
||||
|
|
Loading…
Reference in New Issue
Block a user