<commit_before>#!/usr/bin/env python
#
# Load favorites for a Twitter user and output them to a file.
#

import csv
import os
import time
import twitter

from time import sleep
from twitter.error import TwitterError
from sqlalchemy import desc
from sqlalchemy.orm.exc import NoResultFound

from myarchive.db.tables.twittertables import (
    CSVTweet, RawTweet, Tweet, TwitterUser)

from account_info import *

SLEEP_TIME = 60 * (60 / 15)

USER = "USER"
FAVORITES = "FAVORITES"

KEYS = [
    u'user',
    u'text',
    u'in_reply_to_screen_name',
    u'media',

    # Not that interesting, but saved.
    u'hashtags',
    u'id',
    u'in_reply_to_status_id',
    # Seems to be empty a lot. Put it at the end.
    u'urls',

    # Don't really care about these.
    # u'user_mentions',
    # u'source',
    # u'created_at',
    # u'id_str',
    # u'place',
    # u'in_reply_to_user_id',
    # u'lang',
    # u'possibly_sensitive',
    # u'favorited',
    # u'favorite_count',
    # u'retweeted',
    # u'retweet_count',
]

class BulkApi(twitter.Api):
    """API with an extra call."""

    def LookupStatuses(self, status_ids, trim_user=False,
                       include_entities=True):
        """
        Returns up to 100 status messages, specified by a list passed to
        status_ids.

        Args:
          status_ids:
            A list of numeric IDs of the statuses you are trying to retrieve.
          trim_user:
            When set to True, each tweet returned in a timeline will include
            a user object including only the status authors numerical ID.
            Omit this parameter to receive the complete user object. [Optional]
          include_entities:
            If False, the entities node will be disincluded.
            This node offers a variety of metadata about the tweet in a
            discreet structure, including: user_mentions, urls, and
            hashtags. [Optional]
        Returns:
          A twitter.Status instance representing that status message
        """
        url = '%s/statuses/lookup.json' % self.base_url

        parameters = dict()

        if not status_ids or len(status_ids) > 100:
            raise TwitterError(
                "status_ids must be between 1 and 100 in length.")
        # This takes a comma-separated list of up to 100 IDs.
        parameters['id'] = ",".join(status_ids)

        if trim_user:
            parameters['trim_user'] = True
        if include_entities:
            parameters['include_entities'] = True

        resp = self._RequestUrl(url, 'GET', data=parameters)
        data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

        return [twitter.Status.NewFromJsonDict(x) for x in data]

def archive_tweets(username, db_session, types=(USER, FAVORITES)):
    """
    Archives several types of new tweets along with their associated content.
    """
    new_tweets = []
    api = twitter.Api(
        CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET,
        sleep_on_rate_limit=True)

    for type_ in types:

        # For favorites, always do a full sweep. We can't guarantee an older
        # tweet wasn't recently favorited!
        if type_ == FAVORITES:
            since_id = None
        else:
            since_id = db_session.query(RawTweet.id).\
                filter(RawTweet.favorited_by_str.like("%%%s%%" % username)).\
                order_by(desc(RawTweet.id)).first()
        if since_id:
            since_id = since_id[0]
        print type_, since_id

        start_time = -1
        sleep_time = 0
        max_id = None
        early_termination = False
        request_index = 0
        requests_before_sleeps = 1
        statuses = []
        while not early_termination:
            # Twitter rate-limits us. Space this out a bit to avoid a
            # super-long sleep at the end doesn't kill the connection.
            if request_index >= requests_before_sleeps:
                duration = time.time() - start_time
                if duration < sleep_time:
                    sleep_duration = sleep_time - duration
                    print ("Sleeping for %s seconds to ease up on rate "
                           "limit..." % sleep_duration)
                    sleep(sleep_duration)
            start_time = time.time()
            request_index += 1

            print ("Pulling 200 tweets from API starting with ID %s and "
                   "ending with ID %s..." % (since_id, max_id))
            try:
                if type_ == FAVORITES:
                    loop_statuses = api.GetFavorites(
                        screen_name=username,
                        count=200,
                        since_id=since_id,
                        max_id=max_id,
                        include_entities=True)
                    # 15 requests per 15 minutes.
                    requests_before_sleeps = 15 - 1
                    sleep_time = 60
                elif type_ == USER:
                    loop_statuses = api.GetUserTimeline(
                        screen_name=username,
                        count=200,
                        since_id=since_id,
                        max_id=max_id)
                    # 300 requests per 15 minutes.
                    sleep_time = 3
                    requests_before_sleeps = 300 - 1
            except twitter.error.TwitterError as e:
                # If we overran the rate limit, try again.
                if e.message[0][u'code'] == 88:
                    print (
                        "Overran rate limit. Sleeping %s seconds in an attempt "
                        "to recover..." % sleep_time)
                    request_index = requests_before_sleeps
                    sleep(sleep_time)
                    continue
            print "Found %s tweets this iteration..." % len(loop_statuses)
            # Check for "We ran out of tweets via this API" termination
            # condition.
            if not loop_statuses:
                break
            # Check for early termination condition.
            for loop_status in loop_statuses:
                status_id = int(loop_status.AsDict()["id"])
                if since_id is not None and status_id >= since_id:
                    early_termination = True
                    break
                # Only append if we don't breach since_id.
                statuses.append(loop_status)
                # Capture new max_id
                if status_id < max_id or max_id is None:
                    max_id = status_id - 1

        # Format things the way we want and handle max_id changes.
        print "Adding %s tweets to DB..." % len(statuses)
        existing_rawtweet_ids = [
            returned_tuple[0]
            for returned_tuple in db_session.query(RawTweet.id).all()]
        for status in statuses:
            status_dict = status.AsDict()
            status_id = int(status_dict["id"])
            if status_id in existing_rawtweet_ids:
                continue
            raw_tweet = RawTweet(status_dict=status_dict)
            new_tweets.append(raw_tweet)
            db_session.add(raw_tweet)
            if type_ == FAVORITES:
                raw_tweet.add_user_favorite(username)
        db_session.commit()

    return new_tweets

def import_from_csv(db_session, csv_filepath, username):
    api = BulkApi(
        CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET,
        sleep_on_rate_limit=True)

    print "Importing into CSVTweets..."
    csv_tweets_by_id = dict(
        (csv_tweet[0], csv_tweet)
        for csv_tweet in db_session.query(CSVTweet).all())
    with open(csv_filepath) as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            tweet_id = int(row['tweet_id'])
            if tweet_id not in csv_tweets_by_id:
                csv_tweet = CSVTweet(
                    id=tweet_id,
                    username=username,
                    in_reply_to_status_id=row["in_reply_to_status_id"],
                    in_reply_to_user_id=row["in_reply_to_user_id"],
                    timestamp=row["timestamp"],
                    text=row["text"],
                    retweeted_status_id=row["retweeted_status_id"],
                    retweeted_status_user_id=row["retweeted_status_user_id"],
                    retweeted_status_timestamp=row["retweeted_status_timestamp"],
                    expanded_urls=row["expanded_urls"])
                db_session.add(csv_tweet)
                csv_tweets_by_id[tweet_id] = csv_tweet
    db_session.commit()

    print "Scanning for existing RawTweets..."
    existing_rawtweet_ids = [
        returned_tuple[0]
        for returned_tuple in db_session.query(RawTweet.id).all()]
    ids_to_remove = []
    for tweet_id, csv_tweet in csv_tweets_by_id.items():
        # If any aren't set as imported and should be, clean that up.
        if tweet_id in existing_rawtweet_ids:
            ids_to_remove.append(tweet_id)
            if not csv_tweet.api_import_complete:
                csv_tweet.api_import_complete = True
    db_session.commit()
    for id_to_remove in ids_to_remove:
        csv_tweets_by_id.pop(id_to_remove)

    csv_ids = list(csv_tweets_by_id.keys())
    num_imports = len(csv_ids)
    print "Attempting API import of %s tweets based on CSV file..." % (
        num_imports)

    # API allows 60 requests per 15 minutes.
    sleep_time = 15
    requests_before_sleeps = 60 - 1

    # Set loop starting values
    request_index = 0
    start_time = -1
    new_api_tweets = []
    sliced_ids = csv_ids[:100]
    while sliced_ids:

        # Sleep to not hit the rate limit.
        # Twitter rate-limits us. Space this out a bit to avoid a
        # super-long sleep at the end doesn't kill the connection.
        if request_index >= requests_before_sleeps:
            duration = time.time() - start_time
            if duration < sleep_time:
                sleep_duration = sleep_time - duration
                print ("Sleeping for %s seconds to ease up on rate "
                       "limit..." % sleep_duration)
                sleep(sleep_duration)
        start_time = time.time()

        # Perform the import.
        print "Attempting import of id %s to %s of %s..." % (
            request_index + 1, min(request_index + 100, num_imports), num_imports)
        try:
            statuses = api.LookupStatuses(
                status_ids=[str(sliced_id) for sliced_id in sliced_ids],
                trim_user=False,
                include_entities=True)
            for status in statuses:
                status_dict = status.AsDict()
                # Create the RawTweet
                raw_tweet = RawTweet(status_dict=status_dict)
                db_session.add(raw_tweet)
                # Mark CSVTweet appropriately.
                csv_tweet = csv_tweets_by_id[int(status_dict["id"])]
                csv_tweet.api_import_complete = True
                # Append to new list.
                new_api_tweets.append(raw_tweet)
            db_session.commit()

        except TwitterError as e:
            print e
        request_index += 100
        sliced_ids = csv_ids[request_index:100 + request_index]

    csv_only_tweets = db_session.query(CSVTweet.api_import_complete).\
        filter_by(api_import_complete=False).all()

    return new_api_tweets, csv_only_tweets

def parse_tweets(db_session, raw_tweets=None, csv_only_tweets=None,
                 parse_all_raw=False):
    user = None

    if parse_all_raw is True:
        # Process all captured raw tweets.
        raw_tweets = db_session.query(RawTweet)

    # Filter out existing tweets, making sure to compare with a a tuple since
    # SQLAlchemy will return a list of tuples.
    existing_tweet_ids = db_session.query(Tweet.id).all()
    raw_tweets_to_parse = [
        raw_tweet for raw_tweet in raw_tweets
        if (int(raw_tweet.raw_status_dict["id"]),) not in existing_tweet_ids]
    print "Found %s tweets to parse." % len(raw_tweets_to_parse)

    for index, raw_tweet in enumerate(raw_tweets_to_parse):
        if index % 100 == 0:
            print "Parsing tweet %s to %s of %s..." % (
                index,
                min(index + 100, len(raw_tweets_to_parse)),
                len(raw_tweets_to_parse))

        # Generate User objects. Only really query if we absolutely have to.
        user_dict = raw_tweet.raw_status_dict["user"]
        user_id = int(user_dict["id"])
        if user and user.id == user_id:
            pass
        else:
            try:
                user = db_session.query(TwitterUser).filter_by(id=user_id).one()
            except NoResultFound:
                user = TwitterUser(user_dict)
                db_session.add(user)

        # Generate Tweet objects.
        tweet = Tweet.make_from_raw(raw_tweet)
        user.tweets.append(tweet)
    db_session.commit()

    if csv_only_tweets:
        print len(csv_only_tweets)

