Skip to content

Commit

Permalink
Add House creation logic after login as a user
Browse files Browse the repository at this point in the history
Add Create House View
  • Loading branch information
ronyyosef committed May 5, 2022
1 parent 480d4ff commit 66eb61c
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 85 deletions.
1 change: 1 addition & 0 deletions house/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
LOGIN_PAGE_ROUTE = 'login/login.html'
USER_LOGIN_PAGE_ROUTE = 'registration/login.html'
USER_SIGNUP_ROUTE = 'registration/signup.html'
HOUSE_CREATE_ROUTE = 'house_view/house_create.html'
14 changes: 12 additions & 2 deletions house/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,15 @@ def __init__(self, *args, **kwargs):
self.fields['children'].required = False


class HouseIDForm(forms.Form):
house_id = forms.UUIDField(label='Enter Your House ID')
class HouseCreationForm(forms.ModelForm):
class Meta:
model = House
fields = (
'public',
'country',
'city',
'parent_profession_1',
'parent_profession_2',
'income',
'children'
)
2 changes: 1 addition & 1 deletion house/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ class Migration(migrations.Migration):
name='country',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='house.country'),
),
]
]
5 changes: 2 additions & 3 deletions house/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
urlpatterns = [
path('', views.home_page, name='home'),
path('global/', views.global_page, name='global_page'),
path('login/', views.house_login, name='house_login'),
path('house/<str:house_id>/', views.house_view, name='house_view'),
path('house/add', views.add_house, name='add_house'),
path('house_create/', views.house_create, name='house_create'),
path('house/', views.house_view, name='house_view'),
path('ajax/load-cities/', views.load_cities, name='ajax_load_cities'),
]
79 changes: 44 additions & 35 deletions house/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from house.constants import MINE_MINE_PAGE_ROUTE
from django.shortcuts import render, get_object_or_404
from house.constants import HOME_PAGE_ROUTE, GLOBAL_PAGE_ROUTE, GLOBAL_PAGE_CITY_DROPDOWN_ROUTE, LOGIN_PAGE_ROUTE
from .forms import HouseForm, HouseIDForm
from .helpers import _filter_houses_by_form
from expenses.models import Expenses
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ValidationError
from django.http import HttpResponseRedirect
from django.shortcuts import render

from expenses.models import Expenses
from house.constants import HOME_PAGE_ROUTE, GLOBAL_PAGE_ROUTE, GLOBAL_PAGE_CITY_DROPDOWN_ROUTE
from house.constants import MINE_MINE_PAGE_ROUTE, HOUSE_CREATE_ROUTE
from .forms import HouseForm, HouseCreationForm
from .helpers import _filter_houses_by_form
from .models import House, City


Expand Down Expand Up @@ -33,43 +35,50 @@ def global_page(request):
return render(request, GLOBAL_PAGE_ROUTE, context)


def house_login(request):
errorMsg = ""

if request.method == 'POST':
try:
form = HouseIDForm(request.POST)
house_id = form.data["house_id"]

if House.objects.filter(house_id=house_id).count() == 1:
return HttpResponseRedirect(f'/../house/{house_id}')
else:
errorMsg = "There is no House with the provided ID"
form = HouseIDForm()
# In case an exception not a valid UUID is thrown
except ValidationError:
errorMsg = "There is no House with the provided ID : " + house_id
form = HouseIDForm()
else:
form = HouseIDForm()

return render(request, LOGIN_PAGE_ROUTE, {'form': form, 'msg': errorMsg})
@login_required
def house_view(request):
user = request.user
if hasattr(user, 'house') is False:
return HttpResponseRedirect('/../house_create')


def house_view(request, house_id):
house = get_object_or_404(House, pk=house_id)
house = user.house
expenses_list = Expenses.objects.filter(house_name=house)
context = {'house': house,
'house_expenses': expenses_list}
return render(request, MINE_MINE_PAGE_ROUTE, context)


def add_house(request):
raise NotImplementedError


def load_cities(request):
country_id = request.GET.get('country')
cities = City.objects.filter(country_id=country_id).order_by('name')
context = {'cities': cities}
return render(request, GLOBAL_PAGE_CITY_DROPDOWN_ROUTE, context)


@login_required
def house_create(request):
errorMsg = ""
if request.method == 'POST':
try:
house_form = HouseCreationForm(request.POST)
if house_form.is_valid():
cleaned_data = house_form.cleaned_data
House.create_house(user=request.user,
public=cleaned_data['public'],
parent_profession_1=cleaned_data['parent_profession_1'],
parent_profession_2=cleaned_data['parent_profession_2'],
country=cleaned_data['country'],
city=cleaned_data['city'],
children=cleaned_data['children'],
income=cleaned_data['income'])
return HttpResponseRedirect('/../house')
else:
errorMsg = "Invalid form data"
return render(request, HOUSE_CREATE_ROUTE, {'form': house_form, 'msg': errorMsg})
except ValidationError as ex:
errorMsg = ex.message()
house_form = HouseCreationForm()
else:
house_form = HouseCreationForm()

return render(request, HOUSE_CREATE_ROUTE, {'form': house_form, 'msg': errorMsg})
9 changes: 9 additions & 0 deletions static/css/house_create.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#house_create{
align-content: center;
margin-top: 5%;
margin-bottom: auto;
}

#submit-button{
width: 100%;
}
19 changes: 0 additions & 19 deletions static/css/login.css

This file was deleted.

2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<a class="nav-link" href="/global">Global</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/login">Mine</a>
<a class="nav-link" href="/house">Mine</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/tips">Tips</a>
Expand Down
24 changes: 24 additions & 0 deletions templates/house_view/house_create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "base.html" %}
{% load crispy_forms_filters %} {% load static %}
{% load crispy_forms_tags %}
{% block content %}
<link rel="stylesheet" type="text/css" href="{% static 'css/house_create.css' %}"/>
<div id="house_create">
<div class="d-flex justify-content-center h-100">
<div class="card">
<div class="card-header">
<h3>Add your House information</h3>
</div>
<div class="card-body" style="color: rgb(0, 0, 0); background: rgba(252, 252, 252, 0.3); width: 500px;">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
{{ form|crispy }}
</fieldset>
<button class="btn btn-primary" id="submit-button" type="submit">Submit</button>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}
24 changes: 0 additions & 24 deletions templates/login/login.html

This file was deleted.

0 comments on commit 66eb61c

Please sign in to comment.