예제 #1
0
    def post(self, hurl=None):
        user = users.get_current_user()
        hunt = Hunts.query(Hunts.hurl == hurl).get()
        title = self.request.get('title', None)

        if not hunt:
            logging.error('Hunt not found')
            self.response(400)
            return

        # User must be in the current hunt to create docs.
        if not self.CheckHunterInHunt(user, hunt):
            logging.error('User not in hunt.')
            self.error(403)
            return

        # Validate title
        if re.match(r'^[\w\s]{1,128}$', title) == None:
            logging.error('Invalid doc title')
            self.error(400)
            return

        # Setup a service object to talk to the Drive API.
        scopes = [
            'https://www.googleapis.com/auth/drive',
            'https://www.googleapis.com/auth/drive.appdata',
            'https://www.googleapis.com/auth/drive.apps.readonly',
            'https://www.googleapis.com/auth/drive.file',
            'https://www.googleapis.com/auth/drive.metadata.readonly',
            'https://www.googleapis.com/auth/drive.readonly',
            'https://www.googleapis.com/auth/drive.scripts',
            'https://www.googleapis.com/auth/userinfo.email',
            'https://www.googleapis.com/auth/userinfo.profile',
        ]

        client_email = '*****@*****.**'
        with open('../puzzlebuddy-b7c415ad8eaf.pem') as f:
            private_key = f.read()
        credentials = SignedJwtAssertionCredentials(client_email, private_key,
                                                    scopes)

        if credentials is None or credentials.invalid:
            logging.error('Puzbud credentials failed to load or were invalid')
        http = Http()
        credentials.authorize(http)
        service = discovery.build('drive', 'v2', http=http)

        # Create a new doc.
        body = {
            'title': title,
            'mimeType': 'application/vnd.google-apps.spreadsheet',
            'parents': [{
                'id': hunt.shared_folder_id
            }],
        }
        # TODO(jonlesser): Catch AccessTokenRefreshError exceptions when executing.
        doc = service.files().insert(body=body).execute()

        resp = {'file_id': doc['id']}
        self.response.out.write(json.dumps(resp))
예제 #2
0
  def post(self, hurl=None):
    user = users.get_current_user()
    hunt = Hunts.query(Hunts.hurl == hurl).get()
    title = self.request.get('title', None)

    if not hunt:
      logging.error('Hunt not found')
      self.response(400)
      return

    # User must be in the current hunt to create docs.
    if not self.CheckHunterInHunt(user, hunt):
      logging.error('User not in hunt.')
      self.error(403)
      return

    # Validate title
    if re.match(r'^[\w\s]{1,128}$', title) == None:
      logging.error('Invalid doc title')
      self.error(400)
      return

    # Setup a service object to talk to the Drive API.
    scopes = [
        'https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.appdata',
        'https://www.googleapis.com/auth/drive.apps.readonly',
        'https://www.googleapis.com/auth/drive.file',
        'https://www.googleapis.com/auth/drive.metadata.readonly',
        'https://www.googleapis.com/auth/drive.readonly',
        'https://www.googleapis.com/auth/drive.scripts',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile',
    ]

    client_email = '*****@*****.**'
    with open('../puzzlebuddy-b7c415ad8eaf.pem') as f:
      private_key = f.read()
    credentials = SignedJwtAssertionCredentials(client_email, private_key, scopes)

    if credentials is None or credentials.invalid:
      logging.error('Puzbud credentials failed to load or were invalid')
    http = Http()
    credentials.authorize(http)
    service = discovery.build('drive', 'v2', http=http)

    # Create a new doc.
    body = {
      'title': title,
      'mimeType': 'application/vnd.google-apps.spreadsheet',
      'parents': [{'id': hunt.shared_folder_id}],
    }
    # TODO(jonlesser): Catch AccessTokenRefreshError exceptions when executing.
    doc = service.files().insert(body=body).execute()

    resp = {'file_id': doc['id']}
    self.response.out.write(json.dumps(resp))
