Skip to content

Commit

Permalink
feat: using cloudinary to upload files
Browse files Browse the repository at this point in the history
updated tests
  • Loading branch information
gh0stfrk committed Apr 23, 2024
1 parent a6c961b commit 5d96ce4
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 38 deletions.
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
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)

0 comments on commit 5d96ce4

Please sign in to comment.