Exemple #1
0
    def pre_save(self, obj):
        obj.task = Task.objects.get(pk=self.kwargs['task_pk'])
        if self.request.user.crowduser.balance < obj.task.reward:
            raise NotEnoughMoney
        log.debug("pk %s", self.kwargs['task_pk'])

        if obj.uuid is None or len(obj.uuid) == 0:
            obj.uuid = str(uuid4()).replace("-", "")
        log.debug("obj parameters %s", obj.parameters)
        if obj.parameters is None:
            obj.parameters = {}

        #     trick for the JSONFields

        input = self.request.DATA['input'] if 'input' in self.request.DATA else None
        pars = self.request.DATA['parameters'] if 'parameters' in self.request.DATA else None

        log.debug(input)
        if input is not None:
            data = obj.input_data
            if data is None:
                data = Data()
            data.value = input
            data.save()
            obj.input_data = data

        if pars is not None:
            obj.parameters = pars
Exemple #2
0
    def post(self, request, *args, **kwargs):

        result = json.loads(request.body)
        data = Data(url=request.get_full_path(), data=request.body)
        data.save()

        return HttpResponse(json.dumps(result))
Exemple #3
0
    def execute(self, request, pk=None, task_pk=None):
        task_instance = get_instance_worker(pk, request.user)

        output = self.request.DATA['result'] if 'result' in self.request.DATA else None

        if output is not None:
            data = task_instance.output_data
            if data is None:
                data = Data()
            data.value = output
            data.save()
            task_instance.output_data = data
            return Response(TaskInstanceSerializer(task_instance).data)
        else:
            raise exceptions.ParseError(detail="result is empty, what results is this then?")
Exemple #4
0
    def parse_row(self, row, csv_model):
        title = row.get("title", None)
        description = row.get("description", None)
        image = row.get("image", None)

        data = Data(csv=csv_model,
                    title=title,
                    description=description,
                    image=None,
                    image_url=image)
        data.save()
        if image != "":
            request = requests.get(image, stream=True)
            if request.status_code != requests.codes.ok:
                uploaded_image, file_name = None, None
                return "image can't be downloaded"
            else:
                # downloading image
                file_name = "{}_{}".format(data.id, image.split('/')[-1])
                lf = tempfile.NamedTemporaryFile()
                for block in request.iter_content(1024 * 8):
                    if not block:
                        break
                    lf.write(block)

                # resizing image
                try:
                    img = Image.open(lf)
                except Exception:
                    return "image can't be opened"
                else:
                    width_percent = (settings.BASEWIDTH / float(img.size[0]))
                    height_size = int(
                        (float(img.size[1]) * float(width_percent)))
                    resize_img = img.resize((settings.BASEWIDTH, height_size),
                                            Image.ANTIALIAS)
                    resized = BytesIO()
                    resize_img.save(resized, format='JPEG', quality=90)
                    uploaded_image = files.File(resized)
                    data.image.save(file_name, uploaded_image)
                    return "success"
        else:
            return "no url"
Exemple #5
0
 def handle(self, *args, **options):
     Data.update()
Exemple #6
0
def robot_data(args):
    robot_id = args["robot_id"]
    data_instance = Data(robot_id)
    data_instance.position_x = args["position_x"]
    data_instance.position_y = args["position_y"]
    data_instance.position_z = args["position_z"]
    data_instance.velocity_x = args["velocity_x"]
    data_instance.velocity_y = args["velocity_y"]
    data_instance.velocity_z = args["velocity_z"]
    data_instance.direction_x = args["direction_x"]
    data_instance.direction_y = args["direction_y"]
    data_instance.direction_z = args["direction_z"]
    data_instance.ip = args["ip"]
    data_instance.data = args["data"]
    try:
        base.db.session.add(data_instance)
        base.db.session.commit()
        return create_response(data=args, message="ok", code=0)
    except Exception:
        return create_response(data={}, message="error", code=9999)
#print '-----purge old data'
#time.sleep(3)
op = Data.objects.all().delete()

import csv
data_dict = {}
count = 0

#print 'about to read csv'
#time.sleep(3)
with open('Corpus.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        count += 1
        data_dict[row['value']] = row['key']

#print 'read %d data records' % count

#print 'Attempting to bulk insert %d records' % len(data_dict.keys())

#time.sleep(3)

op = Data.objects.bulk_create([
    Data(key=string_rep, value=numeric_rep)
    for numeric_rep, string_rep in data_dict.iteritems()
])

#print 'Successfully inserted %d data records' % Data.objects.count()
#print 'exiting' ,
#time.sleep(3)
exit()