Commit 760fb918 authored by Ola Hermann Opheim's avatar Ola Hermann Opheim
Browse files

refac new_project

parent a5671c23
from user.models import Profile
from projects.models import Task,ProjectCategory
from django.shortcuts import get_object_or_404
def create_new_project(request, project):
project.user = request.user.profile
project.category = get_object_or_404(ProjectCategory, id=request.POST.get('category_id'))
project.save()
people = Profile.objects.filter(categories__id=project.category.id)
from django.core import mail
from django.contrib.sites.shortcuts import get_current_site
current_site = get_current_site(request)
for person in people:
if person.user.email:
try:
with mail.get_connection() as connection:
mail.EmailMessage(
"New Project: " + project.title , "A new project you might be interested in was created and can be viwed at " + current_site.domain + '/projects/' + str(project.id), "Agreelancer", [person.user.email],
connection=connection,
).send()
except Exception as e:
from django.contrib import messages
messages.success(request, 'Sending of email to ' + person.user.email + " failed: " + str(e))
task_title = request.POST.getlist('task_title')
task_description = request.POST.getlist('task_description')
task_budget = request.POST.getlist('task_budget')
for i in range(0, len(task_title)):
Task.objects.create(
title = task_title[i],
description = task_description[i],
budget = task_budget[i],
project = project,
)
\ No newline at end of file
import unittest
from django.test import Client
from projects.models import Project, ProjectCategory, Task, TaskOffer
from test_helpers import create_test_user, delete_test_data
class NewProjectRegressionTest(unittest.TestCase):
def setUp(self):
self.client = Client()
self.user = create_test_user()
ProjectCategory.objects.create(name='Category')
self.request_data = {
'title': 'Project1',
'description': 'Description',
'category_id': 1,
'task_title': 'Task1',
'task_budget': 123,
'task_description': 'TaskDescription'
}
def tearDown(self):
delete_test_data()
def test_views_new_project(self):
self.client.login(username= 'User', password= 'top_secret')
response = self.client.post('/projects/new/', self.request_data)
new_project_id = int(response.url.split("/")[2])
new_project = Project.objects.get(pk=new_project_id)
self.assertEquals(new_project.user.user, self.user)
self.assertEquals(new_project.category.name, "Category")
new_task = Task.objects.get(title="Task1")
self.assertEquals(new_task.description, "TaskDescription")
self.assertEquals(new_task.budget, 123)
self.assertEquals(new_task.project, new_project)
......@@ -7,7 +7,7 @@ from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .templatetags.projects_extras import sort_projects
#from .templatetags.new_project_extras import create_new_project
from .templatetags.new_project_extras import create_new_project
def projects(request):
......@@ -32,40 +32,11 @@ def projects(request):
@login_required
def new_project(request):
from django.contrib.sites.shortcuts import get_current_site
current_site = get_current_site(request)
if request.method == 'POST':
form = ProjectForm(request.POST)
if form.is_valid():
project = form.save(commit=False)
project.user = request.user.profile
project.category = get_object_or_404(ProjectCategory, id=request.POST.get('category_id'))
project.save()
people = Profile.objects.filter(categories__id=project.category.id)
from django.core import mail
for person in people:
if person.user.email:
try:
with mail.get_connection() as connection:
mail.EmailMessage(
"New Project: " + project.title , "A new project you might be interested in was created and can be viwed at " + current_site.domain + '/projects/' + str(project.id), "Agreelancer", [person.user.email],
connection=connection,
).send()
except Exception as e:
from django.contrib import messages
messages.success(request, 'Sending of email to ' + person.user.email + " failed: " + str(e))
task_title = request.POST.getlist('task_title')
task_description = request.POST.getlist('task_description')
task_budget = request.POST.getlist('task_budget')
for i in range(0, len(task_title)):
Task.objects.create(
title = task_title[i],
description = task_description[i],
budget = task_budget[i],
project = project,
)
create_new_project(request, project)
return redirect('project_view', project_id=project.id)
else:
form = ProjectForm()
......
......@@ -5,7 +5,7 @@ from projects.models import Project, ProjectCategory, Task, TaskOffer
from payment.models import Payment
def create_test_user():
User.objects.create_user(
return User.objects.create_user(
username='User',
email='test@test.com',
password='top_secret'
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment