Example #1
0
    def handle(self, *args, **options):
        # Override backup with command line arg value
        backup_name = options.get('backup')

        if backup_name:
            if not isinstance(backup_name, str):
                print('Incorrect argument type')
                return None
            self.backup = backup_name

        if self.backup in ["False", "false"]:
            print('Skipping Backup')
            return None

        new_aws_key_name = '%s/mermaid_backup_%s.%s' % (
            self.backup, simpleflake(), BACKUP_EXTENSION)
        new_backup_filename = '%s_mermaid_backup_%s.%s' % (
            self.backup, simpleflake(), BACKUP_EXTENSION)
        new_backup_path = os.path.join(self.local_file_location,
                                       new_backup_filename)
        self._pg_dump(new_backup_path)

        if options.get('no_upload', False) is False:
            print('Uploading {0} to S3 bucket {1}'.format(
                new_aws_key_name, AWS_BACKUP_BUCKET))
            self.s3.upload_file(new_backup_path, AWS_BACKUP_BUCKET,
                                new_aws_key_name)
            print('Backup Complete')
Example #2
0
 def test_simpleflake_parts_timestamp(self):
     coolstamp = SIMPLEFLAKE_EPOCH + 1234321.0
     epoch_flake = simpleflake(timestamp=coolstamp)
     timestamp = extract_bits(epoch_flake,
                              SIMPLEFLAKE_TIMESTAMP_SHIFT,
                              SIMPLEFLAKE_TIMESTAMP_LENGTH)
     self.assertEquals(coolstamp, (timestamp / 1000.0) + SIMPLEFLAKE_EPOCH)
Example #3
0
def dashboard_send():
  app = get_app(g.app_id)
  survey_id = create_survey(g.app_id)
  survey = get_survey(g.app_id, survey_id)
  users = get_users(g.app_id)
  sender = str(app["shortcode"])[-4:]
  message = "%s\nREPLY WITH %s" % (
    survey["question"],
    "/".join(get_choices(g.app_id, survey_id)),
  )
  if users:
    for user in users:
      access_token = get_user_token(g.app_id, user)
      params = dict(access_token=access_token)
      payload = json.dumps(dict(
          outboundSMSMessageRequest=dict(
          clientCorrelator=simpleflake(),
          senderAddress="tel:%s" % sender,
          outboundSMSTextMessage=dict(
            message=message,
          ),
          address=["tel:+63%s" % user],
        ),
      ))
      # TODO: Check response
      requests.post(
        "http://devapi.globelabs.com.ph/smsmessaging/v1/outbound/%s/requests" % sender,
        headers={"Content-Type": "application/json"},
        params=params,
        data=payload,
      )
    flash("Sent survey to %d users" % len(users), "info")
  else:
    flash("No users subscribed yet", "error")
  return redirect(url_for("dashboard"))
Example #4
0
 def test_simpleflake_parts_random(self):
     random_bits = random.getrandbits(5)
     flake = sf.simpleflake(random_bits=random_bits)
     rand_result = sf.extract_bits(flake,
                                   sf.SIMPLEFLAKE_RANDOM_SHIFT,
                                   sf.SIMPLEFLAKE_RANDOM_LENGTH)
     self.assertEquals(random_bits, rand_result)
Example #5
0
    def post(self):
        user = self.get_current_user()

        trip = dict()
        trip_fields = {
            'start_date': 'start_date',
            'end_date': 'end_date',
            'description': 'description',
            'place_id': 'place_id',
            'address': 'formatted_address',
            'locality': 'locality',
            'region': 'administrative_area_level_1',
            'county': 'administrative_area_level_2',
            'longitude': 'lng',
            'latitude': 'lat'
        }

        for key in trip_fields:
            trip[key] = self.get_argument(trip_fields[key], None)

        trip_uuid = simpleflake()
        trip['trip_id'] = base62().hash(trip_uuid, 12)

        trip['created_at'] = r.now()
        trip['updated_at'] = r.now()
        trip['creator_user_id'] = user['id']

        trip['geo'] = r.point(float(trip['longitude']),
                              float(trip['latitude']))

        r.table("trip").insert(trip).run()
        self.redirect("/")
Example #6
0
  def __send(self, message_type, args, message):
    data = dict(
      message_type=message_type,
      mobile_number=args.mobile_number,
      shortcode=args.shortcode,
      message_id=str(simpleflake()),
      message=message,
      client_id=current_app.config["CHIKKA_CLIENT_ID"],
      secret_key=current_app.config["CHIKKA_SECRET_KEY"],
    )
    if message_type == "REPLY":
      data.update(
        request_id=args.request_id,
        request_cost="FREE",
      )

    current_app.logger.debug("Sending message: %r", data)
    res = requests.post(CHIKKA_REPLY_ENDPOINT, data=data)
    current_app.logger.debug("""
Status code: %r
Body: %r
""", res.status_code, res.content)

    if res.status_code != requests.codes.ok:
      abort(500)
