Skip to content

Commit

Permalink
Merge pull request #57 from builtwithdjango/check-project-validity
Browse files Browse the repository at this point in the history
Check project validity
  • Loading branch information
rasulkireev committed Jun 12, 2024
2 parents b367bdb + ac87f32 commit 24ee234
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ and this project tries to adhere to [Semantic Versioning](https://semver.org/spe
**Fixed** for any bug fixes.
**Security** in case of vulnerabilities.

## [Unreleased]
## [0.4.2] - 2024-06-12
### Added
- Added a task to check if project is active.

## [0.4.1] - 2022-07-10
### Added
- Complete Redesign
- Complete Redesign.

## [0.4.0] - 2022-07-10
### Added
Expand Down
2 changes: 1 addition & 1 deletion pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HomeView(TemplateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["newsletter_form"] = NewsletterSignupForm
context["projects"] = Project.objects.filter(published=True)[:4]
context["projects"] = Project.objects.filter(published=True, active=True)[:4]
context["guides"] = Post.objects.all()[:4]
context["podcast_episodes"] = Episode.objects.all()[:3]
context["jobs"] = Job.objects.filter(approved=True).order_by("-created_datetime")[:6]
Expand Down
18 changes: 18 additions & 0 deletions projects/migrations/0024_project_active.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.4 on 2024-06-12 09:16

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("projects", "0023_alter_project_user_email"),
]

operations = [
migrations.AddField(
model_name="project",
name="active",
field=models.BooleanField(default=True),
),
]
17 changes: 14 additions & 3 deletions projects/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import requests
from autoslug import AutoSlugField
from django.conf import settings
from django.db import models
Expand All @@ -23,6 +24,7 @@ class Project(models.Model):
large_company = models.BooleanField(default=False)
type = models.CharField(max_length=50, blank=True)
is_profitable = models.BooleanField(default=False)
active = models.BooleanField(default=True)

# Optional Website Information
is_open_source = models.BooleanField(default=False)
Expand Down Expand Up @@ -56,15 +58,24 @@ class Project(models.Model):
blank=True,
)

class Meta:
ordering = ["-date_added"]

def __str__(self):
return self.title

def get_absolute_url(self):
return reverse("project", kwargs={"slug": self.slug})

def check_project_is_active(self):
try:
response = requests.get(self.url, timeout=7)
self.active = response.status_code == 200
except requests.RequestException:
self.active = False

return self.active

class Meta:
ordering = ["-date_added"]


class Technology(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
Expand Down
20 changes: 20 additions & 0 deletions projects/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.conf import settings
from django.core.files.base import ContentFile
from django.core.mail import send_mail
from django_q.tasks import async_task

from .models import Project

Expand Down Expand Up @@ -77,3 +78,22 @@ def notify_admins_of_comment(instance):
["Built with Django <rasul@builtwithdjango.com>"],
fail_silently=False,
)


def check_all_projects():
projects = Project.objects.all()

for project in projects:
async_task(update_project_active_status, project.id, group="Check Project Active Status")


def update_project_active_status(project_id):
project = Project.objects.get(id=project_id)

active = project.check_project_is_active()

if not active:
project.active = False
project.save(update_fields=["active"])

return f"Project {project.title} is active: {active}"
10 changes: 4 additions & 6 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,22 @@ class ProjectListView(FilterView):
filterset_class = ProjectFilter

def get_queryset(self):
queryset = Project.objects.filter(published=True).order_by("-updated_date")
queryset = Project.objects.filter(published=True, active=True)

if self.request.GET.get("order_by"):
ordering = self.request.GET.get("order_by")

if ordering == "like":
queryset = (
Project.objects.filter(published=True)
queryset
# need like_count as an alias for comlex query
# https://stackoverflow.com/questions/39375339/django-complex-annotations-require-an-alias-what-is-alias-here
.annotate(like__count=Count("like", filter=Q(like__like=True))).order_by(f"-like__count")
)
elif ordering == "comments":
queryset = (
Project.objects.filter(published=True).annotate(Count(ordering)).order_by(f"-{ordering}__count")
)
queryset = queryset.annotate(Count(ordering)).order_by(f"-{ordering}__count")
else:
queryset = Project.objects.filter(published=True).order_by("-updated_date")
queryset = queryset.order_by("-updated_date")

return queryset

Expand Down

0 comments on commit 24ee234

Please sign in to comment.