def setUp(self): config = { 'db': 'test_testproject', 'host': '127.0.0.1', 'port': 27017, 'read_preference': read_preferences.ReadPreference.PRIMARY, } self.app = create_app(config) self.app.config['TESTING'] = True self.app_context = self.app.app_context() self.app_context.push() self.testapp = self.app.test_client() from mainapp.api.models import Record r1 = Record( uid='1', name='John Doe', date=datetime.datetime.strptime( '2015-05-12T14:36:00.451765', "%Y-%m-%dT%H:%M:%S.%f" ), md5checksum='5662edb6868e98505e0f4344d2abd47b' ) r2 = Record( uid='2', name='Jane Doe', date=datetime.datetime.strptime( '2015-05-13T14:36:00.451765', "%Y-%m-%dT%H:%M:%S.%f" ), md5checksum='0cf6399e2739304b73b41162735679fb' ) r1.save() r2.save()
def put_record(): if not request.json: return jsonify({'response': 'Error. request should be a JSON string'}) data = request.json VALID_FIELDS = ['uid', 'name', 'date', 'md5checksum'] for field in VALID_FIELDS: if field not in data: return jsonify({ 'response': 'Error. {0} field is missing'.format(field) }) try: date = datetime.datetime.strptime(data.get('date'), "%Y-%m-%dT%H:%M:%S.%f") except ValueError: return jsonify({ 'response': 'Error. Invalid date format. Date should be given' ' in a %%Y-%%m-%%dT%%H:%%M:%%S.%%f format' }) r = Record( uid=data.get('uid'), name=data.get('name'), date=date, md5checksum=data.get('md5checksum') ) if not r.check_md5(): return jsonify({'response': "Error. Checksum doesn't match"}) uidRecord = Record.objects(uid=r.uid).first() nameRecord = Record.objects(name=r.name).first() if uidRecord and r.name != uidRecord.name: return jsonify({'response': "Error. name - uid missmatch"}) if nameRecord and r.uid != nameRecord.uid: return jsonify({'response': "Error. name - uid missmatch"}) r.save() return jsonify({'response': 'record successfuly added'})
def get_record(uid, date): minDate = datetime.datetime.strptime(date, "%Y-%m-%d") maxDate = minDate + datetime.timedelta(days=1) records = Record.objects(uid=uid, date__gte=minDate, date__lt=maxDate) return jsonify({'response': records})