def render_upload_page(eventName):
  x = dbMgr.isEventPresent(eventName)
  if (x == 1):
    x = dbMgr.isEventPublished(eventName)
    #handleOldEvents
    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)
    
    try:
      oldEventName = session['eventName']
      if oldEventName != eventName:
        dbMgr.decMemberCount(session['eventName'])
      else:
        dbMgr.decMemberCount(eventName)
    except:
      app.logger.debug('render_upload_page - No eventName in session')
    finally:
      session['eventName'] = eventName
      dbMgr.incMemberCount(eventName)
      images = []
      images = dbMgr.getImageNames(eventName)
      return render_template('15610_Lab4.html', eventName=eventName, images=images)
  return render_template('15610_Lab4.html', errMsg='Event does not exist. Create new event')
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 waitForPublishResult():
  app.logger.debug('waitForPublishCount called')
  eventName = session['eventName']
  eventMgr.waitForPublishResult(eventName)
  x = dbMgr.isEventPublished(eventName)
  if x == 1:
    app.logger.debug('waitForPublishCount : published')
    return jsonify(publish='publish')
  elif x == 2:
    app.logger.debug('waitForPublishCount : not published')
    return jsonify(notpublish='notpublish')
  else:
    app.logger.debug('waitForPublishCount : new Image')
    dbMgr.resetYesCount(eventName)
    dbMgr.resetNoCount (eventName)
    images = []
    images = dbMgr.getImageNames(eventName)
    return jsonify(images=images)