def state(): """ add a new event """ form = StateForm() events = State.query.all() days = '' event = None if form.validate_on_submit(): if form.mon.data == True: days += 'mon,' if form.tue.data == True: days += 'tue,' if form.wed.data == True: days += 'wed,' if form.thu.data == True: days += 'thu,' if form.fri.data == True: days += 'fri,' if form.sat.data == True: days += 'sat,' if form.sun.data == True: days += 'sun,' if days != "": days = days[ 0:len(days) - 1] # strip "," comma off of end of last day entry (so it is in usable data format) event_duplicate = State.query.filter_by( event_days=days, event_hour=form.hour.data, event_min=form.minute.data).first() if not event_duplicate: event = State(event_days=days, event_hour=form.hour.data, event_min=form.minute.data) db.session.add(event) db.session.commit() sched.add_job(runKettle, 'cron', id=str(event.id), day_of_week=str(days), hour=str(form.hour.data), minute=str(form.minute.data)) sched.print_jobs() print(sched.get_jobs()) flash('Event added!', 'success') return redirect(url_for('state')) else: flash("Event Already Exists, Please revise!", 'danger') else: flash("Error with request please check that it is valid", 'danger') return render_template('state.html', title='State', form=form, event=event, events=events)
def partywise(request): states = { 's26': 'Chattisgarh', 's29': 'Telangana', 's12': 'MadhyaPradesh', 's16': 'Mizoram', 's20': 'Rajasthan' } if request.method == "POST": form = StateForm(request.POST) state = form['dropdown'] Name = states[state.data] ruby = "C:/Users/admin/source/repos/ElectionResultsAnalysis/ElectionResultsAnalysis/app/static/scrape_data/" + Name + ".csv" df = pd.read_csv(ruby, skiprows=2) df.plot(kind='bar', x='Party', y='Won', color='blue') plt.savefig( 'C:/Users/admin/source/repos/FlaskElectionDADV/FlaskElectionDADV/FlaskElectionDADV/static/plots/' + Name + ".png") if (state.data == 's26'): return render(request, 'app/chattisgarh.html') elif (state.data == 's12'): return render(request, 'app/madhyapradesh.html') elif (state.data == 's20'): return render(request, 'app/rajasthan.html') elif (state.data == 's29'): return render(request, 'app/telangana.html') elif (state.data == 's16'): return render(request, 'app/mizoram.html') else: return render(request, 'app/party.html') else: return render(request, 'app/party.html')
def elements_handler(request, blogger=None, posterboard=None, element=None, format='html'): user = request.user data = {'message': ''} if not user.id == blogger.id and user and blogger: return HttpResponseForbidden( 'Posterboard Only Available for Blogger to Edit') if request.method == 'GET' and not request.GET.has_key('_action'): return HttpResponseBadRequest() # create elif request.method == 'POST' and not request.POST.has_key( '_action') and element is None: # Testing json response: #return HttpResponse(json.dumps({'test':1, 'again':2}), mimetype='application/json') elementform = ElementForm(request.POST, prefix='element') if elementform.is_valid(): # commit=False creates and returns the model object but doesn't save it. # Remove it if unnecessary. element = elementform.save(commit=False) if element.type != 'image' and posterboard.is_user_home_page: return HttpResponseForbidden('Only adding images is allowed.') posterboard.element_set.add(element) stateform = StateForm(request.POST, prefix='state') if not stateform.is_valid(): data['errors'] = 'State data isn\'t valid: ' + str( stateform.errors) return ErrorResponse(data['errors'], format) state = stateform.save(commit=False) state.speed = 1 # Appear super fast. state.order = 1 element.state_set.add(state) if element.type == 'image': if not 'image' in request.FILES: data['errors'] = 'No Image File is Provided. ' return ErrorResponse(data['errors'], format) childState = ImageState() childState.image = request.FILES['image'] if (request.POST.has_key('linked-posterboard')): linkpb = request.POST['linked-posterboard'] if (linkpb is not None): pblist = user.posterboard_set.filter( is_user_home_page=False, title=linkpb).all() if (len(pblist) > 0): childState.linkedposterboard = pblist[0] else: data['errors'] = 'Invalid posterboard.' return ErrorResponse(data['errors'], format) else: data['errors'] = 'No posterboard to link to provided.' return ErrorResponse(data['errors'], format) childState.full_clean() state.imagestate = childState # Write the element_content, which should be an image data['element_content'] = '<img src= "'+childState.image.url+ '"'\ 'alt="'+childState.alt+'"'+'>' data['element_path'] = childState.image.url elif element.type == 'video': if not 'video' in request.FILES: data['errors'] = 'No Video File is Provided. ' return ErrorResponse(data['errors'], format) childState = VideoState() childState.original_video = request.FILES['video'] childState.clean() if (childState.original_video.size > settings.MAX_UPLOAD_SIZE): data['errors'] = 'File is too large. ' + \ 'Max size allowed is '+ \ str(settings.MAX_UPLOAD_SIZE/1024.0/1024.0) + \ 'MB' return ErrorResponse(data['errors'], format) state.videostate = childState elif element.type == 'audio': if not 'audio' in request.FILES: data['errors'] = 'No Audio File is Provided. ' return ErrorResponse(data['errors'], format) childState = AudioState() childState.original_audio = request.FILES['audio'] #childState.full_clean() if (childState.original_audio.size > settings.MAX_UPLOAD_SIZE): data['errors'] = 'File is too large. ' + \ 'Max size allowed is '+ \ str(settings.MAX_UPLOAD_SIZE/1024.0/1024.0) + \ 'MB' return ErrorResponse(data['errors'], format) state.audiostate = childState elif element.type == 'text': childStateForm = TextStateForm(request.POST, prefix='text') if not childStateForm.is_valid(): data['errors'] = 'TextState data isn\'t valid: ' + str( childStateForm.errors) return ErrorResponse(data['errors'], format) childState = childStateForm.save(commit=False) state.textstate = childState data['element_content'] = childState.content # TODO: Handle other types of states. else: # no matching type data['errors'] = 'Element type isn\'t valid: ' + element.type return ErrorResponse(data['errors'], format) element.save() state.save() childState.save() if (element.type == "video"): if settings.VIDEO_CONVERT_SYNC: from videologue.management.commands.vlprocess import process_files process_files() else: os.system('python ' + settings.PROJECT_ROOT + '/manage.py vlprocess& 2>&1 1>>' + settings.LOG_FILENAME) data['element-id'] = element.id data['state-id'] = state.id data['child-id'] = childState.pk response = render_to_response( 'elements/wrapper.html', data, context_instance=RequestContext(request)) if format == 'html': return redirect(posterboard) elif format == 'json': data['message'] = 'Element created successfully.' data['content'] = response.content return HttpResponse(json.dumps(data), mimetype='application/json') else: data['errors'] = 'Element data isn\'t valid: ' + str( elementform.errors) #logger.debug('Errors creating Element: '+ data['errors']) return ErrorResponse(data['errors'], format) # Batch update elements elif request.method == 'POST' and request.POST.has_key('_action') and request.POST['_action'] == 'put' and \ blogger.id == user.id: elementForm = ElementForm(request.POST, prefix='element') stateForm = StateForm(request.POST, prefix='state') if elementForm.is_valid() and 'element-id' in request.POST: # Retrieve the actual element in the database and update actual_element = Element.objects.get(pk=request.POST['element-id']) edit_form = ElementForm(request.POST, prefix='element', instance=actual_element) edit_form.is_valid() # don't check as we checked above edit_form.save() else: data['message'] += ' Did not update element.' if stateForm.is_valid() and 'state-id' in request.POST: actual_state = State.objects.get(pk=request.POST['state-id']) edit_form = StateForm(request.POST, prefix='state', instance=actual_state) edit_form.is_valid() edit_form.save() else: data['message'] += ' Did not update state' if 'element-type' in request.POST and 'child-id' in request.POST: if request.POST['element-type'] == 'image': childStateForm = ImageStateForm(request.POST, prefix='image') if not childStateForm.is_valid(): data['errors'] = 'Image data isn\'t valid: ' + str( childStateForm.errors) return ErrorResponse(data['errors'], format) actual_image = ImageState.objects.get( pk=request.POST['child-id']) edit_form = ImageStateForm(request.POST, prefix='image', instance=actual_image) edit_form.is_valid() edit_form.save() if 'image' in request.FILES: actual_image.image = request.FILES['image'] if request.POST['element-type'] == 'video': childStateForm = VideoStateForm(request.POST, prefix='video') actual_video = VideoState.objects.get( pk=request.POST['child-id']) actual_video.clean() edit_form = VideoStateForm(request.POST, prefix='video', instance=actual_video) #if not edit_form.is_valid(): # data['errors'] = 'Video data isn\'t valid: ' + str(edit_form.errors) # return ErrorResponse(data['errors'], format) #edit_form.save() if 'video' in request.FILES: actual_video.original_video = request.FILES['video'] if request.POST['element-type'] == 'audio': childStateForm = AudioStateForm(request.POST, prefix='audio') actual_audio = AudioState.objects.get( pk=request.POST['child-id']) edit_form = AudioStateForm(request.POST, prefix='audio', instance=actual_audio) #if not edit_form.is_valid(): # data['errors'] = 'Audio data isn\'t valid: ' + str(edit_form.errors) # return ErrorResponse(data['errors'], format) #edit_form.save() if 'audio' in request.FILES: actual_audio.original_audio = request.FILES['audio'] elif request.POST['element-type'] == 'text': childStateForm = TextStateForm(request.POST, prefix='text') if not childStateForm.is_valid(): data['errors'] = 'TextState data isn\'t valid: ' + str( childStateForm.errors) return ErrorResponse(data['errors'], format) actual_text = TextState.objects.get( pk=request.POST['child-id']) edit_form = TextStateForm(request.POST, prefix='text', instance=actual_text) edit_form.is_valid() edit_form.save() else: data['message'] += ' Did not update any [type]State' if format == 'html': return redirect(posterboard) elif format == 'json': return HttpResponse(json.dumps(data), mimetype='application/json') # destroy elif request.method == 'GET' and request.GET.has_key('_action') and request.GET['_action']=='delete' and \ element is not None and blogger.id == user.id and element.posterboard_id == posterboard.id: data['message'] = 'Successfully removed element ' + str(element.id) element.delete() if format == 'html': return redirect(posterboard) elif format == 'json': return HttpResponse(json.dumps(data), mimetype='application/json') # All other types of requests are invalid for this specific scenario. error = {'errors': 'Invalid request'} return ErrorResponse(error, format)
def states_handler(request, blogger=None, posterboard=None, element=None, state=None, format='html'): user = request.user data = {"message": ""} if not user.id == blogger.id and user and blogger: return HttpResponseForbidden( 'Posterboard Only Available for Blogger to Edit') if posterboard is None or element is None: return HttpResponseBadRequest('Specify posterboard and element') if request.method == 'GET' and not request.GET.has_key('_action'): return HttpResponseBadRequest() # create elif request.method == 'POST' and not request.POST.has_key( '_action') and state is None: stateform = StateForm(request.POST, prefix='state') if stateform.is_valid(): state = stateform.save(commit=False) if state.order is None: try: max_order = State.objects.filter( pb_element=self.pb_element).aggregate( models.Max('order'))['order__max'] except: max_order = None if max_order is not None: self.order = max_order + 1 element.state_set.add(state) element.save() state.save() data['element-id'] = element.pk data['state-id'] = state.id if format == 'html': return redirect(posterboard) elif format == 'json': data['message'] = 'State created successfully.' data['state'] = jsonload(serializers.serialize( 'json', [state]))[0] return HttpResponse(json.dumps(data), mimetype='application/json') else: data['errors'] = 'State data isn\'t valid: ' + str( stateform.errors) #logger.debug('Errors creating Element: '+ data['errors']) return ErrorResponse(data['errors'], format) # destroy elif request.method == 'GET' and request.GET.has_key('_action') and request.GET['_action']=='delete' and \ element is not None and blogger.id == user.id and element.posterboard_id == posterboard.id and \ state is not None and state.pb_element_id == element.id: # Make sure element has more than one state. if element.state_set.count() <= 1: data['error'] = 'Cannot remove an element that has only one state' return ErrorResponse(data['error'], format) typestate = state.typestate() if typestate is not None: next_state = element.state_set.filter( order__gt=state.order).order_by('order', 'created_at')[0] typestate.state = next_state state.delete(delete_typestate=False) typestate.save() else: state.delete() data['message'] = 'Successfully removed state ' + str(state.id) if format == 'html': return redirect(posterboard) elif format == 'json': return HttpResponse(json.dumps(data), mimetype='application/json') # All other types of requests are invalid for this specific scenario. error = {'errors': 'Invalid request'} return ErrorResponse(error, format)
def elements_handler(request, blogger=None, posterboard=None, element=None, format="html"): user = request.user data = {"message": ""} if not user.id == blogger.id and user and blogger: return HttpResponseForbidden("Posterboard Only Available for Blogger to Edit") if request.method == "GET" and not request.GET.has_key("_action"): return HttpResponseBadRequest() # create elif request.method == "POST" and not request.POST.has_key("_action") and element is None: # Testing json response: # return HttpResponse(json.dumps({'test':1, 'again':2}), mimetype='application/json') elementform = ElementForm(request.POST, prefix="element") if elementform.is_valid(): # commit=False creates and returns the model object but doesn't save it. # Remove it if unnecessary. element = elementform.save(commit=False) if element.type != "image" and posterboard.is_user_home_page: return HttpResponseForbidden("Only adding images is allowed.") posterboard.element_set.add(element) stateform = StateForm(request.POST, prefix="state") if not stateform.is_valid(): data["errors"] = "State data isn't valid: " + str(stateform.errors) return ErrorResponse(data["errors"], format) state = stateform.save(commit=False) state.speed = 1 # Appear super fast. state.order = 1 element.state_set.add(state) if element.type == "image": if not "image" in request.FILES: data["errors"] = "No Image File is Provided. " return ErrorResponse(data["errors"], format) childState = ImageState() childState.image = request.FILES["image"] if request.POST.has_key("linked-posterboard"): linkpb = request.POST["linked-posterboard"] if linkpb is not None: pblist = user.posterboard_set.filter(is_user_home_page=False, title=linkpb).all() if len(pblist) > 0: childState.linkedposterboard = pblist[0] else: data["errors"] = "Invalid posterboard." return ErrorResponse(data["errors"], format) else: data["errors"] = "No posterboard to link to provided." return ErrorResponse(data["errors"], format) childState.full_clean() state.imagestate = childState # Write the element_content, which should be an image data["element_content"] = ( '<img src= "' + childState.image.url + '"' 'alt="' + childState.alt + '"' + ">" ) data["element_path"] = childState.image.url elif element.type == "video": if not "video" in request.FILES: data["errors"] = "No Video File is Provided. " return ErrorResponse(data["errors"], format) childState = VideoState() childState.original_video = request.FILES["video"] childState.clean() if childState.original_video.size > settings.MAX_UPLOAD_SIZE: data["errors"] = ( "File is too large. " + "Max size allowed is " + str(settings.MAX_UPLOAD_SIZE / 1024.0 / 1024.0) + "MB" ) return ErrorResponse(data["errors"], format) state.videostate = childState elif element.type == "audio": if not "audio" in request.FILES: data["errors"] = "No Audio File is Provided. " return ErrorResponse(data["errors"], format) childState = AudioState() childState.original_audio = request.FILES["audio"] # childState.full_clean() if childState.original_audio.size > settings.MAX_UPLOAD_SIZE: data["errors"] = ( "File is too large. " + "Max size allowed is " + str(settings.MAX_UPLOAD_SIZE / 1024.0 / 1024.0) + "MB" ) return ErrorResponse(data["errors"], format) state.audiostate = childState elif element.type == "text": childStateForm = TextStateForm(request.POST, prefix="text") if not childStateForm.is_valid(): data["errors"] = "TextState data isn't valid: " + str(childStateForm.errors) return ErrorResponse(data["errors"], format) childState = childStateForm.save(commit=False) state.textstate = childState data["element_content"] = childState.content # TODO: Handle other types of states. else: # no matching type data["errors"] = "Element type isn't valid: " + element.type return ErrorResponse(data["errors"], format) element.save() state.save() childState.save() if element.type == "video": if settings.VIDEO_CONVERT_SYNC: from videologue.management.commands.vlprocess import process_files process_files() else: os.system( "python " + settings.PROJECT_ROOT + "/manage.py vlprocess& 2>&1 1>>" + settings.LOG_FILENAME ) data["element-id"] = element.id data["state-id"] = state.id data["child-id"] = childState.pk response = render_to_response("elements/wrapper.html", data, context_instance=RequestContext(request)) if format == "html": return redirect(posterboard) elif format == "json": data["message"] = "Element created successfully." data["content"] = response.content return HttpResponse(json.dumps(data), mimetype="application/json") else: data["errors"] = "Element data isn't valid: " + str(elementform.errors) # logger.debug('Errors creating Element: '+ data['errors']) return ErrorResponse(data["errors"], format) # Batch update elements elif ( request.method == "POST" and request.POST.has_key("_action") and request.POST["_action"] == "put" and blogger.id == user.id ): elementForm = ElementForm(request.POST, prefix="element") stateForm = StateForm(request.POST, prefix="state") if elementForm.is_valid() and "element-id" in request.POST: # Retrieve the actual element in the database and update actual_element = Element.objects.get(pk=request.POST["element-id"]) edit_form = ElementForm(request.POST, prefix="element", instance=actual_element) edit_form.is_valid() # don't check as we checked above edit_form.save() else: data["message"] += " Did not update element." if stateForm.is_valid() and "state-id" in request.POST: actual_state = State.objects.get(pk=request.POST["state-id"]) edit_form = StateForm(request.POST, prefix="state", instance=actual_state) edit_form.is_valid() edit_form.save() else: data["message"] += " Did not update state" if "element-type" in request.POST and "child-id" in request.POST: if request.POST["element-type"] == "image": childStateForm = ImageStateForm(request.POST, prefix="image") if not childStateForm.is_valid(): data["errors"] = "Image data isn't valid: " + str(childStateForm.errors) return ErrorResponse(data["errors"], format) actual_image = ImageState.objects.get(pk=request.POST["child-id"]) edit_form = ImageStateForm(request.POST, prefix="image", instance=actual_image) edit_form.is_valid() edit_form.save() if "image" in request.FILES: actual_image.image = request.FILES["image"] if request.POST["element-type"] == "video": childStateForm = VideoStateForm(request.POST, prefix="video") actual_video = VideoState.objects.get(pk=request.POST["child-id"]) actual_video.clean() edit_form = VideoStateForm(request.POST, prefix="video", instance=actual_video) # if not edit_form.is_valid(): # data['errors'] = 'Video data isn\'t valid: ' + str(edit_form.errors) # return ErrorResponse(data['errors'], format) # edit_form.save() if "video" in request.FILES: actual_video.original_video = request.FILES["video"] if request.POST["element-type"] == "audio": childStateForm = AudioStateForm(request.POST, prefix="audio") actual_audio = AudioState.objects.get(pk=request.POST["child-id"]) edit_form = AudioStateForm(request.POST, prefix="audio", instance=actual_audio) # if not edit_form.is_valid(): # data['errors'] = 'Audio data isn\'t valid: ' + str(edit_form.errors) # return ErrorResponse(data['errors'], format) # edit_form.save() if "audio" in request.FILES: actual_audio.original_audio = request.FILES["audio"] elif request.POST["element-type"] == "text": childStateForm = TextStateForm(request.POST, prefix="text") if not childStateForm.is_valid(): data["errors"] = "TextState data isn't valid: " + str(childStateForm.errors) return ErrorResponse(data["errors"], format) actual_text = TextState.objects.get(pk=request.POST["child-id"]) edit_form = TextStateForm(request.POST, prefix="text", instance=actual_text) edit_form.is_valid() edit_form.save() else: data["message"] += " Did not update any [type]State" if format == "html": return redirect(posterboard) elif format == "json": return HttpResponse(json.dumps(data), mimetype="application/json") # destroy elif ( request.method == "GET" and request.GET.has_key("_action") and request.GET["_action"] == "delete" and element is not None and blogger.id == user.id and element.posterboard_id == posterboard.id ): data["message"] = "Successfully removed element " + str(element.id) element.delete() if format == "html": return redirect(posterboard) elif format == "json": return HttpResponse(json.dumps(data), mimetype="application/json") # All other types of requests are invalid for this specific scenario. error = {"errors": "Invalid request"} return ErrorResponse(error, format)
def states_handler(request, blogger=None, posterboard=None, element=None, state=None, format="html"): user = request.user data = {"message": ""} if not user.id == blogger.id and user and blogger: return HttpResponseForbidden("Posterboard Only Available for Blogger to Edit") if posterboard is None or element is None: return HttpResponseBadRequest("Specify posterboard and element") if request.method == "GET" and not request.GET.has_key("_action"): return HttpResponseBadRequest() # create elif request.method == "POST" and not request.POST.has_key("_action") and state is None: stateform = StateForm(request.POST, prefix="state") if stateform.is_valid(): state = stateform.save(commit=False) if state.order is None: try: max_order = State.objects.filter(pb_element=self.pb_element).aggregate(models.Max("order"))[ "order__max" ] except: max_order = None if max_order is not None: self.order = max_order + 1 element.state_set.add(state) element.save() state.save() data["element-id"] = element.pk data["state-id"] = state.id if format == "html": return redirect(posterboard) elif format == "json": data["message"] = "State created successfully." data["state"] = jsonload(serializers.serialize("json", [state]))[0] return HttpResponse(json.dumps(data), mimetype="application/json") else: data["errors"] = "State data isn't valid: " + str(stateform.errors) # logger.debug('Errors creating Element: '+ data['errors']) return ErrorResponse(data["errors"], format) # destroy elif ( request.method == "GET" and request.GET.has_key("_action") and request.GET["_action"] == "delete" and element is not None and blogger.id == user.id and element.posterboard_id == posterboard.id and state is not None and state.pb_element_id == element.id ): # Make sure element has more than one state. if element.state_set.count() <= 1: data["error"] = "Cannot remove an element that has only one state" return ErrorResponse(data["error"], format) typestate = state.typestate() if typestate is not None: next_state = element.state_set.filter(order__gt=state.order).order_by("order", "created_at")[0] typestate.state = next_state state.delete(delete_typestate=False) typestate.save() else: state.delete() data["message"] = "Successfully removed state " + str(state.id) if format == "html": return redirect(posterboard) elif format == "json": return HttpResponse(json.dumps(data), mimetype="application/json") # All other types of requests are invalid for this specific scenario. error = {"errors": "Invalid request"} return ErrorResponse(error, format)