Exemplo n.º 1
0
 def distinct_streets(self, ds):
     filters = {}
     if ds == 'true':
         filters['distinct_streets'] = {
             'order_by': ['municipality', 'street'],
             'distinct': ['municipality', 'street']}
     elif ds != 'false':
         raise ImmediateHttpResponse(
             response=HttpBadRequest(
                 content="{\"error\": \"If given, the option "
                         "distinct_streets must be either 'true' or "
                         "'false'.\"}",
                 content_type='application/json'))
     return filters
Exemplo n.º 2
0
def add_restaurant(request):
    if request.method == "GET":
        form = RestaurantForm(request=request)
        return render(request, "restaurants/addRestaurant.html", {
            "form": form,
            "categories": Cuisine.objects.all()
        })
    elif request.method == "POST":
        rest_data = request.POST.copy()
        # rest_data['cuisines'] = [int(rest_data['cuisines']), ]
        form = RestaurantForm(rest_data, request=request)
        if form.is_valid():
            new_restaurant = form.save()
            return render(request, 'restaurants/submitted.html',
                          {"restaurant_slug": new_restaurant.slug})
            # return redirect(reverse('venues.views.venuess.restaurant_by_slug', args=[new_restaurant.slug]))
        elif not request.is_ajax():
            return render(request, "restaurants/addRestaurant.html", {
                "form": form,
                "categories": Cuisine.objects.all()
            })
        return HttpBadRequest(form.errors())
    return HttpBadRequest()
Exemplo n.º 3
0
    def bad_request(cls, msg, extend_dict=None, err_code=None):
        error_dict = {
            'status': 'error',
            'message': msg,
        }
        if extend_dict:
            error_dict.update(extend_dict)

        if err_code is not None:
            error_dict['err_code'] = err_code

        msg_json = json.dumps(error_dict)

        return HttpBadRequest(msg_json, content_type='application/json')
Exemplo n.º 4
0
    def obj_update(self, bundle, **kwargs):
        # FIXME: I'm not exactly sure how cached cached_object_get is -- should
        # we be explicitly getting a fresh one?  I'm just following what the ModelResource
        # obj_update does - jcs
        bundle.obj = self.cached_obj_get(
            bundle, **self.remove_api_resource_names(kwargs))
        volume = bundle.data

        # Check that we're not trying to modify a Volume that is in
        # used by a target
        try:
            Volume.get_unused_luns(Volume.objects).get(id=volume["id"])
        except Volume.DoesNotExist:
            raise AssertionError("Volume %s is in use!" % volume["id"])

        lun = get_object_or_404(Volume, id=volume["id"])
        node_ids = [node["id"] for node in volume["nodes"]]
        host_ids = set(
            lun.volumenode_set.filter(id__in=node_ids).values_list("host_id",
                                                                   flat=True))

        # Sanity-check the primary/failover relationships and save if OK
        if not any(
                host_ids.issubset(host.id for host in cluster.peers)
                for cluster in HaCluster.all_clusters()):
            error_msg = "Attempt to set primary/secondary VolumeNodes across HA clusters for Volume %s:%s\n" % (
                lun.id,
                lun.label,
            )
            error_msg += "\nVolume Node Hosts %s\n" % ", ".join([
                str(host)
                for host in ManagedHost.objects.filter(id__in=host_ids)
            ])
            error_msg += "\nKnown HA Clusters %s\n" % ", ".join([
                "(%s)" % ", ".join([str(host) for host in cluster.peers])
                for cluster in HaCluster.all_clusters()
            ])

            raise ImmediateHttpResponse(response=HttpBadRequest(error_msg))
        # Apply use,primary values from the request
        for node in volume["nodes"]:
            lun.volumenode_set.filter(id=node["id"]).update(
                primary=node["primary"], use=node["use"])

        # Clear use, primary on any nodes not in this request
        lun.volumenode_set.exclude(id__in=node_ids).update(primary=False,
                                                           use=False)

        return bundle