def download_media(db_session, storage_folder):
    media_path = os.path.join(storage_folder, "media/")
    for user in db_session.query(TwitterUser):
        user.download_media(db_session=db_session, media_path=media_path)
    for tweet in db_session.query(Tweet):
        tweet.download_media(db_session=db_session, media_path=media_path)

def print_tweets(db_session):
    for raw_tweet in db_session.query(RawTweet).all():
        print raw_tweet
<commit_msg>Can't share the counter.<commit_after>#!/usr/bin/env python
#
# Load favorites for a Twitter user and output them to a file.
#

import csv
import os
import time
import twitter

from time import sleep
from twitter.error import TwitterError
from sqlalchemy import desc
from sqlalchemy.orm.exc import NoResultFound

from myarchive.db.tables.twittertables import (
    CSVTweet, RawTweet, Tweet, TwitterUser)

from account_info import *

SLEEP_TIME = 60 * (60 / 15)

USER = "USER"
FAVORITES = "FAVORITES"

KEYS = [
    u'user',
    u'text',
    u'in_reply_to_screen_name',
    u'media',

    # Not that interesting, but saved.
    u'hashtags',
    u'id',
    u'in_reply_to_status_id',
    # Seems to be empty a lot. Put it at the end.
    u'urls',

    # Don't really care about these.
    # u'user_mentions',
    # u'source',
    # u'created_at',
    # u'id_str',
    # u'place',
    # u'in_reply_to_user_id',
    # u'lang',
    # u'possibly_sensitive',
    # u'favorited',
    # u'favorite_count',
    # u'retweeted',
    # u'retweet_count',
]

class BulkApi(twitter.Api):
    """API with an extra call."""

    def LookupStatuses(self, status_ids, trim_user=False,
                       include_entities=True):
        """
        Returns up to 100 status messages, specified by a list passed to
        status_ids.

        Args:
          status_ids:
            A list of numeric IDs of the statuses you are trying to retrieve.
          trim_user:
            When set to True, each tweet returned in a timeline will include
            a user object including only the status authors numerical ID.
            Omit this parameter to receive the complete user object. [Optional]
          include_entities:
            If False, the entities node will be disincluded.
            This node offers a variety of metadata about the tweet in a
            discreet structure, including: user_mentions, urls, and
            hashtags. [Optional]
        Returns:
          A twitter.Status instance representing that status message
        """
        url = '%s/statuses/lookup.json' % self.base_url

        parameters = dict()

        if not status_ids or len(status_ids) > 100:
            raise TwitterError(
                "status_ids must be between 1 and 100 in length.")
        # This takes a comma-separated list of up to 100 IDs.
        parameters['id'] = ",".join(status_ids)

        if trim_user:
            parameters['trim_user'] = True
        if include_entities:
            parameters['include_entities'] = True

        resp = self._RequestUrl(url, 'GET', data=parameters)
        data = self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

        return [twitter.Status.NewFromJsonDict(x) for x in data]

def archive_tweets(username, db_session, types=(USER, FAVORITES)):
    """
    Archives several types of new tweets along with their associated content.
    """
    new_tweets = []
    api = twitter.Api(
        CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET,
        sleep_on_rate_limit=True)

    for type_ in types:

        # For favorites, always do a full sweep. We can't guarantee an older
        # tweet wasn't recently favorited!
        if type_ == FAVORITES:
            since_id = None
        else:
            since_id = db_session.query(RawTweet.id).\
                filter(RawTweet.favorited_by_str.like("%%%s%%" % username)).\
                order_by(desc(RawTweet.id)).first()
        if since_id:
            since_id = since_id[0]
        print type_, since_id

        start_time = -1
        sleep_time = 0
        max_id = None
        early_termination = False
        request_index = 0
        requests_before_sleeps = 1
        statuses = []
        while not early_termination:
            # Twitter rate-limits us. Space this out a bit to avoid a
            # super-long sleep at the end doesn't kill the connection.
            if request_index >= requests_before_sleeps:
                duration = time.time() - start_time
                if duration < sleep_time:
                    sleep_duration = sleep_time - duration
                    print ("Sleeping for %s seconds to ease up on rate "
                           "limit..." % sleep_duration)
                    sleep(sleep_duration)
            start_time = time.time()
            request_index += 1

            print ("Pulling 200 tweets from API starting with ID %s and "
                   "ending with ID %s..." % (since_id, max_id))
            try:
                if type_ == FAVORITES:
                    loop_statuses = api.GetFavorites(
                        screen_name=username,
                        count=200,
                        since_id=since_id,
                        max_id=max_id,
                        include_entities=True)
                    # 15 requests per 15 minutes.
                    requests_before_sleeps = 15 - 1
                    sleep_time = 60
                elif type_ == USER:
                    loop_statuses = api.GetUserTimeline(
                        screen_name=username,
                        count=200,
                        since_id=since_id,
                        max_id=max_id)
                    # 300 requests per 15 minutes.
                    sleep_time = 3
                    requests_before_sleeps = 300 - 1
            except twitter.error.TwitterError as e:
                # If we overran the rate limit, try again.
                if e.message[0][u'code'] == 88:
                    print (
                        "Overran rate limit. Sleeping %s seconds in an attempt "
                        "to recover..." % sleep_time)
                    request_index = requests_before_sleeps
                    sleep(sleep_time)
                    continue
            print "Found %s tweets this iteration..." % len(loop_statuses)
            # Check for "We ran out of tweets via this API" termination
            # condition.
            if not loop_statuses:
                break
            # Check for early termination condition.
            for loop_status in loop_statuses:
                status_id = int(loop_status.AsDict()["id"])
                if since_id is not None and status_id >= since_id:
                    early_termination = True
                    break
                # Only append if we don't breach since_id.
                statuses.append(loop_status)
                # Capture new max_id
                if status_id < max_id or max_id is None:
                    max_id = status_id - 1

        # Format things the way we want and handle max_id changes.
        print "Adding %s tweets to DB..." % len(statuses)
        existing_rawtweet_ids = [
            returned_tuple[0]
            for returned_tuple in db_session.query(RawTweet.id).all()]
        for status in statuses:
            status_dict = status.AsDict()
            status_id = int(status_dict["id"])
            if status_id in existing_rawtweet_ids:
                continue
            raw_tweet = RawTweet(status_dict=status_dict)
            new_tweets.append(raw_tweet)
            db_session.add(raw_tweet)
            if type_ == FAVORITES:
                raw_tweet.add_user_favorite(username)
        db_session.commit()

    return new_tweets

def import_from_csv(db_session, csv_filepath, username):
    api = BulkApi(
        CONSUMER_KEY, CONSUMER_SECRET, ACCESS_KEY, ACCESS_SECRET,
        sleep_on_rate_limit=True)

    print "Importing into CSVTweets..."
    csv_tweets_by_id = dict(
        (csv_tweet[0], csv_tweet)
        for csv_tweet in db_session.query(CSVTweet).all())
    with open(csv_filepath) as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            tweet_id = int(row['tweet_id'])
            if tweet_id not in csv_tweets_by_id:
                csv_tweet = CSVTweet(
                    id=tweet_id,
                    username=username,
                    in_reply_to_status_id=row["in_reply_to_status_id"],
                    in_reply_to_user_id=row["in_reply_to_user_id"],
                    timestamp=row["timestamp"],
                    text=row["text"],
                    retweeted_status_id=row["retweeted_status_id"],
                    retweeted_status_user_id=row["retweeted_status_user_id"],
                    retweeted_status_timestamp=row["retweeted_status_timestamp"],
                    expanded_urls=row["expanded_urls"])
                db_session.add(csv_tweet)
                csv_tweets_by_id[tweet_id] = csv_tweet
    db_session.commit()

    print "Scanning for existing RawTweets..."
    existing_rawtweet_ids = [
        returned_tuple[0]
        for returned_tuple in db_session.query(RawTweet.id).all()]
    ids_to_remove = []
    for tweet_id, csv_tweet in csv_tweets_by_id.items():
        # If any aren't set as imported and should be, clean that up.
        if tweet_id in existing_rawtweet_ids:
            ids_to_remove.append(tweet_id)
            if not csv_tweet.api_import_complete:
                csv_tweet.api_import_complete = True
    db_session.commit()
    for id_to_remove in ids_to_remove:
        csv_tweets_by_id.pop(id_to_remove)

    csv_ids = list(csv_tweets_by_id.keys())
    num_imports = len(csv_ids)
    print "Attempting API import of %s tweets based on CSV file..." % (
        num_imports)

    # API allows 60 requests per 15 minutes.
    sleep_time = 15
    requests_before_sleeps = 60 - 1

    # Set loop starting values
    tweet_index = 0
    request_index = 0
    start_time = -1
    new_api_tweets = []
    sliced_ids = csv_ids[:100]
    while sliced_ids:

        # Sleep to not hit the rate limit.
        # Twitter rate-limits us. Space this out a bit to avoid a
        # super-long sleep at the end doesn't kill the connection.
        if request_index >= requests_before_sleeps:
            duration = time.time() - start_time
            if duration < sleep_time:
                sleep_duration = sleep_time - duration
                print ("Sleeping for %s seconds to ease up on rate "
                       "limit..." % sleep_duration)
                sleep(sleep_duration)
        request_index += 1
        start_time = time.time()

        # Perform the import.
        print "Attempting import of id %s to %s of %s..." % (
            tweet_index + 1, min(tweet_index + 100, num_imports), num_imports)
        try:
            statuses = api.LookupStatuses(
                status_ids=[str(sliced_id) for sliced_id in sliced_ids],
                trim_user=False,
                include_entities=True)
            for status in statuses:
                status_dict = status.AsDict()
                # Create the RawTweet
                raw_tweet = RawTweet(status_dict=status_dict)
                db_session.add(raw_tweet)
                # Mark CSVTweet appropriately.
                csv_tweet = csv_tweets_by_id[int(status_dict["id"])]
                csv_tweet.api_import_complete = True
                # Append to new list.
                new_api_tweets.append(raw_tweet)
            db_session.commit()

        except TwitterError as e:
            print e
        tweet_index += 100
        sliced_ids = csv_ids[request_index:100 + request_index]

    csv_only_tweets = db_session.query(CSVTweet.api_import_complete).\
        filter_by(api_import_complete=False).all()

    return new_api_tweets, csv_only_tweets

