def test_does_this_create_or_change_record_replace_change(self): # Apply inital data - No record, so should be true data_initial = NewEventData( "animal", "lion", { "title": "Lion", "sound": "Roar!" }, approved=True, ) assert data_initial.does_this_create_or_change_record() # Save it so we have record to compare more edits to newEvent([data_initial], None) # Data with Mode Replace - different? data_replace = NewEventData( "animal", "lion", { "title": "Big Cat", "sound": "Roar!" }, mode=EVENT_MODE_REPLACE, ) assert data_replace.does_this_create_or_change_record()
def test_filter_needs_moderation_by_type(self): type_model = Type() type_model.public_id = "animal" type_model.title = "Animal" type_model.save() # Create a record, approve straight away newEvent( [ NewEventData( "animal", "lion", { "title": "Lion", "sound": "Roar!" }, approved=True, ) ], None, ) records = Record.objects.all() assert 1 == len(records) # Purge record purge_record(records[0]) # Test assert 0 == len(Record.objects.all()) assert 0 == len(Event.objects.all()) assert 0 == len(Edit.objects.all())
def test_basic(self): """hello world""" data = NewEventData("animal", "lion", { "title": "Lion", "sound": "Roar!" }) newEvent([data], None) edits = Edit.objects.all() assert 1 == len(edits) assert edits[0].data["sound"] == "Roar!"
def admin_organisation_make_disputed(request, public_id): try: type = Type.objects.get(public_id=TYPE_ORGANISATION_PUBLIC_ID) record = Record.objects.get(type=type, public_id=public_id) except Type.DoesNotExist: raise Http404("Type does not exist") except Record.DoesNotExist: raise Http404("Record does not exist") if request.method == "POST": # Create a form instance and populate it with data from the request (binding): form = OrganisationMakeDisputedForm(request.POST) # Check if the form is valid: if form.is_valid(): # Save the event new_event_data = NewEventData( type, record, {"status": "DISPUTED"}, mode=jsondataferret.EVENT_MODE_MERGE, ) newEvent([new_event_data], user=request.user) # redirect to a new URL: return HttpResponseRedirect( reverse( "jsondataferretexampleapp_admin_organisation_index", kwargs={"public_id": record.public_id}, ) ) else: form = OrganisationMakeDisputedForm() context = { "record": record, "form": form, } return render( request, "jsondataferretexampleapp/admin/organisation/make_disputed.html", context, )
def admin_projects_new(request): try: type = Type.objects.get(public_id=TYPE_PROJECT_PUBLIC_ID) except Type.DoesNotExist: raise Http404("Type does not exist") # If this is a POST request then process the Form data if request.method == "POST": # Create a form instance and populate it with data from the request (binding): form = ProjectNewForm(request.POST) # Check if the form is valid: if form.is_valid(): # process the data in form.cleaned_data as required # Save the event id = form.cleaned_data["id"] existing_record = Record.objects.filter(type=type, public_id=id) if existing_record: form.add_error("id", "This ID already exists") else: data = NewEventData( type, id, {"project_name": {"value": form.cleaned_data["title"]}}, approved=True, ) newEvent([data], user=request.user) # redirect to a new URL: return HttpResponseRedirect( reverse( "jsondataferretexampleapp_admin_project_index", kwargs={"public_id": id}, ) ) # If this is a GET (or any other method) create the default form. else: form = ProjectNewForm() context = { "form": form, } return render(request, "jsondataferretexampleapp/admin/project/new.html", context)
def test_does_this_create_or_change_record_merge_no_change(self): # Apply inital data - No record, so should be true data_initial = NewEventData( "animal", "lion", { "title": "Lion", "sound": "Roar!" }, approved=True, ) assert data_initial.does_this_create_or_change_record() # Save it so we have record to compare more edits to newEvent([data_initial], None) # Data with Mode Merge - same? data_merge = NewEventData("animal", "lion", {"title": "Lion"}, mode=EVENT_MODE_MERGE) assert not data_merge.does_this_create_or_change_record()
def admin_organisations_new(request): # If this is a POST request then process the Form data if request.method == "POST": # Create a form instance and populate it with data from the request (binding): form = OrganisationNewForm(request.POST) # Check if the form is valid: if form.is_valid(): # process the data in form.cleaned_data as required (here we just write it to the model due_back field) # Save the event id = str(uuid.uuid4()) data = NewEventData( TYPE_ORGANISATION_PUBLIC_ID, id, {"title": form.cleaned_data["title"],}, approved=True, ) newEvent([data], user=request.user) # redirect to a new URL: return HttpResponseRedirect( reverse( "jsondataferretexampleapp_admin_organisation_index", kwargs={"public_id": id}, ) ) # If this is a GET (or any other method) create the default form. else: form = OrganisationNewForm() context = { "form": form, } return render( request, "jsondataferretexampleapp/admin/organisation/new.html", context )
def admin_project_import_form(request, public_id): try: type = Type.objects.get(public_id=TYPE_PROJECT_PUBLIC_ID) data = Record.objects.get(type=type, public_id=public_id) except Type.DoesNotExist: raise Http404("Type does not exist") except Record.DoesNotExist: raise Http404("Record does not exist") if request.method == "POST": # Create a form instance and populate it with data from the request (binding): form = ProjectImportForm(request.POST, request.FILES) # Check if the form is valid: if form.is_valid(): # get data guide_file = os.path.join( settings.BASE_DIR, "jsondataferretexampleapp", "spreadsheetform_guides", "project.xlsx", ) json_data = spreadsheetforms.api.get_data_from_form( guide_file, request.FILES["file"].temporary_file_path(), date_format=getattr( settings, "JSONDATAFERRET_SPREADSHEET_FORM_DATE_FORMAT", None ), ) # process the data in form.cleaned_data as required # Save the event new_event_data = NewEventData( TYPE_PROJECT_PUBLIC_ID, data.public_id, json_data, mode=jsondataferret.EVENT_MODE_MERGE, ) newEvent([new_event_data], user=request.user) # redirect to a new URL: return HttpResponseRedirect( reverse( "jsondataferretexampleapp_admin_project_index", kwargs={"public_id": data.public_id}, ) ) # If this is a GET (or any other method) create the default form. else: form = ProjectImportForm() context = { "data": data, "form": form, } return render( request, "jsondataferretexampleapp/admin/project/import_form.html", context )
def test_filter_needs_moderation_by_type(self): type_model = Type() type_model.public_id = "animal" type_model.title = "Animal" type_model.save() # no records, so no results assert 0 == len( Record.objects.filter_needs_moderation_by_type(type_model)) # Create a record, approve straight away newEvent( [ NewEventData( "animal", "lion", { "title": "Lion", "sound": "Roar!" }, approved=True, ) ], None, ) # already approved so no results assert 0 == len( Record.objects.filter_needs_moderation_by_type(type_model)) # Create a edit to that record that is not approved newEvent( [ NewEventData( "animal", "lion", { "title": "Lion", "sound": "Roar ROAR!" }, approved=False, ) ], None, ) # now there is a record assert 1 == len( Record.objects.filter_needs_moderation_by_type(type_model)) # Create a second edit to that record that is not approved newEvent( [ NewEventData( "animal", "lion", { "title": "Lion", "sound": "Purr" }, approved=False, ) ], None, ) # Several edits, but only one record assert 1 == len( Record.objects.filter_needs_moderation_by_type(type_model))