Exemplo n.º 5
0
    def is_valid(self, bundle, request=None):
        # prepare user id
        user_id = None
        if hasattr(request, 'user') and request.user.is_authenticated():
            user_id = request.user.id

        # prepare event id
        event_id = None
        event_uri = bundle.data.get('event')
        if event_uri:
            try:
                view, args, kwargs = resolve(event_uri)
                event_id = kwargs.get('pk')
            except Resolver404:
                raise NotFound("The URL provided '%s' was not a link to a valid resource." % event_uri)

        # prepare form with data
        form = self._meta.validation.form_class(data=dict(
            user=user_id,
            event=event_id,
            action=bundle.data.get('action', '').upper()
        ))

        # non conventional form processing
        if form.is_valid():
            instance = form.instance
            try:
                event_action = EventAction.objects.get(
                    user=instance.user, event=instance.event
                )
            except EventAction.DoesNotExist:
                event_action = instance
                event_action.save()
            else:
                if not event_action.action == instance.action:
                    event_action.action = instance.action
                    event_action.save()
            bundle.obj = event_action

        # error handling
        else:
            if request:
                desired_format = self.determine_format(request)
            else:
                desired_format = self._meta.default_format

            serialized = self.serialize(request, form.errors, desired_format)
            response = HttpBadRequest(content=serialized, content_type=build_content_type(desired_format))
            raise ImmediateHttpResponse(response=response)
Exemplo n.º 6
0
    def obj_delete(self, bundle, **kwargs):

        thread = self.get_thread(**kwargs)

        try:
            reason = bundle.request.GET['reason']
        except KeyError:
            raise ImmediateHttpResponse(response=HttpBadRequest())

        if not thread.deleted:
            user = bundle.request.user
            reason = MessageSuppression(raison=reason, operateur=user)
            reason.save()
            thread.suppression = reason
            thread.save()
Exemplo n.º 7
0
    def post_list(self, request, **kwargs):

        # First and foremost, ensure that the user is logged in and valid.
        logged_in_user = request.user

        if logged_in_user.is_anonymous():
            return HttpUnauthorized()

        # Convert new study data into a python dictionary. If we receive some
        # error converting the data into a dictionary, return a HttpBadRequest.
        try:
            params = json.loads(request.body)
        except Exception:
            return HttpBadRequest()

        # Validate the data passed into the form
        form = NewStudyForm(params)
        if not form.is_valid():
            return HttpBadRequest()

        new_study = form.save(user=logged_in_user)

        response_data = {'success': True, 'id': new_study.id}
        return self.create_response(request, response_data)
Exemplo n.º 8
0
    def obj_update(self, bundle, **kwargs):
        data = bundle.data
        bundle.obj = Request.objects.get(id=bundle.data['id'])
        can_edit = bundle.request.user.has_perm(
            Request.get_permission_name('edit'), bundle.obj)
        if not can_edit:
            raise ImmediateHttpResponse(
                HttpBadRequest(
                    "It appears you don't have permission to change this request."
                ))

        if 'status' in bundle.data:
            status = bundle.data['status']
            del bundle.data['status']
            if status:
                bundle.obj.set_status(status)
        attachments = []
        if 'attachments' in bundle.data:
            for atch in data['attachments']:
                attachment = Attachment.objects.get(id=atch['id'])
                attachments.append(attachment)
            bundle.obj.attachments = attachments
            del data['attachments']
        for field in [
                'title', 'free_edit_body', 'private', 'text', 'phone_contact',
                'prefer_electornic', 'max_cost', 'fee_waiver'
        ]:
            if field in data:
                try:
                    setattr(bundle.obj, field, data[field])
                except Exception as e:
                    logger.info('error setting field %s e=%s' % (field, e))
            else:
                logger.info('field %s not allowed' % field)
        contacts = associate_contacts(bundle, data)

        bundle.obj.contacts = contacts
        bundle.obj.save()
        #bundle.data['can_send'] = bundle.obj.can_send

        if 'generate_pdf' in bundle.data:
            bundle.obj.create_pdf_body()

        if 'do_send' in bundle.data and bundle.data['do_send']:
            #obj sent property will reflect whether it has been sent
            bundle.obj.send()
            #bundle.data['sent'] = bundle.obj.sent
        return bundle
Exemplo n.º 9
0
    def apply_filters(self, request, filters=None):
        objects = super(VolumeResource, self).apply_filters(request, filters)

        try:
            category = request.GET['category']
            if not category in ['unused', 'usable', None]:
                raise ImmediateHttpResponse(response=HttpBadRequest())
            if category == 'unused':
                objects = Volume.get_unused_luns(objects)
            elif category == 'usable':
                objects = Volume.get_usable_luns(objects)
        except KeyError:
            # Not filtering on category
            pass

        try:
            try:
                objects = objects.filter(
                    Q(volumenode__primary=request.GET['primary'])
                    & Q(volumenode__host__id=request.GET['host_id'])
                    & Q(volumenode__not_deleted=True)).distinct()
            except KeyError:
                # Not filtering on primary, try just host_id
                objects = objects.filter(
                    Q(volumenode__host__id=request.GET['host_id'])
                    & Q(volumenode__not_deleted=True)).distinct()
        except KeyError:
            # Not filtering on host_id
            pass

        try:
            try:
                fs = ManagedFilesystem.objects.get(
                    pk=request.GET['filesystem_id'])
            except ManagedFilesystem.DoesNotExist:
                objects = objects.filter(
                    id=-1
                )  # No filesystem so we want to produce an empty list.
            else:
                objects = objects.filter((
                    Q(managedtarget__managedmdt__filesystem=fs)
                    | Q(managedtarget__managedost__filesystem=fs))
                                         | Q(managedtarget__id=fs.mgs.id))
        except KeyError:
            # Not filtering on filesystem_id
            pass

        return objects
