Skip to content

Commit

Permalink
#99 refactored checklist update view
Browse files Browse the repository at this point in the history
  • Loading branch information
StegSchreck committed Dec 17, 2016
1 parent a923f2c commit a3075d8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 30 deletions.
14 changes: 11 additions & 3 deletions core/checklist_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

from core.views.checklist_views import CreateChecklistItemView, DeleteChecklistItemView, \
GetChecklistItemsForTodayView, GetChecklistItemsView, \
UpdateChecklistItemView, UpdateChecklistPriorityView
UpdateChecklistItemNameView, UpdateChecklistPriorityView, \
UpdateChecklistItemStatusView, UpdateChecklistItemConditionView

app_name = 'checklist'


urlpatterns = [
url(r'^get_checklist_items/?$', GetChecklistItemsView.as_view(),
name='get_checklist_items'),
url(r'^get_checklist_items_for_today/?$', GetChecklistItemsForTodayView.as_view(),
name='get_checklist_items_for_today'),
url(r'^add_checklist_item/?$', CreateChecklistItemView.as_view(),
name='add_checklist_item'),
url(r'^update_checklist_item/?$', UpdateChecklistItemView.as_view(),
name='update_checklist_item'),
url(r'^delete_checklist_item/?$', DeleteChecklistItemView.as_view(),
name='delete_checklist_item'),

url(r'^update_checklist_item_name/?$', UpdateChecklistItemNameView.as_view(),
name='update_checklist_item_name'),
url(r'^update_checklist_item_condition/?$', UpdateChecklistItemConditionView.as_view(),
name='update_checklist_item_condition'),
url(r'^update_checklist_item_status/?$', UpdateChecklistItemStatusView.as_view(),
name='update_checklist_item_status'),
url(r'^update_checklist_priority/?$', UpdateChecklistPriorityView.as_view(),
name='update_checklist_priority'),
]
9 changes: 5 additions & 4 deletions core/static/core/js/checklist_settings_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ $('document').ready( function (){
checklist_item_id: elem.closest('.checklist_item_container').attr('id'),
checklist_item_name: elem.val()
};
$.post("/checklist/update_checklist_item", params);
$.post("/checklist/update_checklist_item_name", params);
elem.closest('.checklist_item_container').find('.checklist_item_saved').removeClass('invisible');
}
$('#checklist_items').on('focusout', '.checklist_item_name', function() {
Expand Down Expand Up @@ -126,7 +126,8 @@ $('document').ready( function (){
checklist_item_id: elem.closest('.checklist_item_container').attr('id'),
checklist_item_matchdays: matchdays_comma_seperated
};
$.post("/checklist/update_checklist_item", params);
$.post("/checklist/update_checklist_item_condition", params);
console.log(params);
elem.closest('.checklist_item_container').find('.checklist_item_saved').removeClass('invisible');
}
$('#checklist_items').on('focusout', '.checklist_item_matchdays', function() {
Expand Down Expand Up @@ -157,7 +158,7 @@ $('document').ready( function (){
checklist_item_id: elem.closest('.checklist_item_container').attr('id'),
checklist_item_matchday_pattern: matchday_pattern
};
$.post("/checklist/update_checklist_item", params);
$.post("/checklist/update_checklist_item_condition", params);
elem.closest('.checklist_item_container').find('.checklist_item_saved').removeClass('invisible');
}
$('#checklist_items').on('focusout', '.checklist_item_matchday_pattern', function() {
Expand Down Expand Up @@ -220,7 +221,7 @@ $('document').ready( function (){
params['checklist_item_everyday'] = true;
}

$.post("/checklist/update_checklist_item", params);
$.post("/checklist/update_checklist_item_condition", params);
});


Expand Down
2 changes: 1 addition & 1 deletion core/static/core/js/checklist_user_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $('document').ready( function (){
checklist_item_id: $(this).attr('id'),
checklist_item_checked: checklistItemGotChecked
};
$.post("/checklist/update_checklist_item", params);
$.post("/checklist/update_checklist_item_status", params);

if (checklistItemGotChecked) {
checklistItem.removeClass('glyphicon-unchecked');
Expand Down
16 changes: 8 additions & 8 deletions core/tests/unit/views/test_checklist_settings_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_create_standard_checklist_item(self):

def test_update_checklist_item_name(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_name'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_name': 'do even more unit tests'
})
Expand All @@ -73,7 +73,7 @@ def test_update_checklist_item_name(self):

def test_update_checklist_item_home_match(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_condition'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_home_match': True
})
Expand All @@ -95,7 +95,7 @@ def test_update_checklist_item_home_match(self):

def test_update_checklist_item_matchday(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_condition'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_matchdays': '4'
})
Expand All @@ -117,7 +117,7 @@ def test_update_checklist_item_matchday(self):

def test_update_checklist_item_matchdays(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_condition'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_matchdays': '3,33'
})
Expand All @@ -139,7 +139,7 @@ def test_update_checklist_item_matchdays(self):

def test_update_checklist_item_matchday_pattern(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_condition'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_matchday_pattern': 2
})
Expand All @@ -161,7 +161,7 @@ def test_update_checklist_item_matchday_pattern(self):

def test_update_checklist_item_everyday(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_condition'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_everyday': True
})
Expand All @@ -182,7 +182,7 @@ def test_update_checklist_item_everyday(self):

def test_update_checklist_item_checked(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_status'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_checked': 'true'
})
Expand All @@ -199,7 +199,7 @@ def test_update_checklist_item_checked(self):

