Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: using cloudinary to upload files #3

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .sample.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Postgres db information

# Postgres
DB_NAME=
DB_USER=
DB_PASSWORD=
Expand All @@ -8,5 +7,8 @@ DB_PORT=

SECRET_KEY=

# Settings file
DJANGO_SETTINGS_MODULE='django_project.config.production'
# Settings
DJANGO_SETTINGS_MODULE='django_project.config.production'

# Cloudinary
CLOUDINARY_URL=
1 change: 1 addition & 0 deletions blog/templates/blog/home.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "blog/base.html" %}
{% load static %}
{% load cloudinary %}
{% block content %}
{% for post in posts %}
<article class="media content-section border mt-2 rounded p-2">
Expand Down
1 change: 1 addition & 0 deletions django_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"cloudinary"
]

MIDDLEWARE = [
Expand Down
4 changes: 4 additions & 0 deletions requirements-deploy.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
asgiref==3.8.1
black==23.1.0
certifi==2024.2.2
cfgv==3.4.0
click==8.1.7
cloudinary==1.40.0
crispy-bootstrap5==0.7
distlib==0.3.8
Django==4.2.10
Expand Down Expand Up @@ -29,8 +31,10 @@ pyflakes==3.0.1
python-dotenv==1.0.1
pytz==2024.1
PyYAML==6.0.1
six==1.16.0
sqlparse==0.5.0
uritemplate==4.1.1
urllib3==2.2.1
uvicorn==0.29.0
virtualenv==20.25.3
whitenoise==6.4.0
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Pillow==10.2.0
crispy-bootstrap5==0.7
gunicorn==20.1.0
whitenoise==6.4.0
cloudinary==1.40.0
12 changes: 8 additions & 4 deletions users/forms.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from cloudinary.forms import CloudinaryFileField
from .models import Profile


class UploadProfilePhotoForm(forms.Form):
profile_photo = forms.FileField(label="Profile Photo", required=True)


class UploadProfilePhotoForm(forms.ModelForm):
image = CloudinaryFileField(label="Profile Photo", required=True)

class Meta:
model = Profile
fields = ['image']

class UserRegisterForm(UserCreationForm):
email = forms.EmailField()

Expand Down
9 changes: 2 additions & 7 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
from django.contrib.auth.models import User
from django.db import models

from cloudinary.models import CloudinaryField

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default="default.png", upload_to="users/")

def save(self, *args, **kwargs):
if self.image != "default.png":
self.image.name = f"{self.user.username}/{self.user.username}.{self.image.name.split('.')[-1]}"
super().save(*args, **kwargs)
image = CloudinaryField('image')

def __str__(self):
return f"{self.user.username} Profile"
9 changes: 0 additions & 9 deletions users/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@
from django_project.settings import MEDIA_ROOT


def create_user_directory(username):
try:
os.makedirs(os.path.join(MEDIA_ROOT, "users", username))
except Exception as e:
print(e)


@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
username = instance.get_username()
create_user_directory(username)
Profile.objects.create(user=instance)
1 change: 1 addition & 0 deletions users/templates/users/profile.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "blog/base.html" %}
{% load cloudinary %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
Expand Down
41 changes: 27 additions & 14 deletions users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
class TestRegisterView(TestCase):
@classmethod
def setUpTestData(cls):
if(os.path.exists(os.path.join(settings.MEDIA_ROOT, "users", "random"))):
os.rmdir(os.path.join(settings.MEDIA_ROOT, "users", "random"))
pass

@classmethod
def tearDownClass(cls):
if(os.path.exists(os.path.join(settings.MEDIA_ROOT, "users", "random"))):
os.rmdir(os.path.join(settings.MEDIA_ROOT, "users", "random"))
pass

def test_get_register_view(self):
response = self.client.get("/register/")
Expand Down Expand Up @@ -46,21 +44,12 @@ def setUpTestData(cls):

@classmethod
def tearDownClass(cls):
media_dir = settings.MEDIA_ROOT
username = cls.user.username
if(os.path.exists(os.path.join(media_dir, 'users', username))):
os.rmdir(os.path.join(media_dir, 'users', username))
pass

def test_get_login_view(self):
response = self.client.get("/login/")
self.assertEqual(response.status_code, 200)

def test_user_directory(self):
username = self.user.username
media_dir = settings.MEDIA_ROOT
user_dir = os.path.join(media_dir, 'users', username)
self.assertTrue(os.path.isdir(user_dir)) # asserts user directory is created with each user
self.assertEqual(len(os.listdir(user_dir)), 0) # asserts user directory is empty

def test_login(self):
username = self.user.username
Expand All @@ -75,4 +64,28 @@ def test_login(self):
def test_logout(self):
response = self.client.get("/logout/")
self.assertEqual(response.status_code, 200)


class TestAboutView(TestCase):
def test_get_about_view(self):
response = self.client.get("/about/")
self.assertEqual(response.status_code, 200)


class TestProfileView(TestCase):

@classmethod
def setUpTestData(cls):
cls.user = User.objects.create_user(
username="random", email="[email protected]", password="randomuser"
)

def test_login_required(self):
response = self.client.get("/profile/")
self.assertEqual(response.status_code, 302)


def test_get_profile_view(self):
self.client.login(username="random", password="randomuser")
response = self.client.get("/profile/")
self.assertEqual(response.status_code, 200)
Loading