def create(request): if request.method == 'POST': # save new evento title = request.POST['title'] description = request.POST['description'] date_start = request.POST['date_start'] date_end = request.POST['date_end'] lat = request.POST['lat'] lng = request.POST['lng'] #image = request.FILES['image'] event = Event(title=title) event.last_update = datetime.datetime.now() event.description = description event.date_start = datetime.datetime.strptime(date_start, '%d/%m/%Y %H:%M') if not date_end: event.date_end = None else: event.date_end = datetime.datetime.strptime(date_end, '%d/%m/%Y %H:%M') event.location = [float(lat),float(lng)] user_id = request.session.get('userid') user = Account.objects(id=user_id)[0] event.user = user event.user_going = [user] #im = Image.open(image) #event.image.put(open(im)) event.save() Account.objects(id=user_id).update_one(push__event_going=event) return HttpResponseRedirect(reverse('event-list')) return render_to_response('event/event_create.html',{ 'request':request}, context_instance=RequestContext(request))
def submit_event(request): if request.method == 'POST': if request.POST.has_key('abort'): return HttpResponseRedirect('/') form = EventForm(request.POST) if form.is_valid(): event = Event() event.name = form.cleaned_data['name'] event.desc = form.cleaned_data['desc'] event.url = form.cleaned_data['url'] event.date_start = form.cleaned_data['date_start'] event.date_end = form.cleaned_data['date_end'] event.published = False event.submit_ip = request.META['REMOTE_ADDR'] event.save() return HttpResponseRedirect('/events/submit/success/') else: form = EventForm() return render_to_response('events/submit_event.html', {'form': form}, context_instance=RequestContext(request))
def rest(request, event_id=None): if request.method == 'POST': title = request.POST['title'] description = request.POST['description'] date_start = request.POST['date_start'] date_end = request.POST.get('date_end', None) lat = request.POST['lat'] lng = request.POST['lng'] #image = request.FILES['image'] event = Event(title=title) event.last_update = datetime.datetime.now() event.description = description event.date_start = datetime.datetime.strptime(date_start, '%d/%m/%Y %H:%M') if date_end: event.date_end = datetime.datetime.strptime(date_end, '%d/%m/%Y %H:%M') event.location = [float(lat),float(lng)] user_id = request.session.get('userid') user = Account.objects(id=user_id)[0] event.user = user event.user_going = [user] #im = Image.open(image) #event.image.put(open(im)) event.save() Account.objects(id=user_id).update_one(push__event_going=event) data = json.dumps({'status':'ok'}) elif event_id: user = Account.objects(id=request.session.get('userid'))[0] event = Event.objects(id=event_id, user=user)[0] if request.method == 'PUT': # update field values and save to mongo data = JSONParser().parse(request) title = data['title'] description = data['description'] date_start = data['date_start'] date_end = data['date_end'] lat = data['lat'] lng = data['lng'] #image = request.FILES['image'] event.title = title event.last_update = datetime.datetime.now() event.description = description event.date_start = datetime.datetime.strptime(date_start, '%d/%m/%Y %H:%M') if not date_end: event.date_end = None else: event.date_end = datetime.datetime.strptime(date_end, '%d/%m/%Y %H:%M') event.location = [float(lat),float(lng)] event.user = user #event.image = request.FILES['image'] event.save() data = json.dumps({'status':'ok'}) elif request.method == 'GET': data = { 'id': str(event.id), 'title': event.title, 'description': event.description, 'location': { 'lat': event.location[0], 'lng': event.location[1] }, 'date_start': str(event.date_start.strftime('%d/%m/%Y %H:%M')) if event.date_start else '', 'date_end': str(event.date_end.strftime('%d/%m/%Y %H:%M')) if event.date_end else '' } data = json.dumps(data) elif request.method == 'DELETE': Account.objects().update(pull__event_going=event) Account.objects().update(pull__event_maybe=event) event.delete() data = json.dumps({'status':'ok'}) else: data = json.dumps({'status':'error'}) return HttpResponse(data,content_type='application/json')