Exemplo n.º 10
0
    def _check_data(self):
        """See if all mandatory data is there"""
        # check if all mandatory keys are there
        for key in [k for k in self.mandatory_keys\
                    if k not in ('revision')]:
            if not key in self.data.keys():
                error_text = u"You need to provide key: {0}".format(key)
                logging.error(error_text)
                raise ImmediateHttpResponse(
                    response=HttpBadRequest(error_text))
            # check for data
            elif not self.data[key]:
                error_text = 'Value for key {0} is empty.'.format(key)
                logging.error(error_text)
                raise ImmediateHttpResponse(
                    response=HttpBadRequest(error_text))

        # Check that the Environment exists
        try:
            self.obj.environment = EnvironmentResource().get_via_uri(
                self.data['environment'])
        except Environment.DoesNotExist:
            error_text = 'Environment: {0} not found in database.'.format(
                self.data['environment'])
            logging.error(error_text)
            raise ImmediateHttpResponse(response=HttpBadRequest(error_text))
        except Exception as e:
            error_text = ('Error while looking up Environment: '
                          '{0}, {1}.'.format(self.data['environment'], e))
            logging.error(error_text)
            raise ImmediateHttpResponse(response=HttpBadRequest(error_text))
        # check optional data
        for key in [k for k in self.optional_keys \
                    if k not in ('date',)]:
            if key in self.data.keys():
                try:
                    self.data[key] = float(self.data[key])
                except ValueError:
                    error_text = u"{0} cannot be casted to float.".format(
                        self.data[key])
                    logging.error(error_text)
                    raise ImmediateHttpResponse(
                        response=HttpBadRequest(error_text))

        if 'date' in self.data.keys():
            #FIXME (a8): make that more robust for different json date formats
            try:
                self.data['date'] = datetime.strptime(self.data['date'],
                                                      self.DATETIME_FORMAT)
            except ValueError:
                error_text = u"Cannot convert date {0} into datetime.".format(
                    self.data['date'])
                logging.error(error_text)
                raise ImmediateHttpResponse(
                    response=HttpBadRequest(error_text))
Exemplo n.º 11
0
    def obj_update(self, bundle, pk='', **kwargs):
        p_id = pk.split("-")[-1]
        product = Product.objects.filter(id=int(p_id))
        if product[0].owner == bundle.request.user:
            picture_data = bundle.data.pop("picture", None)
            day_price_data = bundle.data.pop("price", None)
            prices = bundle.data.pop("prices", None)
            bundle = super(ProductResource, self).obj_update(bundle,
                                                             pk=p_id,
                                                             **kwargs)

            self.add_prices(bundle, prices)
            self._obj_process_fields(bundle.obj, picture_data, day_price_data)
            return bundle
        else:
            raise ImmediateHttpResponse(response=HttpBadRequest())
Exemplo n.º 12
0
 def alter_list_data_to_serialize(self, request, data):
     user_id = request.user.id
     if not user_id:
         raise ImmediateHttpResponse(HttpBadRequest("Please login first"))
     
     tags = Tag.objects.raw("""SELECT tag.id, count(*) as 
                             count from uss_tag as tag JOIN 
                             uss_urldesc_tags as urldesc_tags on 
                             tag.id =urldesc_tags.tag_id where 
                             tag.user_id=%s group by tag.id""", [user_id])
     tag_map = {}
     for tag in tags:
         tag_map[tag.id] = tag.count
     objects = data['objects'] 
     for obj in objects:
         obj.data["count"] = tag_map.get(obj.data["id"], 0) 
     return data    
