Exemplo n.º 1
0
def user_create():
  access_token = request.form['access_token']

  # TODO: check for an existing user here

  fb_service = FacebookService(access_token)
  user_details = fb_service.fetch_details()
  name = user_details['name']
  uid = user_details['id']
  email = "*****@*****.**" # TODO: fetch this using the fb service
  photos = fb_service.fetch_all_photos()

  # save the user to db
  user_service = UserService()
  # TODO: fix the field expires
  user_id = user_service.create(name, uid, email, {"token": access_token, "expires": "never"}, db, cursor)
  user_id = int(user_id)


  file_service = FileUtility()
  photo_service = PhotoService()
  image_utility = ImageUtility()

  for photo in photos:
    extension = "jpg" # TODO: extract proper file extension here

    filepath = path.join(getcwd(), "static/original/" + photo['id'] + "." + extension)
    file_service.download(photo['src'],
      filepath
      )

    # copy,crop and save photo
    copy_filepath = path.join(getcwd(), "static/cropped/" + photo['id'] + "." + extension)
    original = Image.open(filepath)

    cropped = image_utility.crop(original)
    cropped.save(copy_filepath)

    photo_service.insert_into_db({
        'fb_id': photo['id'],
        'filename': photo['id'] + "." + extension,
        'caption': photo['caption'],
        'owner_id': user_id
      }, db, cursor)

  return Response(json.dumps({"action_status": True}),
      mimetype='application/json')