Example #7
0
class Base:
    id = db.Column(db.Unicode(),
                   primary_key=True,
                   default=lambda: str(simpleflake()))

    @classmethod
    async def exists(cls: db.Model, id_: str) -> bool:
        return bool(await cls.select('id').where(cls.id == id_).gino.scalar())

    @classmethod
    def get_any(cls: db.Model,
                insensitive: Union[bool, List[str]] = False,
                **kwargs) -> Gino:
        if not kwargs:
            raise ValueError('No kwargs provided')

        queries = []

        if isinstance(insensitive, list):
            for k, v in kwargs:
                if k in insensitive:
                    queries.push(func.lower(getattr(cls, k)) == func.lower(v))
                else:
                    queries.push(getattr(cls, k) == v)
        elif insensitive is True:
            queries = [
                func.lower(getattr(cls, k)) == func.lower(v)
                for k, v in kwargs.items()
            ]
        else:
            queries = [getattr(cls, k) == v for k, v in kwargs.items()]

        return cls.query.where(or_(*queries)).gino
Example #8
0
 def test_simpleflake_parts_random(self):
     random_bits = random.getrandbits(5)
     flake = sf.simpleflake(random_bits=random_bits)
     rand_result = sf.extract_bits(flake,
                                   sf.SIMPLEFLAKE_RANDOM_SHIFT,
                                   sf.SIMPLEFLAKE_RANDOM_LENGTH)
     self.assertEquals(random_bits, rand_result)
Example #9
0
  def post(self):
    user = self.get_current_user()

    trip = dict()
    trip_fields = {
      'start_date': 'start_date', 
      'end_date': 'end_date', 
      'description': 'description', 
      'place_id': 'place_id',
      'address': 'formatted_address',
      'locality': 'locality',
      'region': 'administrative_area_level_1',
      'county': 'administrative_area_level_2',
      'longitude': 'lng',
      'latitude': 'lat'
      }

    for key in trip_fields:
      trip[key] = self.get_argument(trip_fields[key], None)

    trip_uuid = simpleflake()
    trip['trip_id'] = base62().hash(trip_uuid, 12)

    trip['created_at'] = r.now()
    trip['updated_at'] = r.now()
    trip['creator_user_id'] = user['id']

    trip['geo'] = r.point(float(trip['longitude']), float(trip['latitude']))

    r.table("trip").insert(trip).run()
    self.redirect("/")
Example #10
0
    def __send(self, message_type, args, message):
        data = dict(
            message_type=message_type,
            mobile_number=args.mobile_number,
            shortcode=args.shortcode,
            message_id=str(simpleflake()),
            message=message,
            client_id=current_app.config["CHIKKA_CLIENT_ID"],
            secret_key=current_app.config["CHIKKA_SECRET_KEY"],
        )
        if message_type == "REPLY":
            data.update(
                request_id=args.request_id,
                request_cost="FREE",
            )

        current_app.logger.debug("Sending message: %r", data)
        res = requests.post(CHIKKA_REPLY_ENDPOINT, data=data)
        current_app.logger.debug("""
Status code: %r
Body: %r
""", res.status_code, res.content)

        if res.status_code != requests.codes.ok:
            abort(500)
Example #11
0
 def test_simpleflake_parts_timestamp(self):
     coolstamp = sf.SIMPLEFLAKE_EPOCH + 1234321.0
     epoch_flake = sf.simpleflake(timestamp=coolstamp)
     timestamp = sf.extract_bits(epoch_flake,
                                 sf.SIMPLEFLAKE_TIMESTAMP_SHIFT,
                                 sf.SIMPLEFLAKE_TIMESTAMP_LENGTH)
     self.assertEquals(coolstamp,
                       (timestamp / 1000.0) + sf.SIMPLEFLAKE_EPOCH)
Example #12
0
 def test_parse(self):
     coolstamp = sf.SIMPLEFLAKE_EPOCH + 123123
     gen = random.SystemRandom()
     random_bits = gen.getrandbits(sf.SIMPLEFLAKE_RANDOM_LENGTH)
     flake = sf.simpleflake(timestamp=coolstamp, random_bits=random_bits)
     parts = sf.parse_simpleflake(flake)
     self.assertEquals(coolstamp, parts.timestamp)
     self.assertEquals(random_bits, parts.random_bits)
Example #13
0
 def test_parse(self):
     coolstamp = SIMPLEFLAKE_EPOCH + 123123
     random_bits = random.SystemRandom()\
         .getrandbits(SIMPLEFLAKE_RANDOM_LENGTH)
     flake = simpleflake(timestamp=coolstamp, random_bits=random_bits)
     parts = parse_simpleflake(flake)
     self.assertEquals(coolstamp, parts.timestamp)
     self.assertEquals(random_bits, parts.random_bits)