예제 #3
0
  def post(self):
    user = users.get_current_user()
    name = self.request.get('name', '')
    hurl = self.request.get('hurl', '')

    # Serverside validation of form fields.
    if re.match(r'^[a-zA-Z0-9]{4,32}$', hurl) == None:
      logging.warning('Invalid hurl submitted. JS validation must have failed.')
      self.redirect_to('hunt_list')
      return

    if re.match(r'^[\w\s]{1,64}$', name) == None:
      logging.warning('Invalid name submitted. JS validation must have failed.')
      self.redirect_to('hunt_list')
      return

    # Make sure there isn't already a hunt with this hurl.
    existing = Hunts.query(Hunts.hurl == hurl).count(limit=1, keys_only=True)
    if existing > 0:
      logging.warning('Attempting to create a hunt with existing hurl')
      self.redirect_to('hunt_list')
      return

    # Setup a service object to talk to the Drive API.
    scopes = [
        'https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.appdata',
        'https://www.googleapis.com/auth/drive.apps.readonly',
        'https://www.googleapis.com/auth/drive.file',
        'https://www.googleapis.com/auth/drive.metadata.readonly',
        'https://www.googleapis.com/auth/drive.readonly',
        'https://www.googleapis.com/auth/drive.scripts',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/userinfo.profile',
    ]

    client_email = '*****@*****.**'
    with open('puzzlebuddy-b7c415ad8eaf.pem') as f:
      private_key = f.read()
    credentials = SignedJwtAssertionCredentials(client_email, private_key, scopes)

    if credentials is None or credentials.invalid:
      logging.error('Puzbud credentials failed to load or were invalid')
    http = Http()
    credentials.authorize(http)
    service = discovery.build('drive', 'v2', http=http)

    # Create a folder to be shared by the team and contain all docs.
    body = {
      'title': hurl,
      'mimeType': 'application/vnd.google-apps.folder',
    }
    # TODO(jonlesser): Catch AccessTokenRefreshError exceptions when executing.
    doc = service.files().insert(body=body).execute()
    folder_id = doc['id']

    # Create a new Realtime Doc.
    body = {
      'title': '__%s' % hurl,
      'mimeType': 'application/vnd.google-apps.drive-sdk',
      'parents': [{'id': folder_id}],
    }
    doc = service.files().insert(body=body).execute()
    file_id = doc['id']

    # Share new folder with creator.
    body = {
      'value': user.email(),
      'type': 'user',
      'role': 'writer',
    }
    service.permissions().insert(fileId=folder_id,
                                 sendNotificationEmails=False,
                                 body=body).execute()

    # Create a new model with the Realtime Doc id.
    hunt_model = Hunts()
    hunt_model.hurl = hurl
    hunt_model.name = name
    hunt_model.rt_file_id = file_id
    hunt_model.shared_folder_id = folder_id
    hunt_model.hunters = [user]
    hunt_model.put()

    # Direct the user the hunt dashboard
    self.redirect_to('hunt_dashboard', hurl=hurl)
예제 #4
0
    def post(self):
        user = users.get_current_user()
        name = self.request.get('name', '')
        hurl = self.request.get('hurl', '')

        # Serverside validation of form fields.
        if re.match(r'^[a-zA-Z0-9]{4,32}$', hurl) == None:
            logging.warning(
                'Invalid hurl submitted. JS validation must have failed.')
            self.redirect_to('hunt_list')
            return

        if re.match(r'^[\w\s]{1,64}$', name) == None:
            logging.warning(
                'Invalid name submitted. JS validation must have failed.')
            self.redirect_to('hunt_list')
            return

        # Make sure there isn't already a hunt with this hurl.
        existing = Hunts.query(Hunts.hurl == hurl).count(limit=1,
                                                         keys_only=True)
        if existing > 0:
            logging.warning('Attempting to create a hunt with existing hurl')
            self.redirect_to('hunt_list')
            return

        # Setup a service object to talk to the Drive API.
        scopes = [
            'https://www.googleapis.com/auth/drive',
            'https://www.googleapis.com/auth/drive.appdata',
            'https://www.googleapis.com/auth/drive.apps.readonly',
            'https://www.googleapis.com/auth/drive.file',
            'https://www.googleapis.com/auth/drive.metadata.readonly',
            'https://www.googleapis.com/auth/drive.readonly',
            'https://www.googleapis.com/auth/drive.scripts',
            'https://www.googleapis.com/auth/userinfo.email',
            'https://www.googleapis.com/auth/userinfo.profile',
        ]

        client_email = '*****@*****.**'
        with open('puzzlebuddy-b7c415ad8eaf.pem') as f:
            private_key = f.read()
        credentials = SignedJwtAssertionCredentials(client_email, private_key,
                                                    scopes)

        if credentials is None or credentials.invalid:
            logging.error('Puzbud credentials failed to load or were invalid')
        http = Http()
        credentials.authorize(http)
        service = discovery.build('drive', 'v2', http=http)

        # Create a folder to be shared by the team and contain all docs.
        body = {
            'title': hurl,
            'mimeType': 'application/vnd.google-apps.folder',
        }
        # TODO(jonlesser): Catch AccessTokenRefreshError exceptions when executing.
        doc = service.files().insert(body=body).execute()
        folder_id = doc['id']

        # Create a new Realtime Doc.
        body = {
            'title': '__%s' % hurl,
            'mimeType': 'application/vnd.google-apps.drive-sdk',
            'parents': [{
                'id': folder_id
            }],
        }
        doc = service.files().insert(body=body).execute()
        file_id = doc['id']

        # Share new folder with creator.
        body = {
            'value': user.email(),
            'type': 'user',
            'role': 'writer',
        }
        service.permissions().insert(fileId=folder_id,
                                     sendNotificationEmails=False,
                                     body=body).execute()

        # Create a new model with the Realtime Doc id.
        hunt_model = Hunts()
        hunt_model.hurl = hurl
        hunt_model.name = name
        hunt_model.rt_file_id = file_id
        hunt_model.shared_folder_id = folder_id
        hunt_model.hunters = [user]
        hunt_model.put()

        # Direct the user the hunt dashboard
        self.redirect_to('hunt_dashboard', hurl=hurl)
