Skip to content

Commit

Permalink
Merge 6dd2aa2 into 82ff440
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamoreA committed Dec 14, 2018
2 parents 82ff440 + 6dd2aa2 commit d8e1fcb
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
59 changes: 59 additions & 0 deletions authors/apps/articles/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Generated by Django 2.1 on 2018-12-13 17:13

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('profiles', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=200)),
('description', models.TextField()),
('body', models.TextField()),
('image', models.URLField(blank=True)),
('article_slug', models.SlugField(editable=False, max_length=255, unique=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='authorshaven', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body', models.TextField(max_length=1000)),
('created_at', models.DateTimeField(auto_now_add=True)),
('last_update', models.DateTimeField(auto_now=True)),
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='articles.Article')),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='profiles.Profile')),
],
options={
'ordering': ['-created_at', '-last_update'],
},
),
migrations.CreateModel(
name='Reply',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('body', models.TextField()),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reply', to='profiles.Profile')),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='reply', to='articles.Comment')),
],
options={
'ordering': ['-created_at'],
},
),
]
10 changes: 8 additions & 2 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,28 @@ def get(self, request, slug):
"""
Method for getting one article.
"""
article = Article.objects.get(article_slug=slug)
article = Article.objects.filter(article_slug=slug).first()
final = dict()
serializer = ArticleAuthorSerializer(article)
final.update(serializer.data)
final.update({"rating": article.rating})
message = {'message': "Article found.", 'article': final}
if article is None:
return Response({"message": "Article not found, Please check your slug"}, status=status.HTTP_404_NOT_FOUND)
return Response(message, status=status.HTTP_200_OK)



def put(self, request, slug):

"""
Method for editing an article.
"""
# A slug should be provided.
# An article should be edited by the person who created it only.
article = Article.objects.get(article_slug=slug)
article = Article.objects.filter(article_slug=slug).first()
if article is None:
return Response({"message": "Article not found with that slug"}, status=status.HTTP_404_NOT_FOUND)
if request.user.pk != article.author_id:
return Response({'Error': "You are not allowed to perform this request."},
status=status.HTTP_403_FORBIDDEN)
Expand Down

0 comments on commit d8e1fcb

Please sign in to comment.