Example #14
0
def create_app():
  app_id = simpleflake()
  # Store app
  db.hmset(key("apps", app_id), dict(
    id=app_id,
    email=request.form["email"].strip().lower(),
    shortcode=int(request.form["shortcode"].strip()),
  ))
  return app_id
Example #15
0
def activation(signedstring):
    try:
        id = int(unsign(signedstring, app.config))
        the_user = user.User.get(user.User.id == id)
        the_user.status = 1
        the_user.auth_token = str(simpleflake())
        the_user.save()
        session['user_id'] = id
        return redirect(url_for('index'))
    except (BadSignature, DoesNotExist):
        raise BadRequestError()
Example #16
0
    def post(self):
        user = self.get_current_user()

        event = dict()
        event_fields = [
            'title', 'start_date', 'end_date', 'start_time', 'end_time',
            'description', 'website'
        ]

        venue = dict()
        venue_fields = [
            'foursquare_id', 'name', 'address', 'locality', 'region',
            'postal_code', 'longitude', 'latitude'
        ]

        for key in event_fields:
            event[key] = self.get_argument(key, None)

        for key in venue_fields:
            venue[key] = self.get_argument(key, None)

        event_uuid = simpleflake()
        event['event_id'] = base62().hash(event_uuid, 12)

        venue_uuid = simpleflake()
        venue['venue_id'] = base62().hash(venue_uuid, 12)
        event['venue_id'] = venue['venue_id']

        event['created_at'] = r.now()
        event['updated_at'] = r.now()
        event['creator_user_id'] = user['id']

        venue['geo'] = r.point(float(venue['longitude']),
                               float(venue['latitude']))
        venue['created_at'] = r.now()
        venue['updated_at'] = r.now()
        venue['creator_user_id'] = user['id']

        r.table("event").insert(event).run()
        r.table("venue").insert(venue).run()
        self.redirect("/")
Example #17
0
def add_sound(lat, lng, basename, title, container, user, flags):
    timestamp = int(time())
    return Sound.create(id=simpleflake(),
                        lat=check_float(lat),
                        lng=check_float(lng),
                        basename=check_notempty(basename),
                        title=title,
                        container=check_notempty(container),
                        user=user,
                        flags=flags,
                        created=timestamp,
                        modified=timestamp)
Example #18
0
  def post(self):
    user = self.get_current_user()

    event = dict()
    event_fields = [
      'title', 'start_date', 'end_date', 'start_time', 'end_time', 'description', 
      'website'
      ]

    venue = dict()
    venue_fields = [
      'foursquare_id', 'name', 'address', 'locality', 'region', 'postal_code',
      'longitude', 'latitude'
      ]

    for key in event_fields:
      event[key] = self.get_argument(key, None)

    for key in venue_fields:
      venue[key] = self.get_argument(key, None)

    event_uuid = simpleflake()
    event['event_id'] = base62().hash(event_uuid, 12)

    venue_uuid = simpleflake()
    venue['venue_id'] = base62().hash(venue_uuid, 12)    
    event['venue_id'] = venue['venue_id']

    event['created_at'] = r.now()
    event['updated_at'] = r.now()
    event['creator_user_id'] = user['id']

    venue['geo'] = r.point(float(venue['longitude']), float(venue['latitude']))
    venue['created_at'] = r.now()
    venue['updated_at'] = r.now()
    venue['creator_user_id'] = user['id']

    r.table("event").insert(event).run()
    r.table("venue").insert(venue).run()
    self.redirect("/")
Example #19
0
def send_sms(mobile_number, message):
  requests.post(
    CHIKKA_API_URL,
    data=dict(
      message_type="SEND",
      mobile_number=mobile_number,
      shortcode=app.config["CHIKKA_SHORTCODE"],
      message_id=str(simpleflake()),
      message=message + '\n\n*',
      client_id=app.config["CHIKKA_CLIENT_ID"],
      secret_key=app.config["CHIKKA_SECRET_KEY"],
    )
  )
Example #20
0
def send_sms(mobile_number, message):
    requests.post(
        CHIKKA_API_URL,
        data=dict(
            message_type="SEND",
            mobile_number=mobile_number,
            shortcode=SHORTCODE,
            message_id=str(simpleflake()),
            message=message,
            client_id=CLIENT_ID,
            secret_key=SECRET_KEY,
        )
    )
Example #21
0
def links(link_id):
    item = Links.query.get(link_id)

    current_visit = Visits(visit_id=simpleflake(),
                           visit_ip=request.headers.get('CF-Connecting-IP'))

    try:
        db.session.add(current_visit)
        db.session.commit()
        return redirect(f"http://{item.link_url}")
    except:
        traceback.print_exc()
        return "ip append ooops.."