def parse_tweets(db_session, raw_tweets=None, csv_only_tweets=None,
                 parse_all_raw=False):
    user = None

    if parse_all_raw is True:
        # Process all captured raw tweets.
        raw_tweets = db_session.query(RawTweet)

    # Filter out existing tweets, making sure to compare with a a tuple since
    # SQLAlchemy will return a list of tuples.
    existing_tweet_ids = db_session.query(Tweet.id).all()
    raw_tweets_to_parse = [
        raw_tweet for raw_tweet in raw_tweets
        if (int(raw_tweet.raw_status_dict["id"]),) not in existing_tweet_ids]
    print "Found %s tweets to parse." % len(raw_tweets_to_parse)

    for index, raw_tweet in enumerate(raw_tweets_to_parse):
        if index % 100 == 0:
            print "Parsing tweet %s to %s of %s..." % (
                index,
                min(index + 100, len(raw_tweets_to_parse)),
                len(raw_tweets_to_parse))

        # Generate User objects. Only really query if we absolutely have to.
        user_dict = raw_tweet.raw_status_dict["user"]
        user_id = int(user_dict["id"])
        if user and user.id == user_id:
            pass
        else:
            try:
                user = db_session.query(TwitterUser).filter_by(id=user_id).one()
            except NoResultFound:
                user = TwitterUser(user_dict)
                db_session.add(user)

        # Generate Tweet objects.
        tweet = Tweet.make_from_raw(raw_tweet)
        user.tweets.append(tweet)
    db_session.commit()

    if csv_only_tweets:
        print len(csv_only_tweets)

def download_media(db_session, storage_folder):
    media_path = os.path.join(storage_folder, "media/")
    for user in db_session.query(TwitterUser):
        user.download_media(db_session=db_session, media_path=media_path)
    for tweet in db_session.query(Tweet):
        tweet.download_media(db_session=db_session, media_path=media_path)

def print_tweets(db_session):
    for raw_tweet in db_session.query(RawTweet).all():
        print raw_tweet
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# Banco de dados
db = DAL('sqlite://storage.sqlite', pool_size=1, check_reserved=['all'])

# by default give a view/generic.extension to all actions from localhost
# none otherwise. a pattern can be 'controller/function.extension'
response.generic_patterns = ['*'] if request.is_local else []

from gluon.tools import Auth
# autenticação do usuário
auth = Auth(db)

# campos adicionais do usuário
auth.settings.extra_fields['auth_user'] = [
    Field('instituicao', label="Instituição de Ensino", length=120),
    Field('matricula', label='Matrícula', length=32),
    Field('telefone')
]

# create all tables needed by auth if not custom tables
auth.define_tables(username=False, signature=False)

# configure email
mail = auth.settings.mailer
mail.settings.server = 'logging' if request.is_local else 'smtp.gmail.com:587'
mail.settings.sender = 'you@gmail.com'
mail.settings.login = 'username:password'

# configure auth policy
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.settings.reset_password_requires_verification = True

