def view_beamer_off(request): if request.method == 'GET': if request.user.is_staff: snippets = Snippet( classname= 'Beamer(usb, baudrate, bytesize, parity, stopbits, timeout)', code='off(input_off)') serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {}) elif request.method == 'POST': data = JSONParser().parse(request) serializer = SnippetSerializer(data=data) if serializer.is_valid(): parsed_data = encode_api_json(data) name = parsed_data[0] device = parsed_data[1].split(",") method = parsed_data[2] input_off = parsed_data[3] if name == "Beamer": if method == "off": Beamer(device[0], int(device[1]), int(device[2]), device[3], int(device[4]), int(device[5])).off(bytes.fromhex(input_off)) return JsonResponse(data, status=201) return JsonResponse(serializer.errors, status=400) return JsonResponse(serializer.errors, status=400)
def view_powerstrip(request): if request.user.is_staff: if request.method == 'GET': snippets = Snippet( classname='Powerstrip(device_room)', code= 'switch_on(socket), switch_off(socket), switch_all_on(), switch_all_off(), status(socket), status_all()' ) serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {})
def view_usb_switch(request): if request.user.is_staff: if request.method == 'GET': snippets = Snippet( classname='USBSwitch()', code= 'change_hub(), change_to_previous_hub(), change_to_number(hub), set_number_switch(), check_usb(), find_usb_switch()' ) serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {})
def view_hdmi_switch(request): if request.user.is_staff: if request.method == 'GET': snippets = Snippet( classname='HdmiSwitch()', code= 'status(), output_connections(output), get_connection(connection_feedback), status_in(), status_out(), get_in_out_devices(status), connect(inputs, output), switch_off(output), switch_on(output)' ) serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {})
def save_snippet(): if request.json: snippet = Snippet(title=request.json['title'], language=request.json['language'], code=request.json['snippet']) db.session.add(snippet) db.session.commit() return Response(json.dumps({"id": snippet.id}), 201, mimetype='application/json') else: return not_acceptable('Please ensure that your request contains a valid JSON')
def view_beamer(request): if request.user.is_staff: if request.method == 'GET': snippets = Snippet( classname= 'Beamer(usb, baudrate, bytesize, parity, stopbits, timeout)', code= 'on(input_on), off(input_off), changeState(input_change_state, input_on, input_off)' ) serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {})
def create_snippet(word, rawUrl, url, starttime, endtime): try: word_object = Word.objects.get(value=word) except models.Word.DoesNotExist: word_object = Word(value=word) word_object.save() audio_object = Audio.objects.get(url=rawUrl) new_Snippet = Snippet(word=word_object, audio=audio_object, start=starttime, end=endtime, url=url) new_Snippet.save() print "Saved \"%s\" Snippet to database" % word
def restore_object(self, attrs, instance=None): """ 创建或更新一个snippet实例, 返回该snippet实例 如果不定义该function, 则反序列化时将返回一个包括所有field的dict """ if instance: # 更新已存在的snippet实例 instance.title = attrs.get('title', instance.title) instance.code = attrs.get('code', instance.code) instance.linenos = attrs.get('linenos', instance.linenos) instance.language = attrs.get('language', instance.language) instance.style = attrs.get('style', instance.style) return instance # Create new instance return Snippet(**attrs)
def restore_object(self, attrs, instance=None): """ Create or update a new snippet instance, given a dictionary of deserialized field values. Note that if we don't define this method, then deserializing data will simply return a dictionary of items. """ if instance: # Update existing instance instance.title = attrs.get('title', instance.title) instance.code = attrs.get('code', instance.code) instance.linenos = attrs.get('linenos', instance.linenos) instance.language = attrs.get('language', instance.language) instance.style = attrs.get('style', instance.style) return instance # Create new instance return Snippet(**attrs)
def view_usb_switch_check_usb(request): if request.method == 'GET': if request.user.is_staff: snippets = Snippet(classname='USBSwitch()', code='check_usb()') serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {}) elif request.method == 'POST': data = JSONParser().parse(request) serializer = SnippetSerializer(data=data) if serializer.is_valid(): parsed_data = encode_api_json(data) name = parsed_data[0] method = parsed_data[2] if name == "USBSwitch": if method == "check_usb": USBSwitch().check_usb() return JsonResponse(data, status=201) return JsonResponse(serializer.errors, status=400) return JsonResponse(serializer.errors, status=400)
def view_hdmi_switch_on(request): if request.method == 'GET': if request.user.is_staff: snippets = Snippet(classname='HdmiSwitch()', code='switch_on(output)') serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {}) elif request.method == 'POST': data = JSONParser().parse(request) serializer = SnippetSerializer(data=data) if serializer.is_valid(): parsed_data = encode_api_json(data) name = parsed_data[0] method = parsed_data[2] output = parsed_data[3] if name == "HdmiSwitch": if method == "switch_on": HdmiSwitch().switch_on(output) return JsonResponse(data, status=201) return JsonResponse(serializer.errors, status=400) return JsonResponse(serializer.errors, status=400)
def view_powerstrip_status_all(request): if request.method == 'GET': if request.user.is_staff: snippets = Snippet(classname='Powerstrip(device_room)', code='status_all()') serializer = SnippetSerializer(snippets) return JsonResponse(serializer.data, safe=False) else: return render(request, 'index_api.html', {}) elif request.method == 'POST': data = JSONParser().parse(request) serializer = SnippetSerializer(data=data) if serializer.is_valid(): parsed_data = encode_api_json(data) name = parsed_data[0] device_room = parsed_data[1] method = parsed_data[2] if name == "Powerstrip": if method == "status_all": Powerstrip(int(device_room)).status_all() return JsonResponse(data, status=201) return JsonResponse(serializer.errors, status=400) return JsonResponse(serializer.errors, status=400)
def load_data(self): snippet = Snippet(title="Uploading a file to S3 using Boto", language="python", code=""" import cStringIO from boto.s3.connection import S3Connection from boto.s3.key import Key def s3upload(filename, extension, file): conn = S3Connection('AWS_ACCESS_KEY', 'AWS_SECRET_KEY') k = Key(conn.get_bucket('<bucket name>')) k.key = filename k.set_metadata("Content-Type", file.mimetype) fp = cStringIO.StringIO(file.read()) k.set_contents_from_file(fp) k.set_acl("public-read") fp.close() return "https://s3.amazonaws.com/<bucket name>/{}.{}".format(filename, extension) """) self.snippet1 = snippet db.session.add(snippet) db.session.commit()
def create(self, validated_data): return Snippet().objects.create(**validated_data)