예제 #5
0
    def get(self, hurl=None):
        user = users.get_current_user()
        hunt = Hunts.query(Hunts.hurl == hurl).get()

        # Make sure we have a hunt.
        if not hunt:
            self.error(404)
            self.response.out.write('Hunt not found')
            return

        if not self.CheckHunterInHunt(user, hunt):
            # Setup a service object to talk to the Drive API.
            scopes = [
                'https://www.googleapis.com/auth/drive',
                'https://www.googleapis.com/auth/drive.appdata',
                'https://www.googleapis.com/auth/drive.apps.readonly',
                'https://www.googleapis.com/auth/drive.file',
                'https://www.googleapis.com/auth/drive.metadata.readonly',
                'https://www.googleapis.com/auth/drive.readonly',
                'https://www.googleapis.com/auth/drive.scripts',
                'https://www.googleapis.com/auth/userinfo.email',
                'https://www.googleapis.com/auth/userinfo.profile',
            ]

            client_email = '*****@*****.**'
            with open('../puzzlebuddy-b7c415ad8eaf.pem') as f:
                private_key = f.read()
            credentials = SignedJwtAssertionCredentials(
                client_email, private_key, scopes)

            if credentials is None or credentials.invalid:
                logging.error(
                    'Puzbud credentials failed to load or were invalid')
            http = Http()
            credentials.authorize(http)
            service = discovery.build('drive', 'v2', http=http)
            body = {
                'value': user.email(),
                'type': 'user',
                'role': 'writer',
            }
            service.permissions().insert(fileId=hunt.shared_folder_id,
                                         sendNotificationEmails=False,
                                         body=body).execute()

            # Add user to hunt model.
            hunt.hunters.append(user)
            hunt.put()

        debug = os.environ["SERVER_SOFTWARE"].startswith('Development')
        if self.request.get('nodebug'):
            debug = False

        templates = jinja2.Environment(
            loader=jinja2.FileSystemLoader('templates'))
        template = templates.get_template('hunt_dashboard.html')
        self.response.headers.add('X-UA-Compatible', 'IE=edge')
        self.response.out.write(
            template.render({
                'dev': debug,
                'hunt': hunt,
                'hurl': hunt.hurl,
                'name': hunt.name,
                'rt_file_id': hunt.rt_file_id,
                'shared_folder_id': hunt.shared_folder_id,
            }))
예제 #6
0
  def get(self, hurl=None):
    user = users.get_current_user()
    hunt = Hunts.query(Hunts.hurl == hurl).get()

    # Make sure we have a hunt.
    if not hunt:
      self.error(404)
      self.response.out.write('Hunt not found')
      return

    if not self.CheckHunterInHunt(user, hunt):
      # Setup a service object to talk to the Drive API.
      scopes = [
          'https://www.googleapis.com/auth/drive',
          'https://www.googleapis.com/auth/drive.appdata',
          'https://www.googleapis.com/auth/drive.apps.readonly',
          'https://www.googleapis.com/auth/drive.file',
          'https://www.googleapis.com/auth/drive.metadata.readonly',
          'https://www.googleapis.com/auth/drive.readonly',
          'https://www.googleapis.com/auth/drive.scripts',
          'https://www.googleapis.com/auth/userinfo.email',
          'https://www.googleapis.com/auth/userinfo.profile',
      ]

      client_email = '*****@*****.**'
      with open('../puzzlebuddy-b7c415ad8eaf.pem') as f:
        private_key = f.read()
      credentials = SignedJwtAssertionCredentials(client_email, private_key, scopes)

      if credentials is None or credentials.invalid:
        logging.error('Puzbud credentials failed to load or were invalid')
      http = Http()
      credentials.authorize(http)
      service = discovery.build('drive', 'v2', http=http)
      body = {
        'value': user.email(),
        'type': 'user',
        'role': 'writer',
      }
      service.permissions().insert(fileId=hunt.shared_folder_id,
                                   sendNotificationEmails=False,
                                   body=body).execute()

      # Add user to hunt model.
      hunt.hunters.append(user)
      hunt.put()

    debug = os.environ["SERVER_SOFTWARE"].startswith('Development')
    if self.request.get('nodebug'):
      debug = False

    templates = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
    template = templates.get_template('hunt_dashboard.html')
    self.response.headers.add('X-UA-Compatible', 'IE=edge')
    self.response.out.write(template.render({
      'dev': debug,
      'hunt': hunt,
      'hurl': hunt.hurl,
      'name': hunt.name,
      'rt_file_id': hunt.rt_file_id,
      'shared_folder_id': hunt.shared_folder_id,
    }))