Exemplo n.º 1
0
def locations(request):
    def _location_context(location):
        return {'id': str(location._id), 'place_name': l.place_name, 'postcode': l.label}
    errors = []
    data = []
    response_code = 200

    match = request.REQUEST.get('match')
    callback = request.REQUEST.get('callback')

    if match is not None:
        if len(match) > 2:
            data = [_location_context(l)
                for l in Location.objects(Q(place_name__icontains=match) | Q(label__icontains=match))]
        else:
            response_code = 10
            errors.append('Param \'match\' must be greater than 2 characters. You sent \'%s\'' % match)
    else:
        data = [_location_context(l)
            for l in Location.objects()]

    return JsonResponse(
        errors=errors and {'code': response_code, 'message': '. '.join(errors)} or {},
        data=data,
        callback=callback,
    )
Exemplo n.º 2
0
def resource_edit(request, object_id, template='depot/resource_edit.html'):
    """ edits an existing resource. Uses a wizard-like approach, so checks resource.collection_status
        and hands off to resource_* function handler
    """
    UPDATE_LOCS = 'Update locations'
    UPDATE_TAGS = 'Update tags'
    
    resource = get_one_or_404(id=ObjectId(object_id), user=request.user, perm='can_edit')

    # doc = ''
    # places = None
    template_info = _template_info(request.REQUEST.get('popup', ''))

    if request.method == 'POST':
        result = request.POST.get('result', '') # or request.POST.get('result', '')
        if result == 'Cancel':
            return resource_edit_complete(request, resource, template_info)
        resourceform = ShortResourceForm(request.POST, instance=resource)
        eventform = EventForm(request.POST, instance=resource.calendar_event)
        locationform = LocationUpdateForm(request.POST, instance=resource)
        # shelflifeform = ShelflifeForm(request.POST, instance=resource)
        
        if resourceform.is_valid() and locationform.is_valid() and eventform.is_valid():
            acct = get_account(request.user.id)

            # Location
            new_loc = locationform.cleaned_data['new_location'].split(',')
            print new_loc
            resource.locations = Location.objects(id__in=new_loc)
            #resource.add_location_from_name(locationform.cleaned_data['new_location'])
            #resource.save(author=acct, reindex=True)

            # Dates
            event_start = eventform.cleaned_data['start']
            if event_start:
                resource.calendar_event = CalendarEvent(start=event_start, end=eventform.cleaned_data['end'])
                # print 'event_start', event_start
                # print 'event_finish', eventform.cleaned_data['end']
            else:
                resource.calendar_event = None
            resource = resourceform.save()
            
            try:
                resource.save(author=acct, reindex=True)
                return resource_edit_complete(request, resource, template_info)
            except OperationError:
                pass

    else:
        resourceform = ShortResourceForm(instance=resource)
        locationform = LocationUpdateForm(instance=resource)
        eventform = EventForm(instance=resource.calendar_event)
        # if not resource.locations:
        #     doc = resource.uri
        # shelflifeform = ShelflifeForm(instance=resource)
    
    return render_to_response(template,
        RequestContext( request, { 'template_info': template_info, 'object': resource,
            'resourceform': resourceform, 'locationform': locationform, 'eventform': eventform, #'places': places,
            # 'tagsform': tagsform, #'shelflifeform': shelflifeform,
            'UPDATE_LOCS': UPDATE_LOCS, 'UPDATE_TAGS': UPDATE_TAGS  }))
Exemplo n.º 3
0
 def tearDown(self):
     Resource.drop_collection()
     Location.drop_collection()
Exemplo n.º 4
0
 def tearDown(self):
     Resource.drop_collection()
     Location.drop_collection()
Exemplo n.º 5
0
def resource_edit(request, object_id, template='depot/resource_edit.html'):
    """ edits an existing resource. Uses a wizard-like approach, so checks resource.collection_status
        and hands off to resource_* function handler
    """
    UPDATE_LOCS = 'Update locations'
    UPDATE_TAGS = 'Update tags'

    resource = get_one_or_404(id=ObjectId(object_id),
                              user=request.user,
                              perm='can_edit')

    # doc = ''
    # places = None
    template_info = _template_info(request.REQUEST.get('popup', ''))

    if request.method == 'POST':
        result = request.POST.get('result',
                                  '')  # or request.POST.get('result', '')
        if result == 'Cancel':
            return resource_edit_complete(request, resource, template_info)
        resourceform = ShortResourceForm(request.POST, instance=resource)
        eventform = EventForm(request.POST, instance=resource.calendar_event)
        locationform = LocationUpdateForm(request.POST, instance=resource)
        # shelflifeform = ShelflifeForm(request.POST, instance=resource)

        if resourceform.is_valid() and locationform.is_valid(
        ) and eventform.is_valid():
            acct = get_account(request.user.id)

            # Location
            new_loc = locationform.cleaned_data['new_location'].split(',')
            print new_loc
            resource.locations = Location.objects(id__in=new_loc)
            #resource.add_location_from_name(locationform.cleaned_data['new_location'])
            #resource.save(author=acct, reindex=True)

            # Dates
            event_start = eventform.cleaned_data['start']
            if event_start:
                resource.calendar_event = CalendarEvent(
                    start=event_start, end=eventform.cleaned_data['end'])
                # print 'event_start', event_start
                # print 'event_finish', eventform.cleaned_data['end']
            else:
                resource.calendar_event = None
            resource = resourceform.save()

            try:
                resource.save(author=acct, reindex=True)
                return resource_edit_complete(request, resource, template_info)
            except OperationError:
                pass

    else:
        resourceform = ShortResourceForm(instance=resource)
        locationform = LocationUpdateForm(instance=resource)
        eventform = EventForm(instance=resource.calendar_event)
        # if not resource.locations:
        #     doc = resource.uri
        # shelflifeform = ShelflifeForm(instance=resource)

    return render_to_response(
        template,
        RequestContext(
            request,
            {
                'template_info': template_info,
                'object': resource,
                'resourceform': resourceform,
                'locationform': locationform,
                'eventform': eventform,  #'places': places,
                # 'tagsform': tagsform, #'shelflifeform': shelflifeform,
                'UPDATE_LOCS': UPDATE_LOCS,
                'UPDATE_TAGS': UPDATE_TAGS
            }))