Example #22
0
def create_survey(app_id):
  survey_id = simpleflake()
  question = request.form["question"].strip()
  choices = map(lambda s: s.strip().upper(), request.form["choices"].strip().split("\n"))
  # Store survey
  db.hmset(key("surveys", app_id, survey_id), dict(
    id=survey_id,
    question=question,
  )),
  # Add choices
  db.sadd(key("choices", app_id, survey_id), *choices)
  # Set current survey
  db.set(key("curr_survey", app_id), survey_id)
  return survey_id
Example #23
0
def upload():
  merchant = logged_in()
  error = False

  desc = request.form.get('desc')
  if not desc:
    flash("You need to write a description", 'error')
    error = True

  amount = request.form.get('amount')
  if not amount:
    flash("You need to specify item's price", 'error')
    error = True

  file = request.files['file']
  if not (file and allowed_file(file.filename)):
    flash("Did you forget about your file?", 'error')
    error = True

  if not error:
    filename = secure_filename(file.filename)
    filepath=os.path.join(app.config['UPLOAD_FOLDER'], filename)
    file.save(filepath)

    item_id = simpleflake()
    shortcode = merchant['globe_shortcode']
    # Save as upload of user
    db.sadd(
      '{}:items'.format(shortcode),
      item_id,
    )

    # Save upload details
    db.hmset(
      '{}:items:{}'.format(
        shortcode,
        item_id,
      ),
      {
        'filename': filename,
        'filepath': filepath,
        'desc': desc,
        'amount': amount,
      },
    )

    flash('Item added!', 'success')

  return redirect(url_for('dashboard'))
Example #24
0
def upload():
    merchant = logged_in()
    error = False

    desc = request.form.get('desc')
    if not desc:
        flash("You need to write a description", 'error')
        error = True

    amount = request.form.get('amount')
    if not amount:
        flash("You need to specify item's price", 'error')
        error = True

    file = request.files['file']
    if not (file and allowed_file(file.filename)):
        flash("Did you forget about your file?", 'error')
        error = True

    if not error:
        filename = secure_filename(file.filename)
        filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        file.save(filepath)

        item_id = simpleflake()
        shortcode = merchant['globe_shortcode']
        # Save as upload of user
        db.sadd(
            '{}:items'.format(shortcode),
            item_id,
        )

        # Save upload details
        db.hmset(
            '{}:items:{}'.format(
                shortcode,
                item_id,
            ),
            {
                'filename': filename,
                'filepath': filepath,
                'desc': desc,
                'amount': amount,
            },
        )

        flash('Item added!', 'success')

    return redirect(url_for('dashboard'))
Example #25
0
    def gen_key(cls, code=None):
        '''
        生成随机带字符串的 code
        '''

        if code:
            return str(code), ''

        offset = 4
        confuse = str(simpleflake())
        index = randint(0, offset)
        last = index + offset

        code = hashlib.sha224(confuse).hexdigest()[index:last]
        return code, confuse
Example #26
0
class Base:
    """
    Base class for models to inherit from.
    Provides default `id` column and `created_at` column, and utility class methods:
        `exists`: check if a row exists with given id
        `get_any`: gets all rows matching at least one of the given arguments, being optionally case insensitive.
    """
    id = db.Column(db.Unicode(),
                   primary_key=True,
                   default=lambda: str(simpleflake()))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)

    @classmethod
    async def exists(cls: db.Model, id_: str) -> bool:
        """Check if a model exists with the given id."""
        return bool(await cls.select("id").where(cls.id == id_).gino.scalar())

    @classmethod
    def get_any(cls: db.Model,
                insensitive: Union[bool, List[str]] = False,
                **kwargs) -> Gino:
        """Get models that match any of the given kwargs, with them optionally being case insensitive."""
        if not kwargs:
            raise ValueError("No kwargs provided")

        queries = []

        if isinstance(insensitive, list):
            for k, v in kwargs:
                if k in insensitive:
                    queries.push(func.lower(getattr(cls, k)) == func.lower(v))
                else:
                    queries.push(getattr(cls, k) == v)
        elif insensitive is True:
            queries = [
                func.lower(getattr(cls, k)) == func.lower(v)
                for k, v in kwargs.items()
            ]
        else:
            queries = [getattr(cls, k) == v for k, v in kwargs.items()]

        return cls.query.where(or_(*queries)).gino
Example #27
0
    def invoke(self):
        """
        Run the file count application
        """

        # Get inputs
        directory = self.get_input_data_port(self.__directory_port_name)
        recursive = self.get_input_string_port(self.__recursive_port_name, 'n')
        recursive = str(recursive).lower() in {'y', 'yes', 'true'}

        output_dir = self.get_output_data_port(self.__output_data_port_name)

        # Build and the command
        args = '{_dir}{_r}'.format(_dir=directory,
                                   _r='' if not recursive else ' -r')
        cmd = 'python /src/file_count/file_count.py' + ' ' + args
        print('Executing {_cmd}'.format(_cmd=cmd))
        os.system(cmd)

        if os.path.exists(self.__algo_output_file):
            with open(self.__algo_output_file, 'r') as f:
                self.__algo_output = json.load(f)
        else:
            print("Expected output missing.")
            return False

        # Copy output
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        shutil.copy(
            'file_count.json',
            os.path.join(output_dir,
                         'file_count_' + str(simpleflake()) + '.json'))

        # Create output port
        self.set_output_string_port(self.__output_string_port_name,
                                    self.__algo_output['file_count'])

        print("Invoke complete")

        return True