Exemplo n.º 13
0
    def obj_create(self, bundle, **kwargs):

        from dateutil import parser

        is_offline_booking = bool(bundle.data.pop("is_offline_booking", False))

        require_keys(["product", "started_at", "ended_at"], bundle.data)
        product = Product.objects.get(
            pk=int(bundle.data["product"].split('/')[-2]))
        started_at = parser.parse(unquote(bundle.data["started_at"]))
        ended_at = parser.parse(unquote(bundle.data["ended_at"]))
        unit, total_amount = product.calculate_price(started_at, ended_at)
        domain = Site.objects.get_current().domain
        protocol = "https" if USE_HTTPS else "http"
        state = "authorized" if product.owner == bundle.request.user else "authorizing"

        bundle = super(BookingResource,
                       self).obj_create(bundle,
                                        state=state,
                                        owner=product.owner,
                                        borrower=bundle.request.user,
                                        total_amount=total_amount,
                                        **kwargs)

        # Pass unit information to dehydrate
        bundle.data["price_unit"] = UNIT.reverted[unit]

        if not is_offline_booking and state == "authorizing":
            try:
                booking = bundle.obj
                booking.init_payment_processor()
                booking.preapproval(
                    cancel_url="%s://%s%s" %
                    (protocol, domain,
                     reverse("booking_failure", args=[booking.pk.hex])),
                    return_url="%s://%s%s" %
                    (protocol, domain,
                     reverse("booking_success", args=[booking.pk.hex])),
                    ip_address=request.META.get(
                        'HTTP_X_FORWARDED_FOR',
                        request.META.get('REMOTE_ADDR')))
            except IntegrityError:
                raise ImmediateHttpResponse(response=HttpBadRequest())

        return bundle
Exemplo n.º 14
0
    def obj_create(self, bundle, **kwargs):
        data = bundle.data

        try:
            recipient = Patron.objects.get(
                id=int(data["recipient"].split("/")[-2]))
            parent_msg = ProductRelatedMessage.objects.get(
                id=int(data["parent_msg"].split("/")[-2]))
            thread = MessageThread.objects.get(id=int(data["thread"]))
            bundle.obj = ProductRelatedMessage(recipient=recipient,
                                               sender=bundle.request.user,
                                               parent_msg=parent_msg,
                                               subject=data["subject"],
                                               body=data["body"],
                                               thread=thread)
        except IntegrityError:
            raise ImmediateHttpResponse(response=HttpBadRequest())
        return bundle
Exemplo n.º 15
0
    def locate_me(self, request, **kwargs):
        """Used by a user to locate themselves within a building"""
        self._verify(request)
        data = self.deserialize(request,
                                request.body,
                                format=request.META.get(
                                    'Content-Type', 'application/json'))

        access_points = data.get('access_points', None)
        if access_points is None or len(access_points) < 1:
            msg = 'No access points provided'
            raise ImmediateHttpResponse(HttpBadRequest(msg))

        location = self._locate_user(access_points)
        self._save_user_location(request.user, location)

        return self.create_response(request,
                                    data=location.to_dict(),
                                    response_class=HttpAccepted)
Exemplo n.º 16
0
Arquivo: api.py Projeto: adonm/oim-cms
 def update_cost_centre(self, request, **kwargs):
     """View to allow sync of object cost centre into AD.
     """
     self.method_check(request, allowed=['post'])
     try:
         obj = models.Computer.objects.get(pk=kwargs['pk'])
     except models.Computer.DoesNotExist:
         return HttpBadRequest('Unknown computer object')
     new_cost_centre_no = request.REQUEST.get('cost_centre_no', None)
     obj.cost_centre_no = new_cost_centre_no
     obj.save()
     # If the device is linked to an AD object, ensure that the changes are
     # pushed to AD as well.
     if obj.ad_guid:  # Object is linked to AD.
         res = obj.ad_set_cost_centre()
         if not res:
             return HttpResponseServerError(
                 'Unable to update cost centre number in AD')
     return self.create_response(request, data=obj)
