Skip to content

Commit

Permalink
Fly deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiul-999 committed Nov 29, 2024
1 parent 6549dfd commit e996d4d
Show file tree
Hide file tree
Showing 528 changed files with 63,994 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.venv/
__pycache__/
*.sqlite3
.git
.env
18 changes: 18 additions & 0 deletions .github/workflows/fly-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/

name: Fly Deploy
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
concurrency: deploy-group # optional: ensure only one action runs at a time
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.venv/
__pycache__
*.sqlite3
*.sqlite3
.env
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ARG PYTHON_VERSION=3.12-slim

FROM python:${PYTHON_VERSION}

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies.
RUN apt-get update && apt-get install -y \
libpq-dev \
gcc \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir -p /code

WORKDIR /code

COPY requirements.txt /tmp/requirements.txt
RUN set -ex && \
pip install --upgrade pip && \
pip install -r /tmp/requirements.txt && \
rm -rf /root/.cache/
COPY . /code

ENV SECRET_KEY "sZMGco5qgqThLmYYHCucA47PLoIPySW2Mo1XEddufGfMjK8gpE"
RUN python manage.py collectstatic --noinput

EXPOSE 8000

CMD ["gunicorn","--bind",":8000","--workers","2","django_project.wsgi"]
10 changes: 9 additions & 1 deletion articles/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
from django.contrib import admin
from .models import Article
from .models import Article, Comment

# Register your models here.

class CommentInLine(admin.TabularInline):
model = Comment
extra = 0

class ArticleAdmin(admin.ModelAdmin):
inlines = [
CommentInLine,
]
list_display = [
"title",
"body",
"author",
]

admin.site.register(Article, ArticleAdmin)
admin.site.register(Comment)
9 changes: 9 additions & 0 deletions articles/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import forms

from .models import Comment


class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ("comment",)
45 changes: 45 additions & 0 deletions articles/migrations/0002_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 5.1.3 on 2024-11-29 12:23

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


class Migration(migrations.Migration):

dependencies = [
("articles", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Comment",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("comment", models.CharField(max_length=140)),
(
"article",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="articles.article",
),
),
(
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
17 changes: 16 additions & 1 deletion articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ def __str__(self):
return self.title

def get_absolute_url(self):
return reverse("article_detail", kwargs={"pk": self.pk})
return reverse("article_detail", kwargs={"pk": self.pk})

class Comment(models.Model):
article = models.ForeignKey(Article, on_delete=models.CASCADE)
comment = models.CharField(max_length=140)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)

def __str__(self):
return self.comment

def get_absolute_url(self):
return reverse("article_list")

49 changes: 45 additions & 4 deletions articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,59 @@
LoginRequiredMixin,
UserPassesTestMixin
)
from django.views.generic import ListView, DetailView
from django.views import View
from django.views.generic import ListView, DetailView, FormView
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.edit import UpdateView, DeleteView, CreateView
from django.urls import reverse_lazy
from django.urls import reverse_lazy, reverse

from .models import Article
from .forms import CommentForm

class ArticleListView(LoginRequiredMixin, ListView):
model = Article
template_name = "article_list.html"

class ArticleDetailView(LoginRequiredMixin, DetailView):
class CommentGet(DetailView):
model = Article
template_name = "article_detail.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"] = CommentForm()
return context

class CommentPost(SingleObjectMixin, FormView):
model = Article
form_class = CommentForm
template_name = "article_detail.html"

def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super().post(request, *args, **kwargs)

def form_valid(self, form):
comment = form.save(commit=False)
comment.article = self.object
comment.author = self.request.user
comment.save()
return super().form_valid(form)

def get_success_url(self):
article = self.object
return reverse("article_detail", kwargs={"pk": article.pk})




class ArticleDetailView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
view = CommentGet.as_view()
return view(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
view = CommentPost.as_view()
return view(request, *args, **kwargs)

class ArticleUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Article
Expand Down Expand Up @@ -44,4 +84,5 @@ class ArticleCreateView(LoginRequiredMixin, CreateView):

def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
return super().form_valid(form)

28 changes: 21 additions & 7 deletions django_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"""

from pathlib import Path
from environs import Env

env = Env()
env.read_env()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -20,12 +24,13 @@
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-_db%h4lujl_8rfgv4)383-l^5g22j=lq5t1wlwbd&132w8+0#8"
SECRET_KEY = env.str("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = env.bool("DEBUG", default=False)

ALLOWED_HOSTS = []
ALLOWED_HOSTS = ["newsbucket.fly.dev", "localhost", "127.0.0.1"]
CSRF_TRUSTED_ORIGINS = ["https://newsbucket.fly.dev"]


# Application definition
Expand All @@ -36,6 +41,7 @@
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"whitenoise.runserver_nostatic",
"django.contrib.staticfiles",
# 3rd Party
"crispy_forms",
Expand All @@ -49,6 +55,7 @@
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
Expand Down Expand Up @@ -81,10 +88,7 @@
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
"default": env.dj_db_url("DATABASE_URL", default="sqlite:///db.sqlite3"),
}


Expand Down Expand Up @@ -123,6 +127,16 @@
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"
STATICFILES_DIRS = [BASE_DIR / "static"]
STATIC_ROOT = BASE_DIR / "staticfiles"
STORAGES = {
"default": {
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
Expand Down
2 changes: 1 addition & 1 deletion django_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
#from django.views.generic.base import TemplateView

urlpatterns = [
path("admin/", admin.site.urls),
Expand Down
33 changes: 33 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# fly.toml app configuration file generated for newsbucket on 2024-11-29T20:11:32+06:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'newsbucket'
primary_region = 'sin'
console_command = '/code/manage.py shell'

[build]

[deploy]
release_command = 'python manage.py migrate --noinput'

[env]
PORT = '8000'

[http_service]
internal_port = 8000
force_https = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1

[[statics]]
guest_path = '/code/static'
url_prefix = '/static/'
24 changes: 24 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
asgiref==3.8.1
black==24.10.0
click==8.1.7
colorama==0.4.6
crispy-bootstrap5==2024.10
dj-database-url==2.3.0
dj-email-url==1.0.6
Django==5.1.3
django-cache-url==3.4.5
django-crispy-forms==2.3
environs==11.2.1
gunicorn==23.0.0
marshmallow==3.23.1
mypy-extensions==1.0.0
packaging==24.2
pathspec==0.12.1
platformdirs==4.3.6
psycopg==3.2.3
psycopg-binary==3.2.3
python-dotenv==1.0.1
sqlparse==0.5.2
typing_extensions==4.12.2
tzdata==2024.2
whitenoise==6.8.2
Empty file added static/css/.keep
Empty file.
Empty file added static/img/.keep
Empty file.
Empty file added static/js/.keep
Empty file.
Loading

0 comments on commit e996d4d

Please sign in to comment.