Example #28
0
def submit_comment():
    '''
    Hit by a user's browser to add a new comment
    '''
    # slug should be something like a url
    slug = request.form.get('slug')
    comment_text = request.form.get('comment')
    name = request.form.get('name')
    email = request.form.get('email')

    if not comment_text or not name or not slug:
        return jsonify({
            'error': '`comment`, `name` and `slug` needed',
        }), 400

    comment_id = str(simpleflake.simpleflake())
    comment = {
        'id': comment_id,
        'text': comment_text,
        'name': name,
        'email': email,
        'version': '0',
        'ip_address': request.remote_addr,
        'created_at': datetime.utcnow().replace(tzinfo=pytz.utc).isoformat(),
    }
    comment_json = json.dumps(comment)

    # store comment in appropriate place in bucket
    file_name = 'comments/%s/%s.json' % (slug, comment_id)
    bucket.put_object(
        Body=bytes(comment_json, encoding='utf8'),
        Key=file_name,
        ACL='private',
        ContentType='application/json',
    )

    return Response(comment_json, headers={
        'Access-Control-Allow-Origin': '*',
    }, content_type='application/json')
Example #29
0
def add_user(username, fullname, password, email):
    if not re.match(r'^[a-z\d\-]+$', username, flags=re.I):
        raise BadRequestError('Bad username')
    if not re.match(r'^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$',
                    email,
                    flags=re.I):
        raise BadRequestError('Bad email address')
    if not len(password) > 7:
        raise BadRequestError('Password is too short')
    if not re.match(r'[a-z]', fullname, re.I):
        raise BadRequestError('Full name appears not to be complete')
    timestamp = int(time())
    try:
        return User.create(id=simpleflake.simpleflake(),
                           username=username.strip(),
                           fullname=fullname.strip(),
                           password=hashlib.sha256(password).hexdigest(),
                           email=email,
                           status=0,
                           created=timestamp,
                           modified=timestamp)
    except IntegrityError:
        raise ConflictError('Account already exists')
Example #30
0
  def post(self, client_id, user_id):
    args = self.parser.parse_args()

    if args.secret_key != db.hget("apps:" + client_id, "secret_key"):
      abort(401)

    app_name = db.hget("apps:" + client_id, "name")
    user = db.hgetall(
      "apps:{}:users:{}".format(client_id, user_id),
    )

    auth = OtpAuth(args.secret_key)
    code = auth.totp()

    res = requests.post(
      CHIKKA_SMS_ENDPOINT,
      data=dict(
        message_type="SEND",  # Inconsistent
        mobile_number=user["number"],
        shortcode=current_app.config["CHIKKA_SHORTCODE"],
        message_id=simpleflake(),
        message="""{}

Code: {}

-
""".format(app_name, code),
        request_cost="FREE",
        client_id=current_app.config["CHIKKA_CLIENT_ID"],
        secret_key=current_app.config["CHIKKA_SECRET_KEY"],
      ),
    )

    if res.status_code != requests.codes.ok:
      abort(500)

    return ""
Example #31
0
    def invoke(self):
        """
        Run the file count application
        """

        # Get inputs
        directory = self.get_input_data_port(self.__directory_port_name)
        recursive = self.get_input_string_port(self.__recursive_port_name, 'n')
        recursive = str(recursive).lower() in {'y', 'yes', 'true'}

        output_dir = self.get_output_data_port(self.__output_data_port_name)

        # Build and the command
        args = '{_dir}{_r}'.format(_dir=directory, _r='' if not recursive else ' -r')
        cmd = 'python /src/file_count/file_count.py' + ' ' + args
        print('Executing {_cmd}'.format(_cmd=cmd))
        os.system(cmd)

        if os.path.exists(self.__algo_output_file):
            with open(self.__algo_output_file, 'r') as f:
                self.__algo_output = json.load(f)
        else:
            print("Expected output missing.")
            return False

        # Copy output
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        shutil.copy('file_count.json', os.path.join(output_dir, 'file_count_' + str(simpleflake()) + '.json'))

        # Create output port
        self.set_output_string_port(self.__output_string_port_name, self.__algo_output['file_count'])

        print("Invoke complete")

        return True
