def editSource(request, id): """This view is responsible to recieve and process requests for changing sources. If the form is valid, the source gets updated. Otherwise, a form with a description of what is wrong gets returned. This view is not a complete website, as it is supposed to be called via AJAX""" logger = logging.getLogger(__name__) # If we cannot find a source with the requested ID, raise an 404 error. try: source = Source.objects.get(pk=id) except Source.DoesNotExist: raise Http404 # Return the ID to the requesting client, so that the javascript knows which edit-form # needs to be updated when the response is recieved. data = {} data['id'] = id data['success'] = "false" # If this view is supplied with some data: if (request.POST): # Create a form based on these data data.update(createForm(post=request.POST)) # It that form is valid, update the source-object, and display a friendly message telling that things have beed updated. if (data['newSourceForm'].is_valid() and ("timeSelector" not in data or data['timeSelector'].is_valid())): source.name = data['newSourceForm'].cleaned_data['name'] source.url = data['newSourceForm'].cleaned_data['url'] source.md5url = data['newSourceForm'].cleaned_data['md5url'] source.url = data['newSourceForm'].cleaned_data['url'] source.setSchedule(data, save=False) source.save() logger.debug("Saved %s", source) data['message'] = "The source is updated." data['success'] = "true" # If the supplied data was invalid, return an error-message. else: data['message'] = "The schema was not correct." # If this view was invoked without data, return a form with the data of the source-object. else: data.update(createForm(source=source)) return render(request, "update/response.tpl", data)
def editSource(request, id): """This view is responsible to recieve and process requests for changing sources. If the form is valid, the source gets updated. Otherwise, a form with a description of what is wrong gets returned. This view is not a complete website, as it is supposed to be called via AJAX""" logger = logging.getLogger(__name__) # If we cannot find a source with the requested ID, raise an 404 error. try: source = Source.objects.get(pk=id) except Source.DoesNotExist: raise Http404 # Return the ID to the requesting client, so that the javascript knows which edit-form # needs to be updated when the response is recieved. data = {} data["id"] = id data["success"] = "false" # If this view is supplied with some data: if request.POST: # Create a form based on these data data.update(createForm(post=request.POST)) # It that form is valid, update the source-object, and display a friendly message telling that things have beed updated. if data["newSourceForm"].is_valid() and ("timeSelector" not in data or data["timeSelector"].is_valid()): source.name = data["newSourceForm"].cleaned_data["name"] source.url = data["newSourceForm"].cleaned_data["url"] source.md5url = data["newSourceForm"].cleaned_data["md5url"] source.url = data["newSourceForm"].cleaned_data["url"] source.setSchedule(data, save=False) source.save() logger.debug("Saved %s", source) data["message"] = "The source is updated." data["success"] = "true" # If the supplied data was invalid, return an error-message. else: data["message"] = "The schema was not correct." # If this view was invoked without data, return a form with the data of the source-object. else: data.update(createForm(source=source)) return render(request, "update/response.tpl", data)
def newSource(request): """This view recieves data from the "newSource" form. If the data recieved is valid, a new source is created. Otherwise, the form is returned, with information on why the form is not valid. This view is not a complete website, as it is supposed to be called via AJAX""" data = {} # If we recieved some data: if (request.POST): # Create a form based on these data. data.update(createForm(post=request.POST)) # If the form now is valid: if (data['newSourceForm'].is_valid()): # And eventually, the timeSelector data is valid, if a schedule other than none is selected if ('timeSelector' in data and data['timeSelector'].is_valid() == False): pass else: # Try to create a new source source, created = Source.objects.get_or_create( name=data['newSourceForm'].cleaned_data['name']) # If it already exists, return an error message. if not created: data[ 'warningmessage'] = "The source '%s' already exists" % data[ 'newSourceForm'].cleaned_data['name'] # Otherwise, update the freshly created source with the correct data, and save it. else: source.url = data['newSourceForm'].cleaned_data['url'] source.md5url = data['newSourceForm'].cleaned_data[ 'md5url'] source.setSchedule(data, save=False) source.save() # Add an error-message, and remove the form from the view-data, so that it is not displayed. data[ 'warningmessage'] = "The source '%s' is created." % data[ 'newSourceForm'].cleaned_data['name'] data.pop('newSourceForm') # If no data was recieved, create an emty form. else: data['newSourceForm'] = NewSourceForm() return render(request, "update/newSourceForm.tpl", data)
def newSource(request): """This view recieves data from the "newSource" form. If the data recieved is valid, a new source is created. Otherwise, the form is returned, with information on why the form is not valid. This view is not a complete website, as it is supposed to be called via AJAX""" data = {} # If we recieved some data: if request.POST: # Create a form based on these data. data.update(createForm(post=request.POST)) # If the form now is valid: if data["newSourceForm"].is_valid(): # And eventually, the timeSelector data is valid, if a schedule other than none is selected if "timeSelector" in data and data["timeSelector"].is_valid() == False: pass else: # Try to create a new source source, created = Source.objects.get_or_create(name=data["newSourceForm"].cleaned_data["name"]) # If it already exists, return an error message. if not created: data["warningmessage"] = ( "The source '%s' already exists" % data["newSourceForm"].cleaned_data["name"] ) # Otherwise, update the freshly created source with the correct data, and save it. else: source.url = data["newSourceForm"].cleaned_data["url"] source.md5url = data["newSourceForm"].cleaned_data["md5url"] source.setSchedule(data, save=False) source.save() # Add an error-message, and remove the form from the view-data, so that it is not displayed. data["warningmessage"] = "The source '%s' is created." % data["newSourceForm"].cleaned_data["name"] data.pop("newSourceForm") # If no data was recieved, create an emty form. else: data["newSourceForm"] = NewSourceForm() return render(request, "update/newSourceForm.tpl", data)