Exemplo n.º 17
0
def feeds_to_user(request):
    # load = json.loads(request.body)
    # device = load.get('device_id')
    device = request.GET['device_id']
    print "===========DEvice Id====", device
    if not device:
        return HttpBadRequest("Enter device id")

    user_obj = UserProfile.objects.get(device_id=device)
    user_interests = user_obj.interests_category.all()
    user_languages = user_obj.user_languages.all()
    paginator = PageNumberPagination()

    interest_list = []
    language_list = []
    for interest in user_interests:
        interest_list.append(interest)
    for language in user_languages:
        language_list.append(language)

    newsfeeds_objs1 = NewsFeedItems.objects.filter(category__in=interest_list)\
                                            .filter(language__in=language_list)\
                                            .order_by('-pubdate')

    newsfeeds_objs = paginator.paginate_queryset(newsfeeds_objs1, request)

    feed_item = []
    for newsfeeds_obj in newsfeeds_objs:
        user_feeds = {}
        user_feeds['title'] = newsfeeds_obj.title
        user_feeds['descrption'] = newsfeeds_obj.content
        user_feeds['image_url'] = newsfeeds_obj.image_url
        news_pubdate = datetime.datetime.strftime(newsfeeds_obj.pubdate,
                                                  '%a, %d %b %Y %I:%M:%S')
        user_feeds['pubdate'] = str(news_pubdate)
        user_feeds['link'] = newsfeeds_obj.link
        user_feeds['category'] = newsfeeds_obj.category
        user_feeds['language'] = newsfeeds_obj.language
        user_feeds['source'] = newsfeeds_obj.source
        feed_item.append(user_feeds)

    data = {'status_code': 200, 'feeds_details': feed_item}
    return Response(data, status=status.HTTP_200_OK)
Exemplo n.º 18
0
    def add_book(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        self.is_authenticated(request)
        deserialized = self.deserialize(request, request.body,
                                        format=request.META.get('CONTENT_TYPE', 'application/json'))
        deserialized = self.alter_deserialized_detail_data(request, deserialized)
        bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized), request=request)
        try:
            reading_list = ReadingList.objects.get(id=bundle.data.get("id"), user=request.user)
        except ReadingList.DoesNotExist:
            raise ImmediateHttpResponse(HttpBadRequest('Bad reading list id'))

        data = {}
        for field in Book.BOOK_FIELDS:
            data[field] = bundle.data.get(field)

        Book(reading_list=reading_list, **data).save()

        return self.create_response(request, {})
Exemplo n.º 19
0
    def post_detail(self, request, **kwargs):

        assignment = Assignment.objects.get(pk=kwargs['pk'])

        if 'defer' in request.POST:

            debit = assignment.defer()
            debit_res = DebitResource()
            location = debit_res.get_resource_uri(debit)

            return HttpCreated(location=location)

        elif 'nudge' in request.POST:

            nudge = Nudge.objects.create(target=assignment.worker)
            nudge_res = NudgeResource()
            location = nudge_res.get_resource_uri(nudge)

            return HttpCreated(location=location)

        return HttpBadRequest()
Exemplo n.º 20
0
    def feeds_to_user(self, request, **kwargs):
        print "in this ================"
        device = request.GET['device_id']
        # load = json.loads(request.body)
        # device = load.get('device_id')

        if not device:
            return HttpBadRequest("Enter device id")

        user_obj = UserProfile.objects.get(device_id=device)
        user_interests = user_obj.interests_category.all()
        user_languages = user_obj.user_languages.all()
        interest_list = []
        language_list = []
        for interest in user_interests:
            interest_list.append(interest)
        for language in user_languages:
            language_list.append(language)

        newsfeeds_objs = NewsFeedItems.objects.filter(category__in=interest_list)\
                                                .filter(language__in=language_list)\
                                                .order_by('-pubdate')
        feed_item = []
        for newsfeeds_obj in newsfeeds_objs:
            user_feeds = {}
            user_feeds['title'] = newsfeeds_obj.title
            user_feeds['descrption'] = newsfeeds_obj.content
            user_feeds['image_url'] = newsfeeds_obj.image_url
            news_pubdate = datetime.datetime.strftime(newsfeeds_obj.pubdate,
                                                      '%a, %d %b %Y %I:%M:%S')
            user_feeds['pubdate'] = str(news_pubdate)
            user_feeds['link'] = newsfeeds_obj.link
            user_feeds['category'] = newsfeeds_obj.category
            user_feeds['language'] = newsfeeds_obj.language
            user_feeds['source'] = newsfeeds_obj.source
            feed_item.append(user_feeds)

        data = {'status_code': 200, 'feeds_details': feed_item}
        return HttpResponse(json.dumps(data), content_type="application/json")
Exemplo n.º 21
0
    def delete_detail( self, request, **kwargs ):

        repo_id = kwargs[ 'pk' ]

        try:
            basic_bundle = self.build_bundle( request=request )
            repo = Repository.objects.get( mongo_id=repo_id )

            if not self.authorized_delete_detail( repo, basic_bundle ):
                return HttpUnauthorized()

            data_id = request.GET['data_id']

            if not data_id:
                return HttpResponse( status=404 )

            db.data.remove( { '_id': ObjectId( data_id ) } )

            return HttpResponse( status=200 )

        except Exception as e:
            return HttpBadRequest( str( e ) )