Example #32
0
def add_session():
    """
    Create a "session" by taking the email and password, and returning
    an auth token and setting the user ID in the session cookie.
    """
    data = form_or_json()
    try:
        the_user = user.User.get(user.User.email == data['email'])
        if the_user.password == hashlib.sha256(data['password']).hexdigest():
            session['user_id'] = the_user.id
            if the_user.auth_token:
                auth_token = the_user.auth_token
            else:
                auth_token = str(simpleflake())
                the_user.auth_token = auth_token
                the_user.save()
            return jsonify(auth_token=sign(auth_token, app.config),
                           user_id=str(the_user.id))
        else:
            raise UnauthorizedError()
    except KeyError:
        raise BadRequestError()
    except DoesNotExist:
        raise UnauthorizedError()
Example #33
0
 def _new_id(self):
     """The identifier for each upload will be a random number
     generated by the simpleflake library.
     More information at: http://tinyurl.com/mqbjbpm
     """
     return str(simpleflake())
Example #34
0
def checkpoint(thread_local=__LOCALS):
    """
    Reset the checkpoint
    """
    thread_local.checkpoint_id = simpleflake.simpleflake()
Example #35
0
def charge():
  """
  Charge URI is the endpoint for charging and sending of sms
  This endpoint expects the ff params:
  - shortcode
  - subscriber_number
  - amount
  """

  shortcode = request.json['shortcode']
  merchant = db.hgetall(shortcode)
  if not merchant:
    abort(404)

  subscriber_number = request.json['subscriber_number']
  item_id = request.json.get('item_id', 'demo')
  amount = request.json['amount']

  # Get the access token from DB
  access_token = db.hget(
    '{}:{}'.format(shortcode, subscriber_number),
    'access_token',
  )
  if not access_token:
    return jsonify(
      needs_authorization=True,
      dialog_url=const.G_DIALOG.format(merchant['globe_app_id']),
    )

  sender = shortcode[-4:] # this is weird!
  confirm_code = str(simpleflake())[-6:] # Generate a random code for this user session

  params = {'access_token': access_token}
  req = {
    'outboundSMSMessageRequest': {
      'clientCorrelator': str(simpleflake()),
      'senderAddress': 'tel:{}'.format(sender),
      'outboundSMSTextMessage': {
        'message': '{}\n\nYou will be charged PHP {}. This is your code to proceed: {}'.format(
          merchant['store_name'],
          amount,
          confirm_code,
        ),
      },
      'address': ['tel:+63{}'.format(subscriber_number)],
    }
  }

  res = requests.post(
    const.G_SMS_ENDPOINT.format(sender),
    headers={
      'Content-Type': 'application/json',
    },
    params=params,
    data=json.dumps(req)
  )

  app.logger.debug(res.content)

  if not res.ok:
    abort(500)

  payload = res.json()
  payload.update(confirm_code_sent=True)

  # Save confirm code assoc
  # Expires after 15 minutes
  confirm_key = '{}:{}:confirm:{}'.format(
    shortcode,
    subscriber_number,
    confirm_code,
  )
  db.hmset(confirm_key, {
    'item_id': item_id,
    'amount': amount,
  })
  db.expire(confirm_key, 15 * 60)

  return jsonify(**payload)
Example #36
0
 def test_simpleflake_size(self):
     flake = sf.simpleflake()
     self.assertEquals(64, len(sf.binary(flake)))
Example #37
0
def store(result, storage=None):
    storage = storage or get_storage(name=result.experiment.name)
    key = simpleflake()
    storage.set(key, result)
    return key
Example #38
0
def checkpoint(thread_local=__LOCALS):
    """
    Reset the checkpoint
    """
    thread_local.checkpoint_id = simpleflake.simpleflake()
Example #39
0
 def __init__(self) -> None:
     self.id = simpleflake()
     self.components = {}
     self.eindex[self.id] = self
Example #40
0
db_client = MongoClient('localhost', 27017)
# print(db_client.server_info()) get connection Details

# print("Mongodb Connection Successful")

db = db_client['videoAPI']
user_info = db['userdata']
user_login_info = db['login_info']
video_data = db['video_db']

app = Flask(__name__)
app.debug = True
# Enable debug for Dev Environment

# assign user verification unique id using simpleflake
user_verification_id = simpleflake()
reg_time = datetime.now()

# Enable to share data between java script and python flask .so we use flask CORS

CORS(app)


@app.route('/videoapi/register', methods=['GET', 'POST'])
def user_register():
    # check if it is a GET method
    if request.method == 'GET':
        request_data = request.args
    elif request.method == 'POST':
        request_data = request.form
    else:
Example #41
0
def flake_id():
    """Generate a new random Flake ID"""
    return simpleflake()
Example #42
0
    def create_table(self, data):
        table_id = simpleflake()
        print "".join(["Creating new table, id=", str(table_id)])

        return self.update_table(table_id, data)
