def edit_entry(request, id, template_name, page_title): # print 'edit_entry()::id=',id entry = get_object_or_404(PomEntry, id=id, author=request.user) # print 'edit_entry()::entry=',entry old_categorynames_list = [x.name for x in entry.categories.all()] # print 'edit_entry()::old_categorynames_list=',old_categorynames_list categorynames_as_one_string = get_category_names_as_one_string(old_categorynames_list) categorynamesdata = {"categories": categorynames_as_one_string} form_data = get_form_data(request) form = PomEntryEditForm(form_data, instance=entry) # try not allowing today,start,end times etc be edited catnameform = PomCategoryNameForm(form_data, initial=categorynamesdata) context = {"entryform": form, "categoryform": catnameform, "page_title": page_title} form_valid = form.is_valid() catnameform_valid = catnameform.is_valid() # print 'edit_entry()::catnameform_valid=',catnameform_valid # print 'edit_entry()::form_valid=',form_valid # print 'for errors=',form.errors # print 'edit_entry()::request.method=',request.method if request.method == "POST" and form_valid and catnameform_valid: # print 'edit_entry()::POST:today=',request.POST['today'] # print 'edit_entry()::POST:start_time=',request.POST['start_time'] # print 'edit_entry()::POST:end_time=',request.POST['end_time'] edited_entry = form.save() # print 'edit_entry()::edited_entry=',edited_entry catnames = catnameform.cleaned_data["categories"] # print 'edit_entry()::catnames=',catnames # new_categorynames_list=get_list_of_names(catnames) # newlyaddedcatnames=set(new_categorynames_list)-set(old_categorynames_list) # newlyaddedcatnames=list(newlyaddedcatnames) # removedcatnames=set(old_categorynames_list)-set(new_categorynames_list) # removedcatnames=list(removedcatnames) # print 'edit_entry()::removedcatnames=',removedcatnames # for each name in newlyaddedcatnames,get or create category object, add this user in its users field # if newlyaddedcatnames: # newlyaddedcats=get_categories(newlyaddedcatnames) # add_user_to_categories(newlyaddedcats,request.user) # #for each name in removedcatnames ,get category object ,remove this user from its users field # if removedcatnames: # removedcats=get_categories(removedcatnames) # #print 'edit_entry()::removedcats=',removedcats # remove_user_from_categories(removedcats,request.user) cats = get_categories(request.user, get_list_of_names(catnames)) edited_entry.categories = cats edited_entry.save() remove_archive_index_key_from_cache(request) # remove archive index key remove_index_key_from_cache(request) # check this needed? remove_current_month_chart_key_from_cache(request) remove_lone_categories(request.user) # update to remove cats with no entries return redirect("pomlog_entry_archive_index") return custom_render(request, context, template_name)
def add_new_entry(request, template_name, page_title): form_data = get_form_data(request) form = PomEntryDescForm(form_data) difficulty_form = PomEntryDifficultyForm(form_data) # added difficulty catnameform = PomCategoryNameForm(form_data) errorlist = [] context = { "page_title": page_title, "difficulty_form": difficulty_form, "entryform": form, "categoryform": catnameform, "errorlist": errorlist, "start_time_string": "", "stop_time_string": "", "form_errors": False, } if request.method == "POST": start_time_string = request.POST[u"timerstarted"] stop_time_string = request.POST[u"timerstopped"] context["start_time_string"] = start_time_string context["stop_time_string"] = stop_time_string if form.is_valid() and catnameform.is_valid() and difficulty_form.is_valid(): desc = form.cleaned_data["description"] difficulty = difficulty_form.cleaned_data["difficulty"] # added difficulty # print 'cleaned_data difficulty=',difficulty,'type=',type(difficulty) # print 'start_time_string=',start_time_string,len(start_time_string) # print 'stop_time_string=',stop_time_string,len(stop_time_string) if len(start_time_string) == 0 or len(stop_time_string) == 0: # print 'starttime and endtime cannot be empty' errorlist.append("starttime and endtime cannot be empty") return custom_render(request, context, template_name) start_time = long(start_time_string) stop_time = long(stop_time_string) if not (start_time < stop_time): # need to put this in errors errorlist.append("starttime should be less than endtime") return custom_render(request, context, template_name) try: start_ttpl, stop_ttpl = get_timetuples(start_time, stop_time) start_timeval = datetime.time(*start_ttpl) stop_timeval = datetime.time(*stop_ttpl) except ValueError: errorlist.append("format of time entries not correct") return custom_render(request, context, template_name) newentry = PomEntry(description=desc) newentry.save() newentry.start_time = start_timeval newentry.end_time = stop_timeval catnames = catnameform.cleaned_data["categories"] catnames = get_list_of_names(catnames) cats = get_categories(request.user, catnames) # added user as creator """ for x in cats: if request.user not in x.users.all(): x.users.add(request.user)#need to append, not assign """ newentry.categories = cats newentry.author = request.user newentry.difficulty = difficulty # added difficulty # print 'before save with diff=',difficulty newentry.save() remove_index_key_from_cache(request) # removes index_key from cache remove_archive_index_key_from_cache(request) remove_current_month_chart_key_from_cache(request) # print 'newentry saved with diff=',difficulty return redirect("pomlog_entry_archive_index") else: context["form_errors"] = True return custom_render(request, context, template_name) elif request.method == "GET": return custom_render(request, context, template_name)