-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(fix): fixed the upload url path
- Loading branch information
1 parent
4eb9d44
commit 3c2084e
Showing
26 changed files
with
183 additions
and
65 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
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,18 @@ | ||
# Generated by Django 5.0.7 on 2024-07-22 17:51 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0007_alter_article_status'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='thumb', | ||
field=models.ImageField(blank=True, default='../static/images/img.webp', upload_to=''), | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 5.0.7 on 2024-07-22 18:30 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0008_alter_article_thumb'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='thumb', | ||
field=models.ImageField(blank=True, default='../media/images/img.webp', upload_to='images/'), | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 5.0.7 on 2024-07-22 18:32 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0009_alter_article_thumb'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='thumb', | ||
field=models.ImageField(blank=True, default='/media/images/img.webp', upload_to='images/'), | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 5.0.7 on 2024-07-22 18:36 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0010_alter_article_thumb'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='thumb', | ||
field=models.ImageField(blank=True, default='images/img.webp', upload_to='images/'), | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 5.0.7 on 2024-07-22 18:37 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0011_alter_article_thumb'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='thumb', | ||
field=models.ImageField(blank=True, default='../static/images/img.webp', upload_to='images/'), | ||
), | ||
] |
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,18 @@ | ||
# Generated by Django 5.0.7 on 2024-07-22 18:42 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('research', '0012_alter_article_thumb'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='article', | ||
name='thumb', | ||
field=models.ImageField(blank=True, default='../media/images/2077-Collective.png', upload_to='images/'), | ||
), | ||
] |
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 |
---|---|---|
@@ -1,52 +1,47 @@ | ||
from django.db import models | ||
from django.contrib.auth.models import User | ||
from django.utils.text import slugify | ||
from django.utils import timezone | ||
|
||
# Create your models here. | ||
class Article(models.Model): | ||
|
||
# Custom manager to retrieve only ready articles | ||
|
||
class ArticleObjects(models.Manager): | ||
def get_queryset(self): | ||
return super().get_queryset() .filter(status='ready') | ||
return super().get_queryset().filter(status='ready') | ||
|
||
options = ( | ||
('draft', 'Draft'), | ||
('ready', 'Ready'), | ||
) | ||
|
||
title = models.CharField(max_length=100) | ||
content = models.TextField() | ||
summary = models.TextField(blank=True) | ||
author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True) | ||
slug = models.SlugField(blank=True) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
category = models.CharField(max_length=100, blank=True) | ||
thumb = models.ImageField(default='./static/images/img.webp', blank=True) | ||
thumb = models.ImageField(upload_to='images/', default='../media/images/2077-Collective.png', blank=True) | ||
views = models.PositiveBigIntegerField(default=0) | ||
status = models.CharField( | ||
max_length=10, choices=options, default='draft') | ||
objects = models.Manager() # default manager | ||
postobjects = ArticleObjects() # custom manager | ||
status = models.CharField(max_length=10, choices=options, default='draft') | ||
objects = models.Manager() | ||
postobjects = ArticleObjects() | ||
|
||
# Order articles by creation date (most recent) | ||
class Meta: | ||
ordering = ('-created_at',) | ||
|
||
def __str__(self): | ||
return self.title | ||
|
||
# Generate a unique slug for each article | ||
|
||
def save(self, *args, **kwargs): | ||
if not self.slug: | ||
base_slug = slugify(self.title) | ||
slug = base_slug | ||
num = 1 | ||
while Article.objects.filter(slug=slug).exists(): | ||
slug = f"{base_slug}-{num}" | ||
num += 1 | ||
self.slug = slug | ||
self.slug = self.generate_unique_slug() | ||
super().save(*args, **kwargs) | ||
|
||
|
||
|
||
def generate_unique_slug(self): | ||
base_slug = slugify(self.title) | ||
slug = base_slug | ||
num = 1 | ||
while Article.objects.filter(slug=slug).exists(): | ||
slug = f"{base_slug}-{num}" | ||
num += 1 | ||
return slug |
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,7 @@ | ||
from .models import Article | ||
|
||
def get_article_by_id(article_id): | ||
return Article.objects.get(pk=article_id) | ||
|
||
def get_ready_articles(): | ||
return Article.postobjects.all() |
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,16 @@ | ||
from .models import Article | ||
from django.db.models import F | ||
|
||
def increment_article_views(article: Article): | ||
""" | ||
Increment the view count for the given article. | ||
""" | ||
# Increment the view count atomically | ||
Article.objects.filter(pk=article.pk).update(views=F('views') + 1) | ||
article.refresh_from_db() | ||
|
||
def create_article(**kwargs): | ||
""" | ||
Create a new article with the given parameters. | ||
""" | ||
return Article.objects.create(**kwargs) |
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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
from django.urls import path | ||
from .views import index, ArticleListCreate, ArticleDetail, increment_view | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
from .views import ArticleViewSet, index | ||
|
||
router = DefaultRouter() | ||
router.register(r'articles', ArticleViewSet, basename='article') | ||
|
||
|
||
# Define the URL patterns for the research app | ||
urlpatterns = [ | ||
path('', index, name='index'), | ||
path('articles/', ArticleListCreate.as_view(), name='article-list-create'), | ||
path('articles/<int:pk>/', ArticleDetail.as_view(), name='article'), | ||
path('articles/<int:pk>/increment-views/', increment_view, name='increment_view'), # Increment views for an article | ||
] | ||
path('', index, name='index'), | ||
path('api/', include(router.urls)), | ||
] | ||
|
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 |
---|---|---|
@@ -1,31 +1,23 @@ | ||
from rest_framework import generics | ||
from rest_framework import viewsets, status | ||
from rest_framework.response import Response | ||
from django.db.models import F | ||
from .models import Article | ||
from .serializers import ArticleSerializer | ||
from django.http import JsonResponse | ||
from django.shortcuts import render, get_object_or_404 | ||
from django.views.decorators.http import require_POST | ||
from django.views.decorators.csrf import csrf_exempt | ||
|
||
|
||
from django.shortcuts import render | ||
|
||
def index(request): | ||
return render(request, 'index.html') | ||
|
||
# renders only the articles with status 'ready' | ||
class ArticleListCreate(generics.ListCreateAPIView): | ||
queryset = Article.postobjects.all() | ||
class ArticleViewSet(viewsets.ModelViewSet): | ||
serializer_class = ArticleSerializer | ||
|
||
class ArticleDetail(generics.RetrieveUpdateDestroyAPIView): | ||
queryset = Article.objects.all() | ||
serializer_class = ArticleSerializer | ||
|
||
def get_queryset(self): | ||
return Article.objects.filter(status='ready') | ||
|
||
# Increment views for an article | ||
@csrf_exempt | ||
@require_POST | ||
def increment_view(request, pk): | ||
article = get_object_or_404(Article, pk=pk) | ||
article.views += 1 | ||
article.save() | ||
return JsonResponse({'views': article.views}) | ||
def retrieve(self, request, *args, **kwargs): | ||
instance = self.get_object() | ||
instance.views = F('views') + 1 | ||
instance.save(update_fields=['views']) | ||
# Refresh the instance to get the updated views value | ||
instance.refresh_from_db(fields=['views']) | ||
serializer = self.get_serializer(instance) | ||
return Response(serializer.data) |
File renamed without changes.
File renamed without changes.