def editColumns(request,id): """ FORM TO CREATE A NEW COLUMN FOR A SILO """ silo = Silo.objects.get(id=id) data = getSiloColumnNames(id) form = EditColumnForm(initial={'silo_id': silo.id}, extra=data) if request.method == 'POST': form = EditColumnForm(request.POST or None, extra = data) # A form bound to the POST data if form.is_valid(): # All validation rules pass client = MongoClient(uri) db = client.tola for label,value in form.cleaned_data.iteritems(): #update the column name if it doesn't have delete in it if "_delete" not in label and str(label) != str(value) and label != "silo_id" and label != "suds" and label != "id": #update a column in the existing silo db.label_value_store.update_many( {"silo_id": silo.id}, { "$rename": {label: value}, }, False ) #if we see delete then it's a check box to delete that column elif "_delete" in label and value == 1: column = label.replace("_delete", "") db.label_value_store.update_many( {"silo_id": silo.id}, { "$unset": {column: value}, }, False ) messages.info(request, 'Updates Saved', fail_silently=False) else: messages.error(request, 'ERROR: There was a problem with your request', fail_silently=False) #print form.errors data = getSiloColumnNames(id) form = EditColumnForm(initial={'silo_id': silo.id}, extra=data) return render(request, "silo/edit-column-form.html", {'silo':silo,'form': form})
def test_celery_success(self): """ Test if the celery task process_silo actually imports data :return: """ silo = factories.Silo(owner=self.user, public=False) read_type = factories.ReadType(read_type="CSV") upload_file = open('silo/tests/sample_data/test.csv', 'rb') read = factories.Read(owner=self.user, type=read_type, file_data=SimpleUploadedFile( upload_file.name, upload_file.read())) factories.CeleryTask(task_status=CeleryTask.TASK_CREATED, content_object=read) process_done = process_silo(silo.id, read.id) silo = Silo.objects.get(pk=silo.id) self.assertEqual(getSiloColumnNames(silo.id), ['First_Name', 'Last_Name', 'E-mail']) self.assertTrue(process_done)
def export_to_gsheet(request, id): spreadsheet_id = request.GET.get("resource_id", None) query = json.loads(request.GET.get('query', "{}")) print 'querythis', query if type(query) == list: query = json.loads(makeQueryForHiddenRow(query)) cols_to_export = json.loads( request.GET.get('shown_cols', json.dumps(getSiloColumnNames(id)))) msgs = export_to_gsheet_helper(request.user, spreadsheet_id, id, query, cols_to_export) google_auth_redirect = "/export_to_gsheet/%s/" % id for msg in msgs: if "silo_id" in msg.keys(): id = msg.get("silo_id") if "redirect_uri_after_step2" in msg.keys(): request.session['redirect_uri_after_step2'] = google_auth_redirect return HttpResponseRedirect(msg.get("redirect")) messages.add_message(request, msg.get("level"), msg.get("msg")) return HttpResponseRedirect(reverse('listSilos'))
def test_silo_edit_columns_delete(self, mock_db): mock_db.return_value = Mock() self.tola_user.user.is_staff = True self.tola_user.user.is_superuser = True self.tola_user.user.save() fields = [ { 'name': 'test_color', 'type': 'text' }, { 'name': 'type', 'type': 'text' } ] silo = factories.Silo( name='Delete Test', columns=json.dumps(fields), ) data = { 'id': '', 'silo_id': silo.id, 'test_color_delete': True, 'type': 'type' } request = self.factory.post('', data=data) request.user = self.tola_user.user self._bugfix_django_messages(request) views.edit_columns(request, silo.id) silo = Silo.objects.get(pk=silo.id) column_names = util.getSiloColumnNames(silo.id) self.assertTrue('test_color' not in column_names) self.assertTrue('type' in column_names)
def data(self, request, id): if id <= 0: return HttpResponseBadRequest("The silo_id = %s is invalid" % id) silo = Silo.objects.get(pk=id) if silo.public == False: return HttpResponse( "This table is not public. You must use the private API.") query = request.GET.get('query', "{}") filter_fields = json.loads(query) shown_cols = set( json.loads( request.GET.get('shown_cols', json.dumps(getSiloColumnNames(id))))) recordsTotal = LabelValueStore.objects(silo_id=id, **filter_fields).count() #print("offset=%s length=%s" % (offset, length)) #page_size = 100 #page = int(request.GET.get('page', 1)) #offset = (page - 1) * page_size #if page > 0: # workaround until the problem of javascript not increasing the value of length is fixed data = LabelValueStore.objects(silo_id=id, **filter_fields).exclude( 'create_date', 'edit_date', 'silo_id', 'read_id') for col in getCompleteSiloColumnNames(id): if col not in shown_cols: data = data.exclude(col) sort = str(request.GET.get('sort', '')) data = data.order_by(sort) json_data = json.loads(data.to_json()) return JsonResponse(json_data, safe=False)
def test_postNewFormulaColumn(self): data = { 'math_operation': 'sum', 'column_name': '', 'columns': [] } response = self.client.post('/new_formula_column/{}/'.format( self.silo.pk), data=data) self.assertEqual(response.status_code, 302) lvs = LabelValueStore() lvs.a = "1" lvs.b = "2" lvs.c = "3" lvs.silo_id = self.silo.pk lvs.save() lvs = LabelValueStore() lvs.a = "2" lvs.b = "2" lvs.c = "3.3" lvs.silo_id = self.silo.pk lvs.save() lvs = LabelValueStore() lvs.a = "3" lvs.b = "2" lvs.c = "hi" lvs.silo_id = self.silo.pk lvs.save() data = { 'math_operation': 'sum', 'column_name': '', 'columns': ['a', 'b', 'c'] } response = self.client.post('/new_formula_column/{}/'.format( self.silo.pk), data=data) self.assertEqual(response.status_code, 302) formula_column = self.silo.formulacolumns.get(column_name='sum') self.assertEqual(formula_column.operation, 'sum') self.assertEqual(formula_column.mapping, '["a", "b", "c"]') self.assertEqual(formula_column.column_name, 'sum') self.assertEqual(getSiloColumnNames(self.silo.pk), ["sum"]) self.silo = Silo.objects.get(pk=self.silo.pk) self.assertEqual(getColToTypeDict(self.silo).get('sum'), 'float') try: lvs = LabelValueStore.objects.get(a="1", b="2", c="3", sum=6.0, read_id=-1, silo_id=self.silo.pk) lvs.delete() except LabelValueStore.DoesNotExist as e: self.assert_(False) try: lvs = LabelValueStore.objects.get(a="2", b="2", c="3.3", sum=7.3, read_id=-1, silo_id=self.silo.pk) lvs.delete() except LabelValueStore.DoesNotExist as e: self.assert_(False) try: lvs = LabelValueStore.objects.get(a="3", b="2", c="hi", sum="Error", read_id=-1, silo_id=self.silo.pk) lvs.delete() except LabelValueStore.DoesNotExist as e: self.assert_(False)