Exemplo n.º 22
0
        def wrapper(resource, bundle=None, **kwargs):
            """ wraps the decorated method and verifies a list of required
            fields when a new object is being created.

            """
            if not isinstance(bundle, Bundle):
                request = bundle
                data = resource.deserialize(request,
                                            request.body,
                                            format=request.META.get(
                                                'CONTENT_TYPE',
                                                'application/json'))
                bundle = resource.build_bundle(request=request, data=data)
            else:
                request = None

            for required_field in required_fields:
                if required_field not in bundle.data:
                    response = HttpBadRequest(
                        json.dumps("missing %s field" % required_field),
                        content_type=bundle.request.META['CONTENT_TYPE'])
                    raise ImmediateHttpResponse(response=response)
            return func(resource, bundle=bundle, **kwargs)
Exemplo n.º 23
0
def news_to_feeds(request):
    try:
        news_objs = News.objects.all()
        for news_obj in news_objs:
            newsfeeds_obj = NewsFeedItems(language=news_obj.language,
                                          category=news_obj.category,
                                          image_url=news_obj.image_url,
                                          content=news_obj.content,
                                          pubdate=news_obj.pubdate,
                                          source=news_obj.source,
                                          title=news_obj.title,
                                          link=news_obj.link)
            newsfeeds_obj.save()
        return Response({
            'message': 'News saved to Feeds succesfully',
            'status': 200
        })

    except Exception as ex:
        logger.info(
            "Exception while updating user details with device id - {0}".
            format(ex))
    return HttpBadRequest("Update failed")
Exemplo n.º 24
0
    def append(self, request, **kwargs):
        self.is_authenticated(request)

        try:
            dataset_id = kwargs.pop('dataset_id', None)

            bundle = self.build_bundle(request=request)
            obj = self.obj_get(bundle,
                               **self.remove_api_resource_names(kwargs))

            csv_info = json.loads(request.POST.get('csv_info'))
            additional_fields = json.loads(request.POST.get('fields'))

            prepared_csv = prepare_csv_rows(obj.file, csv_info)
            table_name = create_database_table(
                prepared_csv['row_set'],
                csv_info,
                dataset_id,
                append=True,
                additional_fields=additional_fields)
            self.populate_point_data(dataset_id, csv_info)

            bundle.data['table_name'] = table_name

            obj.delete(
            )  # Temporary file has been moved to database, safe to delete

        except Exception as e:
            logger.exception(e)

            raise ImmediateHttpResponse(
                HttpBadRequest(content=json.dumps(
                    derive_error_response_data(e)),
                               content_type='application/json'))

        return self.create_response(request, bundle)
Exemplo n.º 25
0
class ResultBundle(Bundle):
    """tastypie.api.Bundle class to deal with submitted results.

    Note, to populate the Bundle.obj with data .save()
    has to be called first.

    FIXME (a8): add models.Data if they do not exist in DB
    """
    DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'

    # order of mandatory_keys must not be changed
    # FIXME (a8): refactor result_value to just value
    mandatory_keys = (
        'revision',
        'project',
        'executable',
        'benchmark',
        'environment',
        'branch',
        'result_value',
    )

    # Note, views.add_result() expects result_date. Here it's the
    # same as the Result() attribute
    optional_keys = (
        'std_dev',
        'val_min',
        'val_max',
        'date',
    )

    def __init__(self, obj=None, **kwargs):
        self.data = kwargs

        if isinstance(obj, Result):
            self.obj = obj
            self._populate_by_obj()
        elif obj is None:
            self.obj = Result()
        else:
            raise ValueError("obj has to be an instance of models.Result")

        if self.data:
            self._check_data()
        self.__data_validated = False  # not used for now
        super(ResultBundle, self).__init__(data=self.data, obj=self.obj)

    def _populate_obj_by_data(self):
        """Database lookup

        get everything except the result, 2nd try reverse lookup
        """
        def populate(key):
            return {
                'project':
                lambda: ProjectResource().get_via_uri(self.data['project']),
                'executable':
                lambda: ExecutableResource().get_via_uri(self.data['executable'
                                                                   ]),
                'benchmark':
                lambda: BenchmarkResource().get_via_uri(self.data['benchmark']
                                                        ),
                'environment':
                lambda: EnvironmentResource().get_via_uri(self.data[
                    'environment']),
                'branch':
                lambda: BranchResource().get_via_uri(self.data['branch']),
                'revision':
                lambda: RevisionResource().get_via_uri(self.data['commitid'])
            }.get(key, None)()

        try:
            self.obj.value = float(self.data['result_value'])
        except ValueError, error:
            logging.error(
                "Result value: {0} cannot be converted to float. {1}".format(
                    self.data['result_value'], error))
            raise ImmediateHttpResponse(
                response=HttpBadRequest(u"Value needs to be a number"))
        for key in [k for k in self.mandatory_keys \
                    if k not in ('result_value',)]:
            try:
                #populate
                item = populate(key)
                setattr(self.obj, key, item)
            except Exception, error:
                logging.error("Data for field %s: %s not found. %s" %
                              (key, self.data[key], error))
                raise ImmediateHttpResponse(response=HttpBadRequest(
                    u"Error finding: {0}={1}".format(key, self.data[key])))