def test_update_checklist_item_unchecked(self):
self.client.login(username='temporary', password='temporary')
response = self.client.post(reverse('core:checklist:update_checklist_item'),
response = self.client.post(reverse('core:checklist:update_checklist_item_status'),
{'checklist_item_id': self.checklist_item.id,
'checklist_item_checked': 'false'
})
Expand Down
60 changes: 46 additions & 14 deletions core/views/checklist_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,42 +92,74 @@ def post(self, request, *args, **kwargs):


@method_decorator(login_required, name='dispatch')
class UpdateChecklistItemView(JSONResponseMixin, View):
class UpdateChecklistItemNameView(JSONResponseMixin, View):
def post(self, request, *args, **kwargs):
checklist_item_id = request.POST.get('checklist_item_id')
checklist_item_name = request.POST.get('checklist_item_name')

checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id)

if checklist_item:
checklist_item.name = checklist_item_name
checklist_item.save()
return self.render_json_response({'success': True})

return self.render_json_response({'success': False})


@method_decorator(login_required, name='dispatch')
class UpdateChecklistItemStatusView(JSONResponseMixin, View):
def post(self, request, *args, **kwargs):
checklist_item_id = request.POST.get('checklist_item_id')
checklist_item_checked = request.POST.get('checklist_item_checked')

checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id)

if checklist_item:
if checklist_item_checked == 'true':
checklist_item.last_checked_on_matchday = Matchday.get_current()
elif checklist_item_checked == 'false':
checklist_item.last_checked_on_matchday = None
checklist_item.save()
return self.render_json_response({'success': True})

return self.render_json_response({'success': False})


@method_decorator(login_required, name='dispatch')
class UpdateChecklistItemConditionView(JSONResponseMixin, View):
def post(self, request, *args, **kwargs):
checklist_item_id = request.POST.get('checklist_item_id')
checklist_item_matchdays = request.POST.get('checklist_item_matchdays')
checklist_item_matchday_pattern = request.POST.get('checklist_item_matchday_pattern')
checklist_item_home_match = request.POST.get('checklist_item_home_match')
checklist_item_everyday = request.POST.get('checklist_item_everyday')
checklist_item_checked = request.POST.get('checklist_item_checked')
print(checklist_item_id)
print(checklist_item_matchdays)
print(checklist_item_matchday_pattern)
print(checklist_item_home_match)
print(checklist_item_everyday)

checklist_item = ChecklistItem.objects.get(checklist__user=request.user, id=checklist_item_id)

if checklist_item:
self._handle_checklist_item_update(checklist_item, checklist_item_checked, checklist_item_everyday,
checklist_item_home_match, checklist_item_matchday_pattern,
checklist_item_matchdays, checklist_item_name)
self._handle_checklist_item_update(checklist_item, checklist_item_everyday, checklist_item_home_match,
checklist_item_matchday_pattern, checklist_item_matchdays)
return self.render_json_response({'success': True})

return self.render_json_response({'success': False})

def _handle_checklist_item_update(self, checklist_item, checklist_item_checked, checklist_item_everyday,
def _handle_checklist_item_update(self, checklist_item, checklist_item_everyday,
checklist_item_home_match, checklist_item_matchday_pattern,
checklist_item_matchdays, checklist_item_name):
if checklist_item_name:
checklist_item.name = checklist_item_name
elif checklist_item_matchdays:
checklist_item_matchdays):
if checklist_item_matchdays:
self._update_checklist_item_condition(checklist_item, checklist_item_matchdays, None, False)
elif checklist_item_matchday_pattern:
self._update_checklist_item_condition(checklist_item, None, checklist_item_matchday_pattern, False)
elif checklist_item_home_match:
self._update_checklist_item_condition(checklist_item, None, None, True)
elif checklist_item_everyday:
self._update_checklist_item_condition(checklist_item, None, None, False)
elif checklist_item_checked == 'true':
checklist_item.last_checked_on_matchday = Matchday.get_current()
elif checklist_item_checked == 'false':
checklist_item.last_checked_on_matchday = None
checklist_item.save()

@staticmethod
Expand Down

0 comments on commit a3075d8

Please sign in to comment.