-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- user can view articles - user can create articles - user can update articles - user can delete articles - fix hound errors - fix travis build error - fix merge conflict [Delivers #161254663]
- Loading branch information
Showing
25 changed files
with
366 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# this is how the db will be structured. | ||
|
||
from django.db import models | ||
from django.utils.translation import pgettext_lazy as _ | ||
from django.contrib.auth import get_user_model | ||
from django.core.validators import MaxValueValidator, MinValueValidator | ||
|
||
'''Django-autoslug is a reusable Django library | ||
that provides an improved slug field which can automatically: | ||
populate itself from another field and preserve | ||
uniqueness of the value''' | ||
from autoslug import AutoSlugField | ||
from versatileimagefield.fields import VersatileImageField | ||
|
||
|
||
class Article(models.Model): | ||
user = models.ForeignKey( | ||
get_user_model(), | ||
related_name='author', | ||
on_delete=models.CASCADE, | ||
null=True, | ||
blank=True, | ||
default=None | ||
) | ||
# creates a random identifier for a particular article from the title | ||
# field. | ||
slug = AutoSlugField( | ||
populate_from='title', | ||
blank=True, | ||
null=True, | ||
unique=True) | ||
title = models.CharField( | ||
_('Article field', 'title'), | ||
unique=True, | ||
max_length=128 | ||
) | ||
description = models.TextField( | ||
_('Article Field', 'description'), | ||
blank=True, | ||
null=True | ||
) | ||
body = models.TextField( | ||
_('Article Field', 'body'), | ||
blank=True, | ||
null=True | ||
) | ||
image = VersatileImageField( | ||
'Image', | ||
upload_to='article/', | ||
width_field='width', | ||
height_field='height', | ||
blank=True, | ||
null=True | ||
) | ||
height = models.PositiveIntegerField( | ||
'Image Height', | ||
blank=True, | ||
null=True | ||
) | ||
width = models.PositiveIntegerField( | ||
'Image Width', | ||
blank=True, | ||
null=True | ||
) | ||
created_at = models.DateTimeField( | ||
_('Article field', 'created at'), | ||
auto_now_add=True, | ||
editable=False | ||
) | ||
updated_at = models.DateTimeField( | ||
_('Article field', 'updated at'), | ||
auto_now=True | ||
) | ||
|
||
class Meta: | ||
app_label = "article" | ||
|
||
def __str__(self): | ||
return self.title | ||
|
||
|
||
class RateArticle(models.Model): | ||
""" | ||
This is the article class. It holds data for the article. | ||
""" | ||
rater = models.ForeignKey( | ||
"authentication.User", | ||
related_name="ratearticle", | ||
on_delete=models.CASCADE) # link with the user who rated | ||
article = models.ForeignKey( | ||
"article.Article", | ||
related_name="ratearticle", | ||
on_delete=models.CASCADE) # link with the article being rated | ||
rate = models.IntegerField(null=False, blank=False, | ||
validators=[ | ||
MaxValueValidator(5), | ||
MinValueValidator(1) | ||
]) # rate value column | ||
|
||
def __str__(self): | ||
""" | ||
Return a human readable format | ||
""" | ||
return self.rate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'''Serializers allow complex data | ||
such as querysets and model instances | ||
to be converted to | ||
native Python datatypes that can then | ||
be easily rendered into JSON, XML or other content types.''' | ||
|
||
from rest_framework import serializers | ||
from django.apps import apps | ||
from .models import RateArticle | ||
from authors.apps.profiles.serializers import ProfileListSerializer | ||
|
||
|
||
TABLE = apps.get_model('article', 'Article') | ||
Profile = apps.get_model('profiles', 'UserProfile') | ||
|
||
NAMESPACE = 'article' | ||
fields = ('id', 'slug', 'image', 'title', 'description', 'body', 'user',) | ||
|
||
|
||
class ArticleSerializer(serializers.ModelSerializer): | ||
update_url = serializers.HyperlinkedIdentityField( | ||
view_name=NAMESPACE + ':update', lookup_field='slug') | ||
delete_url = serializers.HyperlinkedIdentityField( | ||
view_name=NAMESPACE + ':delete', lookup_field='slug') | ||
author = serializers.SerializerMethodField(read_only=True) | ||
|
||
class Meta: | ||
model = TABLE | ||
|
||
fields = fields + ('author', 'update_url', 'delete_url') | ||
|
||
def get_author(self, obj): | ||
try: | ||
serializer = ProfileListSerializer( | ||
instance=Profile.objects.get(user=obj.user) | ||
) | ||
return serializer.data | ||
except BaseException: | ||
return {} | ||
|
||
def update(self, instance, validated_data): | ||
instance.title = validated_data.get('title', instance.title) | ||
instance.description = validated_data.get( | ||
'description', instance.description) | ||
instance.body = validated_data.get('body', instance.body) | ||
if validated_data.get('image'): | ||
instance.image = validated_data.get('image', instance.image) | ||
|
||
instance.save() | ||
|
||
return instance | ||
|
||
|
||
class ArticleCreateSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = TABLE | ||
|
||
fields = fields | ||
|
||
def create(self, validated_data): | ||
instance = TABLE.objects.create(**validated_data) | ||
validated_data['slug'] = instance.slug | ||
|
||
return validated_data | ||
|
||
|
||
class RateArticleSerializer(serializers.ModelSerializer): | ||
""" | ||
validate rate article | ||
""" | ||
slug = serializers.SlugField() | ||
rate = serializers.IntegerField() | ||
|
||
def validate(self, data): | ||
rate = data['rate'] | ||
if not rate > 0 or not rate <= 5: | ||
raise serializers.ValidationError( | ||
'invalid rate value should be > 0 or <=5') | ||
|
||
return data | ||
|
||
class Meta: | ||
model = RateArticle | ||
fields = ("slug", "rate") |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django.urls import path | ||
from rest_framework_swagger.views import get_swagger_view | ||
from .views import ( | ||
ArticleCreateAPIView, | ||
ArticleListAPIView, | ||
ArticleDeleteAPIView, | ||
ArticleDetailAPIView, | ||
ArticleUpdateAPIView, | ||
Rate, | ||
ArticleRate | ||
) | ||
|
||
schema_view = get_swagger_view(title="Articles") | ||
|
||
urlpatterns = [ | ||
path( | ||
'article/', | ||
ArticleListAPIView.as_view(), | ||
name='list'), | ||
path( | ||
'article/create', | ||
ArticleCreateAPIView.as_view(), | ||
name='create'), | ||
path( | ||
'article/delete/<slug>/', | ||
ArticleDeleteAPIView.as_view(), | ||
name='delete'), | ||
path( | ||
'article/detail/<slug>/', | ||
ArticleDetailAPIView.as_view(), | ||
name='detail'), | ||
path( | ||
'article/update/<slug>/', | ||
ArticleUpdateAPIView.as_view(), | ||
name='update'), | ||
path( | ||
'article/<str:slug>/rate/', | ||
Rate.as_view(), | ||
name="rate"), | ||
path( | ||
'article/rate/<str:pk>/', | ||
ArticleRate.as_view(), | ||
name="view_rate"), | ||
] |
79 changes: 72 additions & 7 deletions
79
authors/apps/articles/views.py → authors/apps/article/views.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.