Exemplo n.º 26
0
    def deploy(self, request, **kwargs):
        """
            The deploy endpoint, at ``{tablo_server}/api/v1/temporary-files/{uuid}/{dataset_id}/deploy/`` deploys
            the file specified by {uuid} into a database table named after the {dataset_id}. The {dataset_id} must
            be unique for the instance of Tablo.

            With the deploy endpoint, this is the start of what Tablo considers an import. The data will be
            temporarily stored in an import table until the finalize endpoint for the dataset_id is called.

            POST messages to the deploy endpoint should include the following data:

            **csv_info**
                Information about the CSV file. This is generally that information obtained through the
                describe endpoint, but can be modified to send additional information or modify it.
            **fields**
                A list of field JSON objects in the following format:

                .. code-block:: json

                    {
                        "name": "field_name",
                        "type": "text",
                        "required": true
                    }

                The value can be specified if the field is a constant value throughout the table. This can
                be use for adding audit information.

            :return:
                An empty HTTP 200 response if the deploy was successful. An error response if otherwise.
        """
        self.is_authenticated(request)

        try:
            dataset_id = kwargs.pop('dataset_id', None)

            bundle = self.build_bundle(request=request)
            obj = self.obj_get(bundle,
                               **self.remove_api_resource_names(kwargs))

            csv_info = json.loads(request.POST.get('csv_info'))
            additional_fields = json.loads(request.POST.get('fields'))

            prepared_csv = prepare_csv_rows(obj.file, csv_info)

            table_name = create_database_table(
                prepared_csv['row_set'],
                csv_info,
                dataset_id,
                additional_fields=additional_fields)
            add_geometry_column(dataset_id)
            self.populate_point_data(dataset_id, csv_info)

            bundle.data['table_name'] = table_name

            obj.delete(
            )  # Temporary file has been moved to database, safe to delete

        except Exception as e:
            logger.exception(e)

            raise ImmediateHttpResponse(
                HttpBadRequest(content=json.dumps(
                    derive_error_response_data(e)),
                               content_type='application/json'))

        return self.create_response(request, bundle)
Exemplo n.º 27
0
    def describe(self, request, **kwargs):
        """
        Describe, located at the ``{tablo-server}/api/v1/temporary-files/{uuid}/describe`` endpoint, will describe
        the uploaded CSV file. This allows you to know the column names and data types that were found within the
        file.

        :return:
            A JSON object in the following format:

            .. code-block:: json

                {
                    "fieldNames": ["field one", "field two", "latitude", "longitude"],
                    "dataTypes": ["String", "Integer", "Decimal", "Decimal"],
                    "optionalFields": ["field one"],
                    "xColumn": "longitude",
                    "yColumn": "latitude",
                    "filename": "uploaded.csv"
                }

            **fieldNames**
                A list of field (column) names within the CSV

            **dataTypes**
                A list of data types for each of the columns. The index of this list will match the index of the
                fieldNames list.

            **optionalFields**
                A list of fields that had empty values, and are taken to be optional.

            **xColumn**
                The best guess at which column contains X spatial coordinates.

            **yColumn**
                The best guess at which column contains Y spatial coordinates.

            **filename**
                The name of the file being described

        """
        self.is_authenticated(request)

        bundle = self.build_bundle(request=request)
        obj = self.obj_get(bundle, **self.remove_api_resource_names(kwargs))

        try:
            if obj.extension == 'csv':
                csv_file_name = obj.file.name
            else:
                raise InvalidFileError('Unsupported file format',
                                       extension=obj.extension)

        except InvalidFileError as e:
            raise ImmediateHttpResponse(
                HttpBadRequest(content=json.dumps(
                    derive_error_response_data(e, code=BAD_DATA)),
                               content_type='application/json'))

        csv_info = json.loads(request.POST.get('csv_info') or '{}') or None

        prepared_csv = prepare_csv_rows(obj.file, csv_info)
        row_set = prepared_csv['row_set']

        if not len(row_set):
            raise ImmediateHttpResponse(
                HttpBadRequest(content=json.dumps(
                    derive_error_response_data(InvalidFileError(
                        'File is empty', lines=0),
                                               code=BAD_DATA)),
                               content_type='application/json'))

        bundle.data['fieldNames'] = row_set.columns.to_list()
        bundle.data['dataTypes'] = prepared_csv['data_types']
        bundle.data['optionalFields'] = prepared_csv['optional_fields']

        x_field, y_field = prepared_csv['coord_fields']
        if x_field and y_field:
            bundle.data['xColumn'] = x_field
            bundle.data['yColumn'] = y_field

        bundle.data.update({'file_name': csv_file_name})

        return self.create_response(request, bundle)