db.define_table(
    'evento',
    Field('nome', length=120, notnull=True),
    Field(
        'data_hora_inicio',
        'datetime',
        label="Data/Horário Inicial",
        notnull=True
    ),
    Field(
        'data_hora_final',
        'datetime',
        label="Data/Horário Final",
        notnull=True
    ),
    Field('localizacao', notnull=True, length=540, label="Localização"),
    Field('descricao', 'text', label="Descrição", notnull=True),
    Field(
        'url_youtube',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Youtube (https://youtube.com/seu-canal) "
    ),
    Field(
        'url_facebook',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Facebook (https://facebook.com/seu-nome)",
    ),
    Field(
        'url_twitter',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Twitter (https://twitter.com/seu-nome)",
    ),
    Field(
        'url_gplus',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Google Plus (https://plus.google.com/u/0/+Seu-nome)"
    ),
)
<commit_msg>No changes after this point. SQN<commit_after># -*- coding: utf-8 -*-

# Banco de dados
db = DAL('sqlite://storage.sqlite', pool_size=1, check_reserved=['all'])

# by default give a view/generic.extension to all actions from localhost
# none otherwise. a pattern can be 'controller/function.extension'
response.generic_patterns = ['*'] if request.is_local else []

from gluon.tools import Auth
# autenticação do usuário
auth = Auth(db)

# campos adicionais do usuário
auth.settings.extra_fields['auth_user'] = [
    Field('instituicao', label="Instituição de Ensino", length=120),
    Field('matricula', label='Matrícula', length=32),
    Field('telefone')
]

# create all tables needed by auth if not custom tables
auth.define_tables(username=False, signature=False)

# configure email
mail = auth.settings.mailer
mail.settings.server = 'logging' if request.is_local else 'smtp.gmail.com:587'
mail.settings.sender = 'you@gmail.com'
mail.settings.login = 'username:password'

# configure auth policy
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.settings.reset_password_requires_verification = True

db.define_table(
    'evento',
    Field('nome', length=120, notnull=True),
    Field(
        'data_hora_inicio',
        'datetime',
        label="Data/Horário Inicial",
        notnull=True
    ),
    Field(
        'data_hora_final',
        'datetime',
        label="Data/Horário Final",
        notnull=True
    ),
    Field('localizacao', notnull=True, length=540, label="Localização"),
    Field('descricao', 'text', label="Descrição", notnull=True),
    Field(
        'url_youtube',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Youtube (https://youtube.com/seu-canal) "
    ),
    Field(
        'url_facebook',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Facebook (https://facebook.com/seu-nome)",
    ),
    Field(
        'url_twitter',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Twitter (https://twitter.com/seu-nome)",
    ),
    Field(
        'url_gplus',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Google Plus (https://plus.google.com/u/0/+Seu-nome)"
    ),
)

db.define_table(
	'organizador',
	Field('nome', length=120, notnull=True),
	Field('foto', 'upload', requires=IS_EMPTY_OR(IS_IMAGE()) ),
	Field(
        'url_facebook',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Facebook (https://facebook.com/seu-nome)",
    ),
    Field(
        'url_twitter',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Twitter (https://twitter.com/seu-nome)",
    ),
    Field(
        'url_gplus',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Google Plus (https://plus.google.com/u/0/+Seu-nome)"
    ),
	Field(
        'url_Github',
        requires=IS_EMPTY_OR(IS_URL()),
        label="Github (https://github.com/Seu-Git)"
    ),
	Field(
        'url_LinkedIn',
        requires=IS_EMPTY_OR(IS_URL()),
        label="LinkedIn "
    ),
)<|endoftext|>


<commit_before>#!/bin/env python
#
#
# Test program for flock of birds.
#
# Relies upon pySerial: pyserial.sf.net
#
import serial
import sys
import getopt
import Command
import FOB

def scan():
    """scan for available ports. return a list of tuples (num, name)"""
    available = []
    for i in range(256):
        try:
            s = serial.Serial(i)
            available.append( (i, s.portstr))
            s.close()   #explicit close 'cause of delayed GC in java
        except serial.serialutil.SerialException:
            pass
    return available

def usage():
    print """Program Usage:
   --test=output - Just print the data read from the bird for fob test output
          echo - Take line of input, send to bird and then read it back
          host_data_read - Perform the host data read bird test.
   --set_rts=<new val>
   --info=basic - Print basic information about the flock
   --scan - Scan for what birds are connected.
    """

def scanFbb():
    print "Scanning bird bus:"
    fob = FOB.Flock()
    fob.open()

    for i in range(14):
       print "bird: ", i,
       fob.setCmdAddr(i)
       bstatus = fob.getBirdStatus()
       print bstatus

def printBasicFlockInfo():
    " Print basic flock information "
    print "Getting basic flock info:"

    fob = FOB.Flock()
    fob.open()

    sw_ver = fob.getSoftwareRevision()
    cur_error_code = fob.getCurrentError()
    addr_mode = fob.getAddressingMode()
    bird_status = fob.getBirdStatus()
    bird_addr = fob.getBirdAddress()

    print "  Software revision: ", sw_ver
    print "  Current error code: ", cur_error_code
    print "  Addr mode: ", addr_mode
    print "  Bird addr: ", bird_addr
    print "  bird status: ", bird_status
    fob.printFlockSystemStatus()

    if(0 == bird_addr):
        print "Standalone mode.  Can't autoconfig"
    else:
        print "Auto-configing the flock...."
        fob.autoConfig(3)

    fob.printFlockSystemStatus()

def test_output():
    print "---- Testing output -----"
    print "Reading 10 OK samples"
    fob = FOB.Flock()
    port = fob.port

    port.flushInput()
    port.flushOutput()

    for i in xrange(10):
        rstr = port.read(4)
        print "%s: %s"%(i,rstr)

    port.close()

def test_echo():
    print """---- Testing echo -----
Read and echo data. 'q' to quit."""

    fob = FOB.Flock()
    port = fob.port
    port.flushInput()
    port.flushOutput()

    raw_str = raw_input("> ")
    while(raw_str != 'q'):
        port.write(raw_str)
        rstr = port.read(len(raw_str))
        print rstr
        raw_str = raw_input("> ")

    port.close()

def test_host_data_read():
    """ Each time data goes to the birt it will respond with 0-255 decimal.
        This is as a response in sequence.
        """
    fob = openBirdConnection()
    vals = []

    for i in xrange(255):
        fob.write('a')
        val = ord(fob.read(1))
        print val
        vals.append(val)

    good_vals = range(256)
    if(good_vals == vals):
        print "Test passed."
    else:
        print "Test failed."

    fob.close()

def set_rts(arg):
    ser = serial.Serial(0)
    print "Port: ", ser
    print "  Setting rts: ", arg
    ser.setRTS(arg)
    ser.close()

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h",
                                   ["test=", "help", "set_rts=", "info=", "scan"])
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    if not len(opts):
        usage()
        sys.exit()
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("--test",):
            if "output" == a:
                test_output()
            elif "echo" == a:
                test_echo()
            elif "host_data_read" == a:
                test_host_data_read()
        if o in ("--set_rts",):
            if "0" == a:
                set_rts(0)
            else:
                set_rts(1)
        if o in ("--info",):
            if "basic" == a:
                printBasicFlockInfo()
            else:
                printBasicFlockInfo()
        if o in ("--scan",):
            scanFbb()

if __name__=='__main__':
    #print "Found ports:"
    #for n,s in scan():
    #    print "(%d) %s" % (n,s)

    main()

<commit_msg>Updated code for ok printing<commit_after>#!/bin/env python
#
#
# Test program for flock of birds.
#
# Relies upon pySerial: pyserial.sf.net
#
import serial
import sys
import getopt
import Command
import FOB

def scan():
    """scan for available ports. return a list of tuples (num, name)"""
    available = []
    for i in range(256):
        try:
            s = serial.Serial(i)
            available.append( (i, s.portstr))
            s.close()   #explicit close 'cause of delayed GC in java
        except serial.serialutil.SerialException:
            pass
    return available

def usage():
    print """Program Usage:
   --test=output - Just print the data read from the bird for fob test output
          echo - Take line of input, send to bird and then read it back
          host_data_read - Perform the host data read bird test.
   --set_rts=<new val>
   --info=basic - Print basic information about the flock
   --scan - Scan for what birds are connected.
    """

def scanFbb():
    print "Scanning bird bus:"
    fob = FOB.Flock()
    fob.open()

    for i in range(14):
       print "bird: ", i,
       fob.setCmdAddr(i)
       bstatus = fob.getBirdStatus()
       print bstatus

def printBasicFlockInfo():
    " Print basic flock information "
    print "Getting basic flock info:"

    fob = FOB.Flock()
    fob.open()

    sw_ver = fob.getSoftwareRevision()
    cur_error_code = fob.getCurrentError()
    addr_mode = fob.getAddressingMode()
    bird_status = fob.getBirdStatus()
    bird_addr = fob.getBirdAddress()

    print "  Software revision: ", sw_ver
    print "  Current error code: ", cur_error_code
    print "  Addr mode: ", addr_mode
    print "  Bird addr: ", bird_addr
    print "  bird status: ", bird_status
    fob.printFlockSystemStatus()

    if(0 == bird_addr):
        print "Standalone mode.  Can't autoconfig"
    else:
        print "Auto-configing the flock...."
        fob.autoConfig(3)

    fob.printFlockSystemStatus()

def test_output():
    print "---- Testing output -----"
    fob = FOB.Flock()
    fob.open();

    print "\nReading 10 OK samples"
    port = fob.port

    port.flushInput()
    port.flushOutput()

    for i in xrange(10):
        rstr = port.read(4)
        print "%s: %s"%(i,rstr)

    port.close()

def test_echo():
    print """---- Testing echo -----
Read and echo data. 'q' to quit."""

    fob = FOB.Flock()
    port = fob.port
    port.flushInput()
    port.flushOutput()

    raw_str = raw_input("> ")
    while(raw_str != 'q'):
        port.write(raw_str)
        rstr = port.read(len(raw_str))
        print rstr
        raw_str = raw_input("> ")

    port.close()

def test_host_data_read():
    """ Each time data goes to the birt it will respond with 0-255 decimal.
        This is as a response in sequence.
        """
    fob = openBirdConnection()
    vals = []

    for i in xrange(255):
        fob.write('a')
        val = ord(fob.read(1))
        print val
        vals.append(val)

    good_vals = range(256)
    if(good_vals == vals):
        print "Test passed."
    else:
        print "Test failed."

    fob.close()

def set_rts(arg):
    ser = serial.Serial(0)
    print "Port: ", ser
    print "  Setting rts: ", arg
    ser.setRTS(arg)
    ser.close()

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h",
                                   ["test=", "help", "set_rts=", "info=", "scan"])
    except getopt.GetoptError:
        # print help information and exit:
        usage()
        sys.exit(2)
    if not len(opts):
        usage()
        sys.exit()
    for o, a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit()
        if o in ("--test",):
            if "output" == a:
                test_output()
            elif "echo" == a:
                test_echo()
            elif "host_data_read" == a:
                test_host_data_read()
        if o in ("--set_rts",):
            if "0" == a:
                set_rts(0)
            else:
                set_rts(1)
        if o in ("--info",):
            if "basic" == a:
                printBasicFlockInfo()
            else:
                printBasicFlockInfo()
        if o in ("--scan",):
            scanFbb()

if __name__=='__main__':
    #print "Found ports:"
    #for n,s in scan():
    #    print "(%d) %s" % (n,s)

    main()

<|endoftext|>


<commit_before>"""Variant calling using samtools mpileup and bcftools.

http://samtools.sourceforge.net/mpileup.shtml
"""
import os
import subprocess

from bcbio import broad
from bcbio.utils import file_exists
from bcbio.distributed.transaction import file_transaction
from bcbio.log import logger
from bcbio.pipeline import config_utils
from bcbio.pipeline.shared import subset_variant_regions
from bcbio.variation.genotype import write_empty_vcf
from bcbio.variation import bamprep, realign

def shared_variantcall(call_fn, name, align_bams, ref_file, config,
                       dbsnp=None, region=None, out_file=None):
    """Provide base functionality for prepping and indexing for variant calling.
    """
    broad_runner = broad.runner_from_config(config)
    for x in align_bams:
        broad_runner.run_fn("picard_index", x)
    if out_file is None:
        out_file = "%s-variants.vcf" % os.path.splitext(align_bams[0])[0]
    if not file_exists(out_file):
        logger.info("Genotyping with {name}: {region} {fname}".format(name=name,
            region=region, fname=os.path.basename(align_bams[0])))
        variant_regions = config["algorithm"].get("variant_regions", None)
        target_regions = subset_variant_regions(variant_regions, region, out_file)
        if ((variant_regions is not None and isinstance(target_regions, basestring)
              and not os.path.isfile(target_regions))
              or not all(realign.has_aligned_reads(x, region) for x in align_bams)):
            write_empty_vcf(out_file)
        else:
            with file_transaction(out_file) as tx_out_file:
                call_fn(align_bams, ref_file, config, target_regions,
                        tx_out_file)
    return out_file

def run_samtools(align_bams, ref_file, config, dbsnp=None, region=None,
                 out_file=None):
    """Detect SNPs and indels with samtools mpileup and bcftools.
    """
    return shared_variantcall(_call_variants_samtools, "samtools", align_bams, ref_file,
                              config, dbsnp, region, out_file)

def prep_mpileup(align_bams, ref_file, max_read_depth, config,
                 target_regions=None, want_bcf=True):
    cl = [config_utils.get_program("samtools", config), "mpileup",
          "-f", ref_file, "-d", str(max_read_depth), "-L", str(max_read_depth),
          "-m", "3", "-F", "0.0002"]
    if want_bcf:
        cl += ["-D", "-S", "-u"]
    if target_regions:
        cl += ["-l", bamprep.region_to_gatk(target_regions)]
    cl += align_bams
    return " ".join(cl)

def _call_variants_samtools(align_bams, ref_file, config, target_regions, out_file):
    """Call variants with samtools in target_regions.
    """
    max_read_depth = "1000"
    mpileup = prep_mpileup(align_bams, ref_file, max_read_depth, config,
                           target_regions=target_regions)
    bcftools = config_utils.get_program("bcftools", config)
    vcfutils = config_utils.get_program("vcfutils.pl", config)
    cmd = ("{mpileup} "
           "| {bcftools} view -v -c -g - "
           "| {vcfutils} varFilter -D {max_read_depth} "
           "> {out_file}")
    subprocess.check_call(cmd.format(**locals()), shell=True)
<commit_msg>Correctly restrict samtools mpileup by region when no BED file of calling regions supplied<commit_after>"""Variant calling using samtools mpileup and bcftools.

http://samtools.sourceforge.net/mpileup.shtml
"""
import os
import subprocess

from bcbio import broad
from bcbio.utils import file_exists
from bcbio.distributed.transaction import file_transaction
from bcbio.log import logger
from bcbio.pipeline import config_utils
from bcbio.pipeline.shared import subset_variant_regions
from bcbio.variation.genotype import write_empty_vcf
from bcbio.variation import bamprep, realign

def shared_variantcall(call_fn, name, align_bams, ref_file, config,
                       dbsnp=None, region=None, out_file=None):
    """Provide base functionality for prepping and indexing for variant calling.
    """
    broad_runner = broad.runner_from_config(config)
    for x in align_bams:
        broad_runner.run_fn("picard_index", x)
    if out_file is None:
        out_file = "%s-variants.vcf" % os.path.splitext(align_bams[0])[0]
    if not file_exists(out_file):
        logger.info("Genotyping with {name}: {region} {fname}".format(name=name,
            region=region, fname=os.path.basename(align_bams[0])))
        variant_regions = config["algorithm"].get("variant_regions", None)
        target_regions = subset_variant_regions(variant_regions, region, out_file)
        if ((variant_regions is not None and isinstance(target_regions, basestring)
              and not os.path.isfile(target_regions))
              or not all(realign.has_aligned_reads(x, region) for x in align_bams)):
            write_empty_vcf(out_file)
        else:
            with file_transaction(out_file) as tx_out_file:
                call_fn(align_bams, ref_file, config, target_regions,
                        tx_out_file)
    return out_file

def run_samtools(align_bams, ref_file, config, dbsnp=None, region=None,
                 out_file=None):
    """Detect SNPs and indels with samtools mpileup and bcftools.
    """
    return shared_variantcall(_call_variants_samtools, "samtools", align_bams, ref_file,
                              config, dbsnp, region, out_file)

def prep_mpileup(align_bams, ref_file, max_read_depth, config,
                 target_regions=None, want_bcf=True):
    cl = [config_utils.get_program("samtools", config), "mpileup",
          "-f", ref_file, "-d", str(max_read_depth), "-L", str(max_read_depth),
          "-m", "3", "-F", "0.0002"]
    if want_bcf:
        cl += ["-D", "-S", "-u"]
    if target_regions:
        str_regions = bamprep.region_to_gatk(target_regions)
        if os.path.isfile(str_regions):
            cl += ["-l", str_regions]
        else:
            cl += ["-r", str_regions]
    cl += align_bams
    return " ".join(cl)

def _call_variants_samtools(align_bams, ref_file, config, target_regions, out_file):
    """Call variants with samtools in target_regions.
    """
    max_read_depth = "1000"
    mpileup = prep_mpileup(align_bams, ref_file, max_read_depth, config,
                           target_regions=target_regions)
    bcftools = config_utils.get_program("bcftools", config)
    vcfutils = config_utils.get_program("vcfutils.pl", config)
    cmd = ("{mpileup} "
           "| {bcftools} view -v -c -g - "
           "| {vcfutils} varFilter -D {max_read_depth} "
           "> {out_file}")
    logger.info(cmd.format(**locals()))
    subprocess.check_call(cmd.format(**locals()), shell=True)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import logging
l = logging.getLogger(__name__)

from contextlib import contextmanager

from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase as DjangoTestCase
from django.utils.importlib import import_module
from django.utils.functional import LazyObject

from django_lean.experiments.loader import ExperimentLoader
from django_lean.experiments.models import Participant
from django_lean.lean_analytics import reset_caches

def get_session(session_key):
    engine = import_module(settings.SESSION_ENGINE)
    session = engine.SessionStore(session_key)
    session.load()
    return session

def create_user_in_group(experiment, i, group, enrollment_date):
    user = User(username="user%s_%s" % (i, group),
                email="email%s_%s@example.com" % (i, group))
    user.save()
    experiment_participant = Participant(user=user,
                                         experiment=experiment,
                                         group=group)
    experiment_participant.save()
    experiment_participant.enrollment_date = enrollment_date
    experiment_participant.save()
    return user

class TestCase(DjangoTestCase):
    def _pre_setup(self):
        super(TestCase, self)._pre_setup()
        experiments = getattr(self, 'experiments', [])
        ExperimentLoader.load_all_experiments(apps=experiments)
        self.original_LEAN_ANALYTICS = getattr(settings, 'LEAN_ANALYTICS', [])
        settings.LEAN_ANALYTICS = []
        reset_caches()

    def _post_teardown(self):
        if hasattr(settings, 'LEAN_ANALYTICS'):
            settings.LEAN_ANALYTICS = self.original_LEAN_ANALYTICS
        super(TestCase, self)._post_teardown()

class TestUser(object):
    """
    A utility class for testing that implements an 'ExperimentUser' object
    useful in tests.
    """
    def __init__(self, username=None, anonymous_visitor=None,
                 verified_human=True):
        if anonymous_visitor:
            self.anonymous_id = anonymous_visitor.id
        else:
            self.anonymous_id = None

        if not username == None:
            self.user, created = User.objects.get_or_create(
                username=username, email="%s@example.com" % username)
        else:
            self.user = None

        self.verified_human = verified_human
        self.session = {}
        self.temporary_enrollments = {}

    def is_anonymous(self):
        return self.user == None

    def set_anonymous_id(self, anonymous_id):
        self.anonymous_id = anonymous_id

    def get_anonymous_id(self):
        return self.anonymous_id

    def get_registered_user(self):
        return self.user

    def is_verified_human(self):
        return self.verified_human

    def store_temporary_enrollment(self, experiment_name, group_id):
        self.temporary_enrollments[experiment_name] = group_id

    def get_added_enrollments(self):
        return self.temporary_enrollments

    def get_temporary_enrollment(self, experiment_name):
        added_enrollments = self.get_added_enrollments()
        if not added_enrollments:
            return None
        else:
            return added_enrollments.get(experiment_name)

@contextmanager
def patch(namespace, name, value):
    """Patches `namespace`.`name` with `value`."""
    if isinstance(namespace, LazyObject):
        if namespace._wrapped is None:
            namespace._setup()
        namespace = namespace._wrapped

    try:
        original = getattr(namespace, name)
    except AttributeError:
        original = NotImplemented
    try:
        if value is NotImplemented:
            if original is not NotImplemented:
                delattr(namespace, name)
        else:
            setattr(namespace, name, value)
        yield
    finally:
        if original is NotImplemented:
            if value is not NotImplemented:
                delattr(namespace, name)
        else:
            setattr(namespace, name, original)
<commit_msg>use TransactionTestCase<commit_after># -*- coding: utf-8 -*-
import logging
l = logging.getLogger(__name__)

from contextlib import contextmanager

from django.conf import settings
from django.contrib.auth.models import User
from django.test import TransactionTestCase as DjangoTestCase
from django.utils.importlib import import_module
from django.utils.functional import LazyObject

from django_lean.experiments.loader import ExperimentLoader
from django_lean.experiments.models import Participant
from django_lean.lean_analytics import reset_caches

def get_session(session_key):
    engine = import_module(settings.SESSION_ENGINE)
    session = engine.SessionStore(session_key)
    session.load()
    return session

def create_user_in_group(experiment, i, group, enrollment_date):
    user = User(username="user%s_%s" % (i, group),
                email="email%s_%s@example.com" % (i, group))
    user.save()
    experiment_participant = Participant(user=user,
                                         experiment=experiment,
                                         group=group)
    experiment_participant.save()
    experiment_participant.enrollment_date = enrollment_date
    experiment_participant.save()
    return user

class TestCase(DjangoTestCase):
    def _pre_setup(self):
        super(TestCase, self)._pre_setup()
        experiments = getattr(self, 'experiments', [])
        ExperimentLoader.load_all_experiments(apps=experiments)
        self.original_LEAN_ANALYTICS = getattr(settings, 'LEAN_ANALYTICS', [])
        settings.LEAN_ANALYTICS = []
        reset_caches()

    def _post_teardown(self):
        if hasattr(settings, 'LEAN_ANALYTICS'):
            settings.LEAN_ANALYTICS = self.original_LEAN_ANALYTICS
        super(TestCase, self)._post_teardown()

class TestUser(object):
    """
    A utility class for testing that implements an 'ExperimentUser' object
    useful in tests.
    """
    def __init__(self, username=None, anonymous_visitor=None,
                 verified_human=True):
        if anonymous_visitor:
            self.anonymous_id = anonymous_visitor.id
        else:
            self.anonymous_id = None

        if not username == None:
            self.user, created = User.objects.get_or_create(
                username=username, email="%s@example.com" % username)
        else:
            self.user = None

        self.verified_human = verified_human
        self.session = {}
        self.temporary_enrollments = {}

    def is_anonymous(self):
        return self.user == None

    def set_anonymous_id(self, anonymous_id):
        self.anonymous_id = anonymous_id

    def get_anonymous_id(self):
        return self.anonymous_id

    def get_registered_user(self):
        return self.user

    def is_verified_human(self):
        return self.verified_human

    def store_temporary_enrollment(self, experiment_name, group_id):
        self.temporary_enrollments[experiment_name] = group_id

    def get_added_enrollments(self):
        return self.temporary_enrollments

    def get_temporary_enrollment(self, experiment_name):
        added_enrollments = self.get_added_enrollments()
        if not added_enrollments:
            return None
        else:
            return added_enrollments.get(experiment_name)

@contextmanager
def patch(namespace, name, value):
    """Patches `namespace`.`name` with `value`."""
    if isinstance(namespace, LazyObject):
        if namespace._wrapped is None:
            namespace._setup()
        namespace = namespace._wrapped

    try:
        original = getattr(namespace, name)
    except AttributeError:
        original = NotImplemented
    try:
        if value is NotImplemented:
            if original is not NotImplemented:
                delattr(namespace, name)
        else:
            setattr(namespace, name, value)
        yield
    finally:
        if original is NotImplemented:
            if value is not NotImplemented:
                delattr(namespace, name)
        else:
            setattr(namespace, name, original)
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Netius System
# Copyright (C) 2008-2012 Hive Solutions Lda.
#
# This file is part of Hive Netius System.
#
# Hive Netius System is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hive Netius System is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Hive Netius System. If not, see <http://www.gnu.org/licenses/>.

__author__ = "João Magalhães joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2012 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "GNU General Public License (GPL), Version 3"
""" The license for the module """

import netius.servers

class ReverseProxyServer(netius.servers.ProxyServer):

    def __init__(self, config = "proxy.json", hosts = {}, *args, **kwargs):
        netius.servers.ProxyServer.__init__(self, *args, **kwargs)
        self.load_config(path = config, hosts = hosts)

    def on_data_http(self, connection, parser):
        netius.servers.ProxyServer.on_data_http(self, connection, parser)

        prefix = None
        method = parser.method.upper()
        path = parser.path_s
        headers = parser.headers
        version_s = parser.version_s

        host = headers.get("host", None)
        for _host, _prefix in self.hosts.iteritems():
            if not _host == host: continue
            prefix = _prefix
            url = prefix + path
            break

        if not prefix:
            self.debug("No valid proxy endpoint found for '%s'" % host)
            connection.send_response(
                data = "No valid proxy endpoint found",
                version = version_s,
                code = 404,
                code_s = "Not Found",
                callback = self._prx_close
            )
            return

        # verifies if the current connection contains an existing proxy
        # connection if that's the case that must be unset from the
        # connection and from the connection map internal structures
        proxy_c = hasattr(connection, "proxy_c") and connection.proxy_c
        proxy_c = proxy_c or None
        connection.proxy_c = None
        if proxy_c in self.conn_map:
            proxy_c.close()
            del self.conn_map[proxy_c]

        _connection = self.http_client.method(
            method,
            url,
            headers = parser.headers,
            data = parser.get_message(),
            version = version_s,
            connection = proxy_c
        )

        self.debug("Setting connection as waiting, proxy connection loading ...")

        _connection.waiting = True
        connection.proxy_c = _connection
        self.conn_map[_connection] = connection

if __name__ == "__main__":
    import logging
    hosts = {
        "host.com" : "http://localhost"
    }
    server = ReverseProxyServer(
        hosts = hosts,
        level = logging.INFO
    )
    server.serve(env = True)
<commit_msg>small code fix<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Netius System
# Copyright (C) 2008-2012 Hive Solutions Lda.
#
# This file is part of Hive Netius System.
#
# Hive Netius System is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hive Netius System is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Hive Netius System. If not, see <http://www.gnu.org/licenses/>.

__author__ = "João Magalhães joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2012 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "GNU General Public License (GPL), Version 3"
""" The license for the module """

import netius.servers

class ReverseProxyServer(netius.servers.ProxyServer):

    def __init__(self, config = "proxy.json", hosts = {}, *args, **kwargs):
        netius.servers.ProxyServer.__init__(self, *args, **kwargs)
        self.load_config(path = config, hosts = hosts)

    def on_data_http(self, connection, parser):
        netius.servers.ProxyServer.on_data_http(self, connection, parser)

        prefix = None
        method = parser.method.upper()
        path = parser.path_s
        headers = parser.headers
        version_s = parser.version_s

        host = headers.get("host", None)
        for _host, _prefix in self.hosts.iteritems():
            if not _host == host: continue
            prefix = _prefix
            url = prefix + path
            break

        if not prefix:
            self.debug("No valid proxy endpoint found for '%s'" % host)
            connection.send_response(
                data = "No valid proxy endpoint found",
                version = version_s,
                code = 404,
                code_s = "Not Found",
                callback = self._prx_close
            )
            return

        # verifies if the current connection contains an existing proxy
        # connection if that's the case that must be unset from the
        # connection and from the connection map internal structures
        proxy_c = hasattr(connection, "proxy_c") and connection.proxy_c
        proxy_c = proxy_c or None
        connection.proxy_c = None
        if proxy_c in self.conn_map:
            proxy_c.close()
            if proxy_c in self.conn_map: del self.conn_map[proxy_c]

        _connection = self.http_client.method(
            method,
            url,
            headers = parser.headers,
            data = parser.get_message(),
            version = version_s,
            connection = proxy_c
        )

        self.debug("Setting connection as waiting, proxy connection loading ...")

        _connection.waiting = True
        connection.proxy_c = _connection
        self.conn_map[_connection] = connection

if __name__ == "__main__":
    import logging
    hosts = {
        "host.com" : "http://localhost"
    }
    server = ReverseProxyServer(
        hosts = hosts,
        level = logging.INFO
    )
    server.serve(env = True)
<|endoftext|>


<commit_before>from gluon.tools import *
import uuid, datetime, re, os, time, stat
now=datetime.datetime.now()

migrate = DEV_TEST
fake_migrate = False

if SUSPEND_SERVICE:
    raise HTTP(503, "
Service is momentarily unavailable
")

if is_gae:
    db=GQLDB()
    session.connect(request,response,db=db)
else:
    db=DAL(DBURI, pool_size=20)

PAST=datetime.datetime.today()-datetime.timedelta(minutes=1)

#### cleanup sessions
##ps=os.path.join(request.folder,'sessions')
##try: [os.unlink(os.path.join(ps,f)) for f in os.listdir(ps) if os.stat(os.path.join(ps,f))[stat.ST_MTIME]<time.time()-3600]
##except: pass
#### end cleanup sessions

def wysiwyg(field,value):
    return DIV(field.name,TEXTAREA(_name=field.name, _cols="60", value=value,
                                   _id="wysiwyg",
                                   _style="width: 810px; height: 200px;"))

######################################
### PERSON
######################################

db.define_table('auth_user',
    ##Field('username', length=512, default='', label=T('Username')),
    db.Field('first_name',length=128,label=T('First Name')),
    db.Field('last_name',length=128,label=T('Last Name')),
    db.Field('email',length=128),
    db.Field('password','password',default='',label=T('Password'),readable=False),
    db.Field('level','text',label=T('Python knowledge level'),requires=IS_IN_SET(('principiante','intermedio','avanzado'))),
    db.Field('tutorials','list:string',label=T('Tutorials'),readable=False,writable=False),
    db.Field('dni','integer'),
    db.Field('certificate','boolean',default=False,label=T('I want a certificate of attendance'),readable=False,writable=False),
    db.Field('address',length=255,label=T('Mailing Address'),default=''),
    db.Field('city',label=T('City'),default=''),
    db.Field('state',label=T('State'),default='Buenos Aires'),
    db.Field('country',label=T('Country'),default='Argentina'),
    db.Field('zip_code',label=T('Zip/Postal Code'),default=''),
    db.Field('phone_number',label=T('Phone Number'),comment=T('(cellphone)')),
    db.Field('include_in_delegate_listing','boolean',default=True,label=T('Include in Delegates List')),
    db.Field('sponsors','boolean',default=True,label=T('Contacto con Auspiciantes'),readable=True,writable=True),
    db.Field('twitter_username',length=64,label=T('Twitter username'),default=''),
    db.Field('personal_home_page',length=128,label=T('Personal Home Page'),default=''),
    db.Field('company_name',label=T('Entity Name'),default=''),
    db.Field('company_home_page',length=128,label=T('Entity Home Page'),default=''),
    db.Field('badge_line1',label=T('Badge Line 1'),default='',readable=False,writable=False),
    db.Field('badge_line2',label=T('Badge Line 2'),default='',readable=False,writable=False),
    db.Field('sponsor_id',"reference sponsor",label=T('Sponsor'),readable=False,writable=False),
    db.Field('t_shirt_size',label=T('T-shirt Size')),
    db.Field('attendee_type',label=T('Registration Type'),default=ATTENDEE_TYPES[0][0],readable=False,writable=False),
    db.Field('discount_coupon',length=64,label=T('Discount Coupon'), readable=False,writable=False),
    db.Field('donation','double',default=0.0,label=T('Donation to PyAr'),readable=False,writable=False),
    db.Field('amount_billed','double',default=0.0,readable=False,writable=False),
    db.Field('amount_added','double',default=0.0,readable=False,writable=False),
    db.Field('amount_subtracted','double',default=0.0,readable=False,writable=False),
    db.Field('amount_paid','double',default=0.0,readable=False,writable=False),
    db.Field('amount_due','double',default=0.0,readable=False,writable=False),
    db.Field('resume','text',label=T('Resume (Bio)'),readable=True,writable=True),
    db.Field('photo','upload',label=T('Photo'), readable=True,writable=True),
    db.Field('cv','upload',label=T('CV'),readable=True,writable=True),
    db.Field('speaker','boolean',default=False,readable=False,writable=False),
    db.Field('session_chair','boolean',default=False,readable=False,writable=False),
    db.Field('manager','boolean',default=False,readable=False,writable=False),
    db.Field('reviewer','boolean',default=True,readable=False,writable=False),
    db.Field('latitude','double',default=0.0,readable=False,writable=False),
    db.Field('longitude','double',default=0.0,readable=False,writable=False),
    db.Field('confirmed','boolean',default=False,writable=False,readable=False),
    db.Field('registration_key',length=64,default='',readable=False,writable=False),
    db.Field('reset_password_key',length=64,default='',readable=False,writable=False),
    Field('reset_password_key', length=512, writable=False, readable=False, default=''),
    Field('registration_id', length=512, writable=False, readable=False, default=''),
    db.Field('created_by_ip',readable=False,writable=False,default=request.client),
    db.Field('created_on','datetime',readable=False,writable=False,default=request.now),
    ##db.Field('cena_viernes','boolean', comment="-con cargo-"),
    ##db.Field('cena_sabado','boolean', comment="sin cargo para los disertantes + organizadores"),
    ##db.Field('cena_obs','string', comment="indique si quiere invitar a la cena a familiares o amigos (cant. de reservas) -con cargo-"),
    format="%(last_name)s, %(first_name)s (%(id)s)",
    migrate=migrate, fake_migrate=fake_migrate)

# web2py planet model

db.define_table("feed",
    Field("name", label=T("name")),
    Field("author", label=T("author")),
    Field("email", requires=IS_EMAIL(), label=T("email")),
    Field("url", requires=IS_URL(), comment=T("RSS/Atom feed")),
    Field("link", requires=IS_URL(), comment=T("Blog href"), label=T("link")),
    Field("general", "boolean", comment=T("Many categories (needs filters)"), label=T("general")),
    migrate=migrate, fake_migrate=fake_migrate)

PLANET_FEEDS_MAX = 12

# end of web2py planet model

##db.auth_user.username.comment=T('(required)')
db.auth_user.first_name.comment=T('(required)')
db.auth_user.last_name.comment=T('(required)')
db.auth_user.email.comment=T('(required)')
db.auth_user.password.comment=not JANRAIN and T('(required)') or T('(optional)')
db.auth_user.resume.widget=lambda field,value: SQLFORM.widgets.text.widget(field,value,_cols="10",_rows="8")
db.auth_user.photo.comment=T('Your picture (100px)')
db.auth_user.dni.comment=T('(required if you need a certificate)')
#db.auth_user.certificate.comment=XML(A(str(T('El Costo de Certificado es $x.-')) + '[2]',_href='#footnote2'))
db.auth_user.t_shirt_size.requires=IS_IN_SET(T_SHIRT_SIZES,T_SHIRT_SIZES_LABELS)
db.auth_user.t_shirt_size.comment=XML(A(str(T('cost TBD')) + ' [2]',_href='#footnote2'))

db.auth_user.level.comment=T("")

db.auth_user.zip_code.comment=T('(for map)')

db.auth_user.company_name.comment=T('company, university')

db.auth_user.sponsors.comment=XML(A(str(T('Privacy policy')) + ' [1]',_href='#footnote1'))
#db.auth_user.include_in_delegate_listing.comment=T('If checked, your Name, Company and Location will be displayed publicly')
db.auth_user.include_in_delegate_listing.comment=XML(A(str(T('Privacy policy')) + ' [1]',_href='#footnote1'))
db.auth_user.resume.comment=T('Short Biografy and reference (required for speakers)')

db.auth_user.cv.comment=XML(A(str(T('Job Fair')) + ' [3]',_href='#footnote3'))

##db.auth_user.username.requires=[IS_LENGTH(512),IS_NOT_EMPTY(), IS_NOT_IN_DB(db,'auth_user.username')]
db.auth_user.first_name.requires=[IS_LENGTH(128),IS_NOT_EMPTY()]
db.auth_user.last_name.requires=[IS_LENGTH(128),IS_NOT_EMPTY()]

## Virtual fields:
import md5
# security hash can be used to validate user_id in public url (ie. schedule bookmark)
db.auth_user.security_hash = Field.Virtual(lambda row:  "created_by_ip" in row.auth_user and md5.new("%s%s" % (row.auth_user.created_by_ip, row.auth_user.created_on)).hexdigest())

auth=Auth(globals(),db)                      # authentication/authorization

db.auth_user.password.requires=[CRYPT()]
if not JANRAIN:
    db.auth_user.password.requires.append(IS_NOT_EMPTY())

auth.settings.table_user=db.auth_user
auth.settings.cas_domains = None        # disable CAS
auth.define_tables(username=False, migrate=migrate)
auth.settings.controller='user'
auth.settings.login_url=URL(r=request,c='user',f='login')
auth.settings.on_failed_authorization=URL(r=request,c='user',f='login')
auth.settings.logout_next=URL(r=request,c='default',f='index')
auth.settings.register_next=URL(r=request,c='default',f='index')
auth.settings.verify_email_next=URL(r=request,c='default',f='index')
auth.settings.profile_next=URL(r=request,c='user',f='profile')
auth.settings.retrieve_password_next=URL(r=request,c='user',f='login')
auth.settings.change_password_next=URL(r=request,c='default',f='index')
auth.settings.logged_url=URL(r=request,c='user',f='profile')
auth.settings.create_user_groups = False
auth.settings.actions_disabled = ['register', 'change_password','request_reset_password']
auth.settings.reset_password_requires_verification = True

if EMAIL_SERVER:
    mail=Mail()                                  # mailer
    mail.settings.server=EMAIL_SERVER
    mail.settings.sender=EMAIL_SENDER
    mail.settings.login=EMAIL_AUTH
    auth.settings.mailer=mail                    # for user email verification
    auth.settings.registration_requires_verification = EMAIL_VERIFICATION
    auth.messages.verify_email_subject = EMAIL_VERIFY_SUBJECT
    auth.messages.verify_email = EMAIL_VERIFY_BODY

##if RECAPTCHA_PUBLIC_KEY:
##    auth.settings.captcha=Recaptcha(request, RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY)
auth.define_tables(migrate=migrate, fake_migrate=fake_migrate)

db.auth_membership.user_id.represent=lambda v: "%(last_name)s, %(first_name)s (%(id)s)" % db.auth_user[v]

db.auth_user.email.requires=[IS_LENGTH(128),IS_EMAIL(),IS_NOT_IN_DB(db,'auth_user.email')]
db.auth_user.personal_home_page.requires=[IS_LENGTH(128),IS_NULL_OR(IS_URL())]
db.auth_user.company_home_page.requires=[IS_LENGTH(128),IS_NULL_OR(IS_URL())]
db.auth_user.country.requires=IS_IN_SET(COUNTRIES)
db.auth_user.created_by_ip.requires=\
    IS_NOT_IN_DB(db(db.auth_user.created_on>PAST),'auth_user.created_by_ip')
db.auth_user.registration_key.default=str(uuid.uuid4())

db.auth_user.reviewer.writable=db.auth_user.reviewer.readable=auth.has_membership('manager')
db.auth_user.speaker.writable=db.auth_user.speaker.readable=auth.has_membership('manager')

# Enable tutorial selection after proposal deadline
##db.auth_user.tutorials.writable = db.auth_user.tutorials.readable = TODAY_DATE>PROPOSALS_DEADLINE_DATE
##db.auth_user.tutorials.label = "Charlas Preferidas"

# Enable simplified registration (no password asked)
if SIMPLIFIED_REGISTRATION and TODAY_DATE>REVIEW_DEADLINE_DATE and request.controller=='user' and request.function=='register':
    db.auth_user.password.readable = False
    db.auth_user.password.writable = False
    ##db.auth_user.confirmed.default = False
else:
    db.auth_user.confirmed.default = False
    db.auth_user.password.comment = "(nueva para este sitio)"
    ##db.auth_user.confirmed.readable = True
    ##db.auth_user.confirmed.writable = True
    ##db.auth_user.address.requires = IS_NOT_EMPTY()

db.auth_user.confirmed.label = T("Confirm attendance")

# badge:
if ENABLE_BADGE:
    db.auth_user.badge_line1.readable = True
    db.auth_user.badge_line2.readable = True
    db.auth_user.badge_line1.writable = True
    db.auth_user.badge_line2.writable = True
    db.auth_user.sponsor_id.readable = True
    db.auth_user.sponsor_id.writable = True

    db.auth_user.badge_line1.comment = T("(i.e. position)")
    db.auth_user.badge_line2.comment = T("(ie. interests)")
    db.auth_user.sponsor_id.comment = T("(logo for badge)")
<commit_msg>fixed issue #6 and re-enabled navbar actions<commit_after>from gluon.tools import *
import uuid, datetime, re, os, time, stat
now=datetime.datetime.now()

migrate = DEV_TEST
fake_migrate = False

if SUSPEND_SERVICE:
    raise HTTP(503, "
Service is momentarily unavailable
")

if is_gae:
    db=GQLDB()
    session.connect(request,response,db=db)
else:
    db=DAL(DBURI, pool_size=20)

PAST=datetime.datetime.today()-datetime.timedelta(minutes=1)

#### cleanup sessions
##ps=os.path.join(request.folder,'sessions')
##try: [os.unlink(os.path.join(ps,f)) for f in os.listdir(ps) if os.stat(os.path.join(ps,f))[stat.ST_MTIME]<time.time()-3600]
##except: pass
#### end cleanup sessions

def wysiwyg(field,value):
    return DIV(field.name,TEXTAREA(_name=field.name, _cols="60", value=value,
                                   _id="wysiwyg",
                                   _style="width: 810px; height: 200px;"))

######################################
### PERSON
######################################

db.define_table('auth_user',
    ##Field('username', length=512, default='', label=T('Username')),
    db.Field('first_name',length=128,label=T('First Name')),
    db.Field('last_name',length=128,label=T('Last Name')),
    db.Field('email',length=128),
    db.Field('password','password',default='',label=T('Password'),readable=False),
    db.Field('level','text',label=T('Python knowledge level'),requires=IS_IN_SET(('principiante','intermedio','avanzado'))),
    db.Field('tutorials','list:string',label=T('Tutorials'),readable=False,writable=False),
    db.Field('dni','integer'),
    db.Field('certificate','boolean',default=False,label=T('I want a certificate of attendance'),readable=False,writable=False),
    db.Field('address',length=255,label=T('Mailing Address'),default=''),
    db.Field('city',label=T('City'),default=''),
    db.Field('state',label=T('State'),default='Buenos Aires'),
    db.Field('country',label=T('Country'),default='Argentina'),
    db.Field('zip_code',label=T('Zip/Postal Code'),default=''),
    db.Field('phone_number',label=T('Phone Number'),comment=T('(cellphone)')),
    db.Field('include_in_delegate_listing','boolean',default=True,label=T('Include in Delegates List')),
    db.Field('sponsors','boolean',default=True,label=T('Contacto con Auspiciantes'),readable=True,writable=True),
    db.Field('twitter_username',length=64,label=T('Twitter username'),default=''),
    db.Field('personal_home_page',length=128,label=T('Personal Home Page'),default=''),
    db.Field('company_name',label=T('Entity Name'),default=''),
    db.Field('company_home_page',length=128,label=T('Entity Home Page'),default=''),
    db.Field('badge_line1',label=T('Badge Line 1'),default='',readable=False,writable=False),
    db.Field('badge_line2',label=T('Badge Line 2'),default='',readable=False,writable=False),
    db.Field('sponsor_id',"reference sponsor",label=T('Sponsor'),readable=False,writable=False),
    db.Field('t_shirt_size',label=T('T-shirt Size')),
    db.Field('attendee_type',label=T('Registration Type'),default=ATTENDEE_TYPES[0][0],readable=False,writable=False),
    db.Field('discount_coupon',length=64,label=T('Discount Coupon'), readable=False,writable=False),
    db.Field('donation','double',default=0.0,label=T('Donation to PyAr'),readable=False,writable=False),
    db.Field('amount_billed','double',default=0.0,readable=False,writable=False),
    db.Field('amount_added','double',default=0.0,readable=False,writable=False),
    db.Field('amount_subtracted','double',default=0.0,readable=False,writable=False),
    db.Field('amount_paid','double',default=0.0,readable=False,writable=False),
    db.Field('amount_due','double',default=0.0,readable=False,writable=False),
    db.Field('resume','text',label=T('Resume (Bio)'),readable=True,writable=True),
    db.Field('photo','upload',label=T('Photo'), readable=True,writable=True),
    db.Field('cv','upload',label=T('CV'),readable=True,writable=True),
    db.Field('speaker','boolean',default=False,readable=False,writable=False),
    db.Field('session_chair','boolean',default=False,readable=False,writable=False),
    db.Field('manager','boolean',default=False,readable=False,writable=False),
    db.Field('reviewer','boolean',default=True,readable=False,writable=False),
    db.Field('latitude','double',default=0.0,readable=False,writable=False),
    db.Field('longitude','double',default=0.0,readable=False,writable=False),
    db.Field('confirmed','boolean',default=False,writable=False,readable=False),
    db.Field('registration_key',length=64,default='',readable=False,writable=False),
    db.Field('reset_password_key',length=64,default='',readable=False,writable=False),
    Field('reset_password_key', length=512, writable=False, readable=False, default=''),
    Field('registration_id', length=512, writable=False, readable=False, default=''),
    db.Field('created_by_ip',readable=False,writable=False,default=request.client),
    db.Field('created_on','datetime',readable=False,writable=False,default=request.now),
    ##db.Field('cena_viernes','boolean', comment="-con cargo-"),
    ##db.Field('cena_sabado','boolean', comment="sin cargo para los disertantes + organizadores"),
    ##db.Field('cena_obs','string', comment="indique si quiere invitar a la cena a familiares o amigos (cant. de reservas) -con cargo-"),
    format="%(last_name)s, %(first_name)s (%(id)s)",
    migrate=migrate, fake_migrate=fake_migrate)

# web2py planet model

db.define_table("feed",
    Field("name", label=T("name")),
    Field("author", label=T("author")),
    Field("email", requires=IS_EMAIL(), label=T("email")),
    Field("url", requires=IS_URL(), comment=T("RSS/Atom feed")),
    Field("link", requires=IS_URL(), comment=T("Blog href"), label=T("link")),
    Field("general", "boolean", comment=T("Many categories (needs filters)"), label=T("general")),
    migrate=migrate, fake_migrate=fake_migrate)

PLANET_FEEDS_MAX = 12

# end of web2py planet model

##db.auth_user.username.comment=T('(required)')
db.auth_user.first_name.comment=T('(required)')
db.auth_user.last_name.comment=T('(required)')
db.auth_user.email.comment=T('(required)')
db.auth_user.password.comment=not JANRAIN and T('(required)') or T('(optional)')
db.auth_user.resume.widget=lambda field,value: SQLFORM.widgets.text.widget(field,value,_cols="10",_rows="8")
db.auth_user.photo.comment=T('Your picture (100px)')
db.auth_user.dni.comment=T('(required if you need a certificate)')
#db.auth_user.certificate.comment=XML(A(str(T('El Costo de Certificado es $x.-')) + '[2]',_href='#footnote2'))
db.auth_user.t_shirt_size.requires=IS_IN_SET(T_SHIRT_SIZES,T_SHIRT_SIZES_LABELS)
db.auth_user.t_shirt_size.comment=XML(A(str(T('cost TBD')) + ' [2]',_href='#footnote2'))

db.auth_user.level.comment=T("")

db.auth_user.zip_code.comment=T('(for map)')

db.auth_user.company_name.comment=T('company, university')

db.auth_user.sponsors.comment=XML(A(str(T('Privacy policy')) + ' [1]',_href='#footnote1'))
#db.auth_user.include_in_delegate_listing.comment=T('If checked, your Name, Company and Location will be displayed publicly')
db.auth_user.include_in_delegate_listing.comment=XML(A(str(T('Privacy policy')) + ' [1]',_href='#footnote1'))
db.auth_user.resume.comment=T('Short Biografy and reference (required for speakers)')

db.auth_user.cv.comment=XML(A(str(T('Job Fair')) + ' [3]',_href='#footnote3'))

##db.auth_user.username.requires=[IS_LENGTH(512),IS_NOT_EMPTY(), IS_NOT_IN_DB(db,'auth_user.username')]
db.auth_user.first_name.requires=[IS_LENGTH(128),IS_NOT_EMPTY()]
db.auth_user.last_name.requires=[IS_LENGTH(128),IS_NOT_EMPTY()]

## Virtual fields:
import md5
# security hash can be used to validate user_id in public url (ie. schedule bookmark)
db.auth_user.security_hash = Field.Virtual(lambda row:  "created_by_ip" in row.auth_user and md5.new("%s%s" % (row.auth_user.created_by_ip, row.auth_user.created_on)).hexdigest())

auth=Auth(globals(),db)                      # authentication/authorization

db.auth_user.password.requires=[CRYPT()]
if not JANRAIN:
    db.auth_user.password.requires.append(IS_NOT_EMPTY())

auth.settings.table_user=db.auth_user
auth.settings.cas_domains = None        # disable CAS
auth.define_tables(username=False, migrate=migrate)
auth.settings.controller='user'
auth.settings.login_url=URL(r=request,c='user',f='login')
#auth.settings.on_failed_authorization=URL(r=request,c='user',f='login')
auth.settings.logout_next=URL(r=request,c='default',f='index')
auth.settings.register_next=URL(r=request,c='default',f='index')
auth.settings.verify_email_next=URL(r=request,c='default',f='index')
auth.settings.profile_next=URL(r=request,c='user',f='profile')
auth.settings.retrieve_password_next=URL(r=request,c='user',f='login')
auth.settings.change_password_next=URL(r=request,c='default',f='index')
auth.settings.logged_url=URL(r=request,c='user',f='profile')
auth.settings.create_user_groups = False
#sauth.settings.actions_disabled = ['register', 'change_password','request_reset_password']
auth.settings.reset_password_requires_verification = True

if EMAIL_SERVER:
    mail=Mail()                                  # mailer
    mail.settings.server=EMAIL_SERVER
    mail.settings.sender=EMAIL_SENDER
    mail.settings.login=EMAIL_AUTH
    auth.settings.mailer=mail                    # for user email verification
    auth.settings.registration_requires_verification = EMAIL_VERIFICATION
    auth.messages.verify_email_subject = EMAIL_VERIFY_SUBJECT
    auth.messages.verify_email = EMAIL_VERIFY_BODY

##if RECAPTCHA_PUBLIC_KEY:
##    auth.settings.captcha=Recaptcha(request, RECAPTCHA_PUBLIC_KEY, RECAPTCHA_PRIVATE_KEY)
auth.define_tables(migrate=migrate, fake_migrate=fake_migrate)

db.auth_membership.user_id.represent=lambda v: "%(last_name)s, %(first_name)s (%(id)s)" % db.auth_user[v]

db.auth_user.email.requires=[IS_LENGTH(128),IS_EMAIL(),IS_NOT_IN_DB(db,'auth_user.email')]
db.auth_user.personal_home_page.requires=[IS_LENGTH(128),IS_NULL_OR(IS_URL())]
db.auth_user.company_home_page.requires=[IS_LENGTH(128),IS_NULL_OR(IS_URL())]
db.auth_user.country.requires=IS_IN_SET(COUNTRIES)
db.auth_user.created_by_ip.requires=\
    IS_NOT_IN_DB(db(db.auth_user.created_on>PAST),'auth_user.created_by_ip')
db.auth_user.registration_key.default=str(uuid.uuid4())

db.auth_user.reviewer.writable=db.auth_user.reviewer.readable=auth.has_membership('manager')
db.auth_user.speaker.writable=db.auth_user.speaker.readable=auth.has_membership('manager')

# Enable tutorial selection after proposal deadline
##db.auth_user.tutorials.writable = db.auth_user.tutorials.readable = TODAY_DATE>PROPOSALS_DEADLINE_DATE
##db.auth_user.tutorials.label = "Charlas Preferidas"

# Enable simplified registration (no password asked)
if SIMPLIFIED_REGISTRATION and TODAY_DATE>REVIEW_DEADLINE_DATE and request.controller=='user' and request.function=='register':
    db.auth_user.password.readable = False
    db.auth_user.password.writable = False
    ##db.auth_user.confirmed.default = False
else:
    db.auth_user.confirmed.default = False
    db.auth_user.password.comment = "(nueva para este sitio)"
    ##db.auth_user.confirmed.readable = True
    ##db.auth_user.confirmed.writable = True
    ##db.auth_user.address.requires = IS_NOT_EMPTY()

db.auth_user.confirmed.label = T("Confirm attendance")

# badge:
if ENABLE_BADGE:
    db.auth_user.badge_line1.readable = True
    db.auth_user.badge_line2.readable = True
    db.auth_user.badge_line1.writable = True
    db.auth_user.badge_line2.writable = True
    db.auth_user.sponsor_id.readable = True
    db.auth_user.sponsor_id.writable = True

    db.auth_user.badge_line1.comment = T("(i.e. position)")
    db.auth_user.badge_line2.comment = T("(ie. interests)")
    db.auth_user.sponsor_id.comment = T("(logo for badge)")
<|endoftext|>


<commit_before># Swift Message & Alert System
#
# Opens and accepts messages over a socket
# and from stdin for irc channel alerts

import asyncio
import sqlite3
import socket
import sys
import re
from hangups import ChatMessageSegment
from Bank.Hangouts import name_to_nick
from Bank import g

def is_an_alert(message):
    result = re.match("^\[(\S+)\] \((\S+)!(\S+)@([0-9a-zA-Z.:-]+)\) (.*)", message)
    if result:
        return result.groups()

def timeout(future, sock, address):
    if future.cancel():
        sock.close()
        print('Swift: client connection {0} timed out.'.format(address))

class Subscribers(list):

    def __init__(self, *args):
        list.__init__(self, *args)

    @property
    def first_name(self):
        return [(name.split()[0], id) for name, id in self]

    @property
    def nick(self):
        return [((name.split()[0][0] + name.split()[1]).lower(), id)
                for name, id in self]

class Swift:

    def __init__(self, client, conv_list):
        self.loop = asyncio.get_event_loop()
        self.host = socket.gethostname()
        self.port = g.config['notifications']['port']
        self.h_client = client
        self.h_conv_list = conv_list
        self.db = g.db
        self.cursor = g.cursor

        # Load subscriber list on initialization
        self.cursor.execute("SELECT name, conv_id FROM subscribers")
        self.subscribers = Subscribers(self.cursor.fetchall())

        asyncio.async(self.open_swift())
        self.loop.add_reader(sys.stdin, self.read_stdin)

    @asyncio.coroutine
    def open_swift(self):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setblocking(0)
            sock.bind((self.host, self.port))
            sock.listen(5)
            print('Swift bound on port {0}'.format(self.port))
            yield from self.accept_swift(sock)
        except Exception as e:
            print('Swift: ', e)
            raise e
        finally:
            sock.close()

    @asyncio.coroutine
    def accept_swift(self, sock):
        try:
            futures = {}
            timers = {}
            while True:
                (client, address) = yield from self.loop.sock_accept(sock)
                address = ":".join(str(part) for part in address)
                #futures[address] = asyncio.async(self.read_client(client, address))
                #timers[address] = asyncio.async(asyncio.sleep(10))
                #timers[address].add_done_callback(lambda x: timeout(futures[address], client, address))
                future = asyncio.async(self.read_client(client, address))
                print('Swift client connected from: ', address)
            return future
        except Exception as e:
            print('Swift: ', e)
            raise e

    @asyncio.coroutine
    def read_client(self, client, address):
        try:
            while True:
                data = yield from self.loop.sock_recv(client, 2048)
                if data:
                    data = data.decode().strip()
                    for message in data.splitlines():
                        print("Swift received from {0}: {1}".format(address, message))
                        alert = is_an_alert(message)
                        if alert:
                            (recipient, nick, uname, domain, msg) = alert
                            for subscriber, conv_id in self.subscribers.nick:
                                if recipient == subscriber:
                                    yield from self.notify('Private message from {0}:\n"{1}"'
                                        .format(nick, msg), conv_id)
                                    print('Swift forwarded message to {0}.'.format(recipient))
                else:
                    client.close()
                    print('Swift: remote client {0} closed connection.'.format(address))
                    break
        except Exception as e:
            print('Swift: ', e)
            raise e

    @asyncio.coroutine
    def notify(self, message, conv_id):
        segment = ChatMessageSegment.from_str(message)
        conv = self.h_conv_list.get(conv_id)
        asyncio.async(conv.send_message(segment, image_file=None))

    # Listen on stdin for irc alerts
    def read_stdin(self):
        message = sys.stdin.readline()
        message = message.strip()
        print("Swift received from stdin: ", message)
        sender, channel, *parts = message.split()
        message = " ".join(parts).strip(":")

        for subscriber, conv_id in self.subscribers:
            re_mentions = re.compile('{0}|{1}'.format(subscriber.split()[0],
                                     name_to_nick(subscriber)), re.IGNORECASE)
            if re.search(re_mentions, message) and sender != name_to_nick(subscriber):
                highlighted = re.sub(re_mentions, lambda x: '' + x.group(0) + '', message)
                asyncio.async(self.notify('{0} mentioned you on IRC in {1}:\n"{2}"'
                    .format(sender, channel, highlighted), conv_id))
                print("Forwarded IRC mention to {0}".format(subscriber))
<commit_msg>Prevent Swift from blowing up if the IRC process dies<commit_after># Swift Message & Alert System
#
# Opens and accepts messages over a socket
# and from stdin for irc channel alerts

import asyncio
import sqlite3
import socket
import sys
import re
from hangups import ChatMessageSegment
from Bank.Hangouts import name_to_nick
from Bank import g

def is_an_alert(message):
    result = re.match("^\[(\S+)\] \((\S+)!(\S+)@([0-9a-zA-Z.:-]+)\) (.*)", message)
    if result:
        return result.groups()

def timeout(future, sock, address):
    if future.cancel():
        sock.close()
        print('Swift: client connection {0} timed out.'.format(address))

class Subscribers(list):

    def __init__(self, *args):
        list.__init__(self, *args)

    @property
    def first_name(self):
        return [(name.split()[0], id) for name, id in self]

    @property
    def nick(self):
        return [((name.split()[0][0] + name.split()[1]).lower(), id)
                for name, id in self]

class Swift:

    def __init__(self, client, conv_list):
        self.loop = asyncio.get_event_loop()
        self.host = socket.gethostname()
        self.port = g.config['notifications']['port']
        self.h_client = client
        self.h_conv_list = conv_list
        self.db = g.db
        self.cursor = g.cursor

        # Load subscriber list on initialization
        self.cursor.execute("SELECT name, conv_id FROM subscribers")
        self.subscribers = Subscribers(self.cursor.fetchall())

        asyncio.async(self.open_swift())
        self.loop.add_reader(sys.stdin, self.read_stdin)

    @asyncio.coroutine
    def open_swift(self):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setblocking(0)
            sock.bind((self.host, self.port))
            sock.listen(5)
            print('Swift bound on port {0}'.format(self.port))
            yield from self.accept_swift(sock)
        except Exception as e:
            print('Swift: ', e)
            raise e
        finally:
            sock.close()

    @asyncio.coroutine
    def accept_swift(self, sock):
        try:
            futures = {}
            timers = {}
            while True:
                (client, address) = yield from self.loop.sock_accept(sock)
                address = ":".join(str(part) for part in address)
                #futures[address] = asyncio.async(self.read_client(client, address))
                #timers[address] = asyncio.async(asyncio.sleep(10))
                #timers[address].add_done_callback(lambda x: timeout(futures[address], client, address))
                future = asyncio.async(self.read_client(client, address))
                print('Swift client connected from: ', address)
            return future
        except Exception as e:
            print('Swift: ', e)
            raise e

    @asyncio.coroutine
    def read_client(self, client, address):
        try:
            while True:
                data = yield from self.loop.sock_recv(client, 2048)
                if data:
                    data = data.decode().strip()
                    for message in data.splitlines():
                        print("Swift received from {0}: {1}".format(address, message))
                        alert = is_an_alert(message)
                        if alert:
                            (recipient, nick, uname, domain, msg) = alert
                            for subscriber, conv_id in self.subscribers.nick:
                                if recipient == subscriber:
                                    yield from self.notify('Private message from {0}:\n"{1}"'
                                        .format(nick, msg), conv_id)
                                    print('Swift forwarded message to {0}.'.format(recipient))
                else:
                    client.close()
                    print('Swift: remote client {0} closed connection.'.format(address))
                    break
        except Exception as e:
            print('Swift: ', e)
            raise e

    @asyncio.coroutine
    def notify(self, message, conv_id):
        segment = ChatMessageSegment.from_str(message)
        conv = self.h_conv_list.get(conv_id)
        asyncio.async(conv.send_message(segment, image_file=None))

    # Listen on stdin for irc alerts
    def read_stdin(self):
        message = sys.stdin.readline()
        if len(message) == 0:
            print('IRC is kill. Closing read from stdin.')
            self.loop.remove_reader(sys.stdin)
            return
        message = message.strip()
        print("Swift received from stdin: ", message)
        sender, channel, *parts = message.split()
        message = " ".join(parts).strip(":")

        for subscriber, conv_id in self.subscribers:
            re_mentions = re.compile('{0}|{1}'.format(subscriber.split()[0],
                                     name_to_nick(subscriber)), re.IGNORECASE)
            if re.search(re_mentions, message) and sender != name_to_nick(subscriber):
                highlighted = re.sub(re_mentions, lambda x: '' + x.group(0) + '', message)
                asyncio.async(self.notify('{0} mentioned you on IRC in {1}:\n"{2}"'
                    .format(sender, channel, highlighted), conv_id))
                print("Forwarded IRC mention to {0}".format(subscriber))
<|endoftext|>