Example #43
0
    def put(cls, models):
        if not models:
            return []

        update_models = []
        insert_models = []
        for model in models:
            if '_key' in model.__dict__ and model._key:
                update_models.append(model)
            else:
                insert_models.append(model)

        columns = ['key_id', 'namespace']
        columns.extend(sorted(cls._properties.keys()))

        if insert_models:
            row_values = ['%s' for _ in range(len(columns))]
            row_values = "({})".format(', '.join(row_values))
            values = [row_values for _ in range(len(insert_models))]

            args = []
            for model in models:
                model._key = db.Key.from_path(cls.kind(),
                                              simpleflake(),
                                              namespace='benchmark_test')
                row_args = [getattr(model, column) for column in columns]
                args.extend(row_args)
            args = tuple(args)
            sql = "INSERT INTO {table_name} ({columns}) VALUES {values}".format(
                table_name=cls.__name__,
                columns=', '.join(columns),
                values=', '.join(values),
            )
            with cursor() as db_cursor:
                try:
                    db_cursor.execute("BEGIN")
                    db_cursor.execute(sql, args)
                    db_cursor.execute("COMMIT")
                except Exception as e:
                    _logger.error("Failed to execute SQL: {}".format(sql))
                    _logger.exception(e.message)
                    db_cursor.execute("ROLLBACK")
                    raise NotImplementedError("{} / {}".format(sql, args))

        if update_models:
            for model in update_models:
                assignments = [
                    '{column} = %s'.format(column=column) for column in columns
                ]
                assignments = ', '.join(assignments)

                args = [getattr(model, column) for column in columns]
                args.append(model._key.id())
                args.append(model._key.namespace())
                args = tuple(args)
                sql = "UPDATE {table_name} SET {assignments} WHERE key_id = %s AND namespace = %s".format(
                    table_name=cls.__name__,
                    assignments=assignments,
                )
            with cursor() as db_cursor:
                try:
                    db_cursor.execute("BEGIN")
                    db_cursor.execute(sql, args)
                    db_cursor.execute("COMMIT")
                except Exception as e:
                    _logger.error("Failed to execute SQL: {}".format(sql))
                    _logger.exception(e.message)
                    db_cursor.execute("ROLLBACK")
                    raise NotImplementedError("{} / {}".format(sql, args))
Example #44
0
def store(result, storage=None):
    storage = storage or get_storage(name=result.experiment.name)
    key = simpleflake()
    storage.set(key, result)
    return key
Example #45
0
 def _new_id(self):
     '''The identifier for each upload will be a random number
     generated by the simpleflake library.
     More information at: http://tinyurl.com/mqbjbpm
     '''
     return str(simpleflake())
Example #46
0
from sqlalchemy import create_engine, Table, Column, Integer, Text, String, DateTime, MetaData
from datetime import datetime
from simpleflake import simpleflake

engine = create_engine('sqlite:///local.db', echo = True)
meta = MetaData() 

user = Table(
    'users',
    meta,
    Column('id', Integer, primary_key = True),
    Column('username', String(16), nullable = False),
    Column('password', String(512), nullable = False),
    Column('email', String(64), nullable = False),
    Column('reg_time', DateTime, default = datetime.utcnow)
)

message = Table(
    'messages',
    meta,
    Column('id', Integer, primary_key = True, default = simpleflake()),
    Column('username', String(16), nullable = False),
    Column('message', Text, nullable = False),
    Column('sent_at', DateTime, default = datetime.utcnow)
)

meta.create_all(engine)
Example #47
0
def get_unique_id(**kwargs):
    kwargs.setdefault('epoch', settings.JANGL_EPOCH)
    return simpleflake(**kwargs)
Example #48
0
def charge():
    """
  Charge URI is the endpoint for charging and sending of sms
  This endpoint expects the ff params:
  - shortcode
  - subscriber_number
  - amount
  """

    shortcode = request.json['shortcode']
    merchant = db.hgetall(shortcode)
    if not merchant:
        abort(404)

    subscriber_number = request.json['subscriber_number']
    item_id = request.json.get('item_id', 'demo')
    amount = request.json['amount']

    # Get the access token from DB
    access_token = db.hget(
        '{}:{}'.format(shortcode, subscriber_number),
        'access_token',
    )
    if not access_token:
        return jsonify(
            needs_authorization=True,
            dialog_url=const.G_DIALOG.format(merchant['globe_app_id']),
        )

    sender = shortcode[-4:]  # this is weird!
    confirm_code = str(
        simpleflake())[-6:]  # Generate a random code for this user session

    params = {'access_token': access_token}
    req = {
        'outboundSMSMessageRequest': {
            'clientCorrelator': str(simpleflake()),
            'senderAddress': 'tel:{}'.format(sender),
            'outboundSMSTextMessage': {
                'message':
                '{}\n\nYou will be charged PHP {}. This is your code to proceed: {}'
                .format(
                    merchant['store_name'],
                    amount,
                    confirm_code,
                ),
            },
            'address': ['tel:+63{}'.format(subscriber_number)],
        }
    }

    res = requests.post(const.G_SMS_ENDPOINT.format(sender),
                        headers={
                            'Content-Type': 'application/json',
                        },
                        params=params,
                        data=json.dumps(req))

    app.logger.debug(res.content)

    if not res.ok:
        abort(500)

    payload = res.json()
    payload.update(confirm_code_sent=True)

    # Save confirm code assoc
    # Expires after 15 minutes
    confirm_key = '{}:{}:confirm:{}'.format(
        shortcode,
        subscriber_number,
        confirm_code,
    )
    db.hmset(confirm_key, {
        'item_id': item_id,
        'amount': amount,
    })
    db.expire(confirm_key, 15 * 60)

    return jsonify(**payload)
