def set_type_default(request): if request.method == "POST": form = ActivityCreateForm(request.POST) if form.is_valid(): preference = UserActivityPreference.objects.get(user=request.user, activity_type=form.cleaned_data['activity_type']) preference.default_name = form.cleaned_data['name'] preference.default_description = form.cleaned_data['description'] preference.default_start_time = form.cleaned_data['start_time'].time() #preference.default_assembling_time = (form.cleaned_data['start_time'] - form.cleaned_data['assembling_time']).seconds / 60 preference.default_duration = str((form.cleaned_data['end_time'] - form.cleaned_data['start_time']).seconds / float(3600)) preference.default_activity_place = form.cleaned_data['activity_place'] #preference.default_assembling_place = form.cleaned_data['assembling_place'] preference.save() return HttpResponse('已设为默认') return HttpResponse('error!')
def edit(request, activity_id):#only the invitor can modify it user = request.user activity = Activity.objects.get(pk=activity_id) if activity.invitor == user: if request.method == 'POST': form = ActivityCreateForm(request.POST, files=request.FILES, instance=activity, invitor=activity.invitor) if form.is_valid(): form.save() return redirect('/activity/detail/' + activity_id) else: form = ActivityCreateForm(instance=activity, invitor=activity.invitor) return render_to_response('activity/edit.html', {'form': form, 'activity_id': activity_id,}, context_instance=RequestContext(request)) else: return HttpResponse('you are not the invitor!')
def create(request): if request.method == 'POST': form = ActivityCreateForm(request.POST, files=request.FILES) if form.is_valid(): new_activity = form.save(False) new_activity.invitor = request.user new_activity.save() if not new_activity.start_time: for time in form.candidate_times: CandidateTime.objects.create(time=time, activity=new_activity) return redirect('/activity/detail/' + str(new_activity.id)) else: try: year = int(request.GET['year']) month = int(request.GET['month']) day = int(request.GET['day']) start_time = datetime.datetime(year, month, day) form = ActivityCreateForm(invitor=request.user, initial={'start_time':start_time}) except: form = ActivityCreateForm(invitor=request.user) return render_to_response('activity/create.html', {'form': form,}, context_instance=RequestContext(request))