def upload_photo():
    error = None
    try:
      eventName = session['eventName']
      x = dbMgr.isEventPresent(eventName)
      if x != 1:
        return render_template('15610_Lab4.html', errMsg='Invalid Session. Join new Event by entering valid link')
      #handleOldEvents
      x = dbMgr.isEventPublished(eventName)
      app.logger.debug('handleOldEvents : ' + str(x))
      images = []
      images = dbMgr.getImageNames(eventName)
      if x == 1:
        err='EventName - ' + eventName + ' already published'
        return render_template('15610_Lab4.html', errMsg=err, images=images)
      elif x == 2:
        err='EventName - ' + eventName + ' not published'
        return render_template('15610_Lab4.html', errMsg=err, images=images)

      f=request.files['photo']
      filename = secure_filename(f.filename)
      x = allowedFile (f.filename)
      if x == 0:
        err= 'Invalid file format'
        return render_template('15610_Lab4.html', errMsg=err, eventName=eventName, images=images)


      saveToPath='/home/psachdev/15610/lab4/static/'+ filename
      f.save(saveToPath)

      #Check Peers if its fine to upload
      y = addNewImage (eventName, filename, saveToPath)
      if y == 0:
        return render_template('15610_Lab4.html', eventName=eventName, errMsg='Duplicate Name found. Upload image with different filename')

      
      renderPath='/static/'+ filename
      updateOtherMembers(eventName)
      images = []
      images = dbMgr.getImageNames(eventName)
      if images == None:
        return render_template('15610_Lab4.html', eventName=eventName, errMsg='Unable to upload Image. Upload image with different filename')

      x = startEventTimer(eventName)
      if x == -1:
        srvMgr.releaseUploadPromise (eventName, imageName)
        dbMgr.deleteImageName (imageName)
        return render_template('15610_Lab4.html', eventName=eventName, errMsg='Unable to start timer')

      app.logger.debug('upload_photo - success')
      return render_template('15610_Lab4.html', eventName=request.form['eventName'], images=images, vote='vote')
    except Exception, e:
      return render_template('15610_Lab4.html', errMsg=e.args[0])
def addNewImage (eventName, imageName, imagePath):
  x = dbMgr.isImageNamePresent(imageName)
  if x == 0:
    x = srvMgr.canIUpload (eventName, imageName)
    if x == 1:
      x = dbMgr.insertNewImage(imageName, eventName)
      if x == 0:
        y = srvMgr.confirmUpload (eventName, imageName, imagePath)
        if y == 0:
          return 1
        else:
          srvMgr.releaseUploadPromise (eventName, imageName)
          dbMgr.deleteImageName (imageName)
    else:
      srvMgr.releaseUploadPromise (eventName, imageName)
  return 0
def releaseUploadPromise (eventName):
  imageName = request.form ['inputParam']
  x = dbMgr.deleteImagePromise (imageName)
  y = dbMgr.deleteImageName (imageName)
  return jsonify (status = 'True')