Example #49
0
import random

def my_hash(bits=96):
    assert bits % 8 == 0
    required_length = bits / 8 * 2
    s = hex(random.getrandbits(bits)).lstrip('0x').rstrip('L')
    if len(s) < required_length:
        return my_hash(bits)
    else:
        return s

# print my_hash()
# 9c14dac2a6f1d65929095295    # just a sample; it's a string

## or: ##################

# You can also use simpleflake. By default it generates 64 bit
# unique IDs. The generated ID consists of 2 parts: a) timestamp,
# b) random number. It has the advantage that the generated IDs
# arrive in ascending order.
# see http://engineering.custommade.com/simpleflake-distributed-id-generation-for-the-lazy/

$ pip install simpleflake
>>> import simpleflake
>>> simpleflake.simpleflake()
3620361890155888216L    # just a sample
>>> hex(simpleflake.simpleflake()).lstrip('0x').rstrip('L')
'349b3dc4a2976240'      # just a sample
Example #50
0
def getid():
    from simpleflake import simpleflake
    return simpleflake()
Example #51
0
 def __init__(self, name: str = "") -> None:
     self.id = simpleflake()
     self.name = name
Example #52
0
 def test_simpleflake_size(self):
     flake = sf.simpleflake()
     self.assertEquals(64, len(sf.binary(flake)))
Example #53
0
    def __init__(self, group=None, target=None, name=None,
                 args=(), kwargs=None, verbose=None):
        threading.Thread.__init__(self, group=group, target=target, name=name,
                                  verbose=verbose)
        self.args = args
        self.kwargs = kwargs
        self.worker = up.DropboxUploader(self.args)
        self.worker.client = dbClient
        self.state = -1 #It can be 0 for stop when the next chunk is uploaded
                        #or 1 for delete the upload.

    def run(self):
        logging.debug('Starting Upload of the file:{}'.format(self.args))
        for i in self.worker.upload_chunked():
            logging.debug(i)
            if self.state == 0:
                return #send signal to UI
            elif self.state == 1:
                return #delete the thread, dont update the ui even if 
                       #a chunk is uploaded in the meantime.
                
        self.worker.finish('/new.pdf') #change path
        logging.debug('Upload completed.') #write to history with global lock
        #raise event to inform user
        
        return

paths = [r"C:\Users\Fadi\Desktop\03.Colloquial Swedish 2007.pdf"]

t = MyThreadWithArgs(name=str(simpleflake()), args=paths[0])
t.start()
Example #54
0
def get_uuid():
    return str(simpleflake())
Example #55
0
                                  target=target,
                                  name=name,
                                  verbose=verbose)
        self.args = args
        self.kwargs = kwargs
        self.worker = up.DropboxUploader(self.args)
        self.worker.client = dbClient
        self.state = -1  #It can be 0 for stop when the next chunk is uploaded
        #or 1 for delete the upload.

    def run(self):
        logging.debug('Starting Upload of the file:{}'.format(self.args))
        for i in self.worker.upload_chunked():
            logging.debug(i)
            if self.state == 0:
                return  #send signal to UI
            elif self.state == 1:
                return  #delete the thread, dont update the ui even if
                #a chunk is uploaded in the meantime.

        self.worker.finish('/new.pdf')  #change path
        logging.debug('Upload completed.')  #write to history with global lock
        #raise event to inform user

        return


paths = [r"C:\Users\Fadi\Desktop\03.Colloquial Swedish 2007.pdf"]

t = MyThreadWithArgs(name=str(simpleflake()), args=paths[0])
t.start()
Example #56
0
import random

def my_hash(bits=96):
    assert bits % 8 == 0
    required_length = bits / 8 * 2
    s = hex(random.getrandbits(bits)).lstrip('0x').rstrip('L')
    if len(s) < required_length:
        return my_hash(bits)
    else:
        return s

# print my_hash()
# 9c14dac2a6f1d65929095295    # just a sample; it's a string

## or: ##################

# You can also use simpleflake. By default it generates 64 bit
# unique IDs. The generated ID consists of 2 parts: a) timestamp,
# b) random number. It has the advantage that the generated IDs
# arrive in ascending order.
# see http://engineering.custommade.com/simpleflake-distributed-id-generation-for-the-lazy/

$ pip install simpleflake
>>> import simpleflake
>>> simpleflake.simpleflake()
3620361890155888216L    # just a sample