Exemplo n.º 28
0
    def obj_get_list(self, bundle, **kwargs):

        get = {}
        if hasattr(bundle.request, 'GET'):
            get = bundle.request.GET.copy()

        querysets = self.set_querysets(get)

        if not querysets:
            return HttpBadRequest("No groups selected")

        import operator

        objects = Entity.objects.filter(
            reduce(operator.or_,
                   map(lambda x: self.allowed_querysets.get(x), querysets)))

        filters = self.set_filters(get)
        objects = objects.filter(**filters)

        addresslist = get.get('addresslist', 'off')
        if addresslist == 'off':
            export_fields = self.set_fields(get)
            objects = objects.values(*export_fields)
            converted = list(map(ExportObject, objects))
        elif addresslist in ['doubles', 'living_with']:
            objects = objects.filter(
                ~Q(street_name=''), ~Q(house_number='')).values(
                    'street_name', 'house_number', 'address_2', 'address_3',
                    'postcode', 'city', 'organization__name_prefix',
                    'organization__name', 'organization__name_short',
                    'organization__salutation', 'person__titles',
                    'person__initials', 'person__firstname',
                    'person__preposition', 'person__surname',
                    'person__postfix_titles', 'person__living_with',
                    'person__gender', 'id')

            def getname(obj):
                if obj.get('organization__name'):
                    name = obj.get('organization__name_prefix', "")
                    name += " "
                    name += obj.get('organization__name')
                    name = re.sub("\s+", " ", name)
                    return name.strip()
                elif obj.get('person__surname'):
                    titles = obj.get('person__titles')
                    if titles:
                        firstname = "%s %s" % (
                            titles,
                            obj.get('person__initials',
                                    obj.get('person__firstname', '')))
                    else:
                        firstname = obj.get('person__firstname',
                                            obj.get('person__initials', ''))

                    name = "%s %s %s %s" % (
                        firstname, obj.get('person__preposition',
                                           ''), obj.get('person__surname', ''),
                        obj.get('person__postfix_titles', ''))
                    name = re.sub("\s+", " ", name)
                    return name.strip()
                else:
                    return ""

            if addresslist == 'living_with':
                doubles = {}
                others = []
                for obj in objects:
                    if obj.get('person__living_with'):
                        if doubles.get(obj['person__living_with']):
                            other = doubles[obj['person__living_with']]

                            if obj.get('person__gender') == "M":
                                obj['combined_name'] = "%s en %s" % (
                                    getname(obj), getname(other))
                            else:
                                obj['combined_name'] = "%s en %s" % (
                                    getname(other), getname(obj))

                            others.append(obj)
                        else:
                            doubles[obj['id']] = obj
                    else:
                        others.append(obj)

                objects = others

            def format(obj):
                converted_obj = {}
                converted_obj['streetnumber'] = "%s %s" % (
                    obj.get('street_name'), obj.get('house_number'))
                postcode = obj.get('postcode').replace(' ', '')
                converted_obj['postcodecity'] = "%s %s" % (postcode,
                                                           obj.get('city'))
                converted_obj['kixcode'] = "%s%s" % (postcode,
                                                     obj.get('house_number'))
                converted_obj['name'] = obj.get('combined_name', getname(obj))
                return ExportObject(converted_obj)

            converted = list(map(format, objects))
            converted.sort(key=lambda p: p.kixcode)

        return converted
Exemplo n.º 29
0
 def response(self):
     return HttpBadRequest(json.dumps(self._response),
                           content_type='application/json')
Exemplo n.º 30
0
 def _unauthorized(self):
     response = HttpBadRequest()
     response.content = "Unauthorized"
     return response