<commit_before>from django.contrib import admin
from django.contrib.contenttypes import generic
from django import forms

from ostinato.models import ContentItem

## Admin Actions
def action_allow_comments(modeladmin, request, queryset):
    queryset.update(allow_comments=True)
action_allow_comments.short_description = "Comments - Allow comments on items"

def action_disallow_comments(modeladmin, request, queryset):
    queryset.update(allow_comments=False)
action_disallow_comments.short_description = "Comments - Disallow comments on items"

def action_show_in_nav(modeladmin, request, queryset):
    queryset.update(show_in_nav=True)
action_show_in_nav.short_description = "Navigation - Show Items in Nav"

def action_dont_show_in_nav(modeladmin, request, queryset):
    queryset.update(show_in_nav=False)
action_dont_show_in_nav.short_description = "Navigation - Dont show in Nav"

## Inline Classes
class ContentItemInline(generic.GenericStackedInline):
    model = ContentItem
    extra = 0

## ModelAdmin Classes
def statemachine_form(for_model=None):
    """
    Factory function to create a special case form that will render
    an extra _sm_actions choice field, showing only the available actions
    for the current state.
    """
    class _StateMachineBaseModelForm(forms.ModelForm):
        """
        """
        _sm_action = forms.ChoiceField(choices=[], label="Take Action",
                                       required=False)

        class Meta:
            model = for_model

        def __init__(self, *args, **kwargs):
            super(_StateMachineBaseModelForm, self).__init__(*args, **kwargs)
            actions = (('', '--- No Action ---'),)
            for action in self.instance.sm_state_actions():
                actions += ((action, action),)
            self.fields['_sm_action'] = forms.ChoiceField(
                choices=actions, label="Take Action", required=False)

        def save(self, *args, **kwargs):
            """
            Override the save method so that we can take any required actions
            and move to the next state.
            """
            action = self.cleaned_data['_sm_action']
            if action: self.instance.sm_take_action(action)
            return super(_StateMachineBaseModelForm, self).save(*args, **kwargs)

    if for_model: return _StateMachineBaseModelForm
    else: return None

class ContentItemAdmin(admin.ModelAdmin):
    form = statemachine_form(for_model=ContentItem)

    list_display = ['title', 'short_title', 'parent', 'order', 'sm_state_admin',
                    'allow_comments', 'show_in_nav',
                    'created_date', 'modified_date', 'publish_date']
    list_filter = ['allow_comments', 'show_in_nav', 'publish_date']
    date_hierarchy = 'created_date'
    search_fields = ['title', 'short_title', 'description', 'location']
    actions = [action_show_in_nav, action_dont_show_in_nav,
               action_allow_comments, action_disallow_comments]
    fieldsets = (
        (None, {
            'fields': ('title', 'short_title', 'description', 'order'),
        }),
        ('Content Properties', {
            'fields': ('parent', 'allow_comments', 'show_in_nav',
                       'tags', 'location')
        }),
        ('Authoring and Publication', {
            'fields': ('publish_date', 'authors', 'contributors',
                       '_sm_action')
        }),
    )

## Admin registrations
admin.site.register(ContentItem, ContentItemAdmin)
<commit_msg>Show the current state in the admin dropdown, instead of '--- No Action ---'<commit_after>from django.contrib import admin
from django.contrib.contenttypes import generic
from django import forms

from ostinato.models import ContentItem

## Admin Actions
def action_allow_comments(modeladmin, request, queryset):
    queryset.update(allow_comments=True)
action_allow_comments.short_description = "Comments - Allow comments on items"

def action_disallow_comments(modeladmin, request, queryset):
    queryset.update(allow_comments=False)
action_disallow_comments.short_description = "Comments - Disallow comments on items"

def action_show_in_nav(modeladmin, request, queryset):
    queryset.update(show_in_nav=True)
action_show_in_nav.short_description = "Navigation - Show Items in Nav"

def action_dont_show_in_nav(modeladmin, request, queryset):
    queryset.update(show_in_nav=False)
action_dont_show_in_nav.short_description = "Navigation - Dont show in Nav"

## Inline Classes
class ContentItemInline(generic.GenericStackedInline):
    model = ContentItem
    extra = 0

## ModelAdmin Classes
def statemachine_form(for_model=None):
    """
    Factory function to create a special case form that will render
    an extra _sm_actions choice field, showing only the available actions
    for the current state.
    """
    class _StateMachineBaseModelForm(forms.ModelForm):
        _sm_action = forms.ChoiceField(choices=[], label="Take Action",
                                       required=False)

        class Meta:
            model = for_model

        def __init__(self, *args, **kwargs):
            super(_StateMachineBaseModelForm, self).__init__(*args, **kwargs)
            actions = (('', '-- %s --' % self.instance.sm_state),)
            for action in self.instance.sm_state_actions():
                actions += ((action, action),)
            self.fields['_sm_action'] = forms.ChoiceField(
                choices=actions, label="Take Action", required=False)

        def save(self, *args, **kwargs):
            """
            Override the save method so that we can take any required actions
            and move to the next state.
            """
            action = self.cleaned_data['_sm_action']
            if action: self.instance.sm_take_action(action)
            return super(_StateMachineBaseModelForm, self).save(*args, **kwargs)

    if for_model: return _StateMachineBaseModelForm
    else: return None

class ContentItemAdmin(admin.ModelAdmin):
    form = statemachine_form(for_model=ContentItem)

    list_display = ['title', 'short_title', 'parent', 'order', 'sm_state_admin',
                    'allow_comments', 'show_in_nav',
                    'created_date', 'modified_date', 'publish_date']
    list_filter = ['allow_comments', 'show_in_nav', 'publish_date']
    date_hierarchy = 'created_date'
    search_fields = ['title', 'short_title', 'description', 'location']
    actions = [action_show_in_nav, action_dont_show_in_nav,
               action_allow_comments, action_disallow_comments]
    fieldsets = (
        (None, {
            'fields': ('title', 'short_title', 'description', 'order'),
        }),
        ('Content Properties', {
            'fields': ('parent', 'allow_comments', 'show_in_nav',
                       'tags', 'location')
        }),
        ('Authoring and Publication', {
            'fields': ('publish_date', 'authors', 'contributors',
                       '_sm_action')
        }),
    )

## Admin registrations
admin.site.register(ContentItem, ContentItemAdmin)
<|endoftext|>


<commit_before>from __future__ import unicode_literals
import dbhash
import random
from util.benchmarking import Timer

def queryWithAddedWhere(query, phrase, valueList, appendedValue=None):
    continuing_where = False
    if query:
        continuing_where = """WHERE""" in query
    if continuing_where:
        query = query + """ AND"""
    else:
        query = query + """ WHERE"""
    valueList.append(appendedValue)
    return query + phrase

def queryWithAddedWhereAppend(query, phrase, valueList, appendedList=None):
    continuing_where = False
    if query:
        continuing_where = """WHERE""" in query
    if continuing_where:
        query = query + """ AND"""
    else:
        query = query + """ WHERE"""
    for v in appendedList:
        valueList.append(v)
    return query + phrase

def randomIndexedPage(dbconn):
    cur = dbconn.connection.cursor()
    offset = random.randint(0, 4961336)
    query = """SELECT id FROM page_categories LIMIT 1 OFFSET %s;"""
    values = (offset,)
    cur.execute(query, values)
    res = cur.fetchall()
    cur.close()
    return res[0][0]

def pageIDForPageTitle(dbconn, name, doCache=False):
    if name in dbconn.name_cache:
        return dbconn.name_cache[name]

    cursor = dbconn.connection.cursor()
    query = """SELECT page_id FROM page_names WHERE name = %s"""
    values = (name,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    cursor.close()
    if len(res) is not 0:
        pageID = res[0][0]
        if doCache:
            dbconn.name_cache[name] = pageID
        return pageID
    else:
        return None

def pageTitleForPageID(dbconn, pageID):
    cursor = dbconn.connection.cursor()
    query = """SELECT name FROM page_names WHERE page_id = %s"""
    values = (pageID,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res) is not 0:
        name = res[0][0]
    else:
        name = None
    cursor.close()
    return name.decode('utf-8')

def followRedirectForPageID(dbconn, pageID):
    cursor = dbconn.connection.cursor()
    query = """SELECT to_id FROM redirects WHERE from_id = (%s)"""
    values = (pageID,)
    cursor.execute(query, values)
    res = cursor.fetchall();
    cursor.close()

    if len(res) is not 0:
        pageID = res[0][0]
    return pageID

def pagesLinkedFromPageID(dbconn, pageID):
    cursor = dbconn.connection.cursor()
    query = """SELECT to_id FROM page_links WHERE from_id = %s"""
    values = (pageID,)
    cursor.execute(query, values)
    links = cursor.fetchall()
    cursor.close()
    links = [l[0] for l in links]
    return links

def categoryForPageID(dbconn, pageID, order='minor'):
    cursor = dbconn.connection.cursor()
    query = """SELECT minor_category, major_category FROM page_categories WHERE id=%s;"""
    values = (pageID,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res)==0:
        return None
    elif order=='minor':
        return res[0][0]
    elif order=='major':
        return res[0][1]
    else:
        raise ValueError("order argument must be one of ['minor', 'major']")

def textForLineID(dbconn, lineID):
    cursor = dbconn.connection.cursor()
    query = """SELECT line FROM iambic_lines WHERE id=%s;"""
    values = (lineID,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res)==0:
        return None
    return res[0][0]

def rhymeCountForRhyme(dbconn, word, rhyme):
    cursor = dbconn.connection.cursor()
    query = """SELECT count FROM rhyme_counts WHERE word=%s"""
    values = (word,)
    cursor.execute(query, values)
    cnt = cursor.fetchall()
    if len(cnt)>0:
        cursor.close()
        return cnt[0][0]
    query = """SELECT COUNT(*) FROM iambic_lines WHERE word != %s AND rhyme_part = %s"""
    values = (word, rhyme)
    cursor.execute(query, values)
    cnt = cursor.fetchall()
    cursor.close()
    return cnt[0][0]

pos_prev_constraints = ["pos_0", "pos_1", "pos_m1", "pos_m2"]
pos_next_constraints = ["pos_len_m2", "pos_len_m1", "pos_len", "pos_len_p1"]
hashed_leading_names = ["leading_2gram", "leading_3gram", "leading_4gram"]
hashed_lagging_names = ["lagging_2gram", "lagging_3gram", "lagging_4gram"]
subquery_constraints = ['starts', 'ends', 'rhyme_part']
pos_config_sets = [{"constraints":pos_prev_constraints, "names":hashed_leading_names},
                    {"constraints":pos_next_constraints, "names":hashed_lagging_names}]

## line must be a dictionary containing the keys for either a lagging 4gram or a leading 4gram
def posCountsForLine(dbconn, line, position='leading'):
    cursor = dbconn.connection.cursor()

    if position=='leading':
        dict_to_hash = {k:line[k] for k in line if k in pos_next_constraints}
        query = """SELECT COUNT(*) FROM leading_pos_counts WHERE leading_4gram=%s"""
        values = (dbhash.columnsDictToSHA(dict_to_hash), )
    elif position=='lagging':
        dict_to_hash = {k:line[k] for k in line if k in pos_prev_constraints}
        query = """SELECT COUNT(*) FROM lagging_pos_counts WHERE lagging_4gram=%s"""
        values = (dbhash.columnsDictToSHA(dict_to_hash), )
    else:
        raise ValueError("position must be one of ['leading', 'lagging']")

    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res) > 0:
        return res[0][0]
    return 0

def optimizedConstraints(constraints):
    mod_constraints = {k:constraints[k] for k in constraints}

    for pos_config in pos_config_sets:

        pos_constraints = pos_config["constraints"]
        column_names = pos_config["names"]

        # Get the columns that we are going to combine into a precomputed hash for those columns
        pos_columns = filter(lambda x: x in pos_constraints, constraints.keys())

        # Check whether or not there are at least two parts of speech in question
        if len(pos_columns) >= 2:

            # Get a dictionary for which to compute a hash
            dict_to_hash = {col:constraints[col] for col in pos_columns}
            dict_as_sha = dbhash.columnsDictToSHA(dict_to_hash)

            # modify options to include the new keys
            hashed_column_name = column_names[len(pos_columns) - 2]
            for p in pos_columns:
                mod_constraints.pop(p)
            mod_constraints[hashed_column_name] = dict_as_sha

    return mod_constraints

def queryWithAddedConstraint(_query, key, valueList, constraints):
    if key == "excluded_word":
        _query = queryWithAddedWhere(_query, """ iambic_lines.word != %s""", valueList, constraints[key])
    elif key == "excluded_lines":
        excludedLines = constraints[key]
        if len(excludedLines) is 1:
            _query = queryWithAddedWhere(_query, """ iambic_lines.id != %s""", valueList, excludedLines[0])
        elif len(excludedLines) is not 0:
            format_strings = ','.join(['%s'] * len(excludedLines))
            _query = queryWithAddedWhereAppend(_query, """ iambic_lines.id NOT IN (%s)""" % format_strings, valueList, list(excludedLines))
    else:
        if key in hashed_leading_names + hashed_lagging_names:
            _query = queryWithAddedWhere(_query, """ pos_hashes.""" + key + """ = %s""", valueList, constraints[key])
        else:
            _query = queryWithAddedWhere(_query, """ iambic_lines.""" + key + """ = %s""", valueList, constraints[key])
    return _query

def createViewForConstraints(dbconn, constraints=None, temp_view_name='tv'):
    if constraints is None:
        return

    cur = dbconn.connection.cursor()
    query = """CREATE VIEW {} AS SELECT * FROM iambic_lines """.format(temp_view_name)
    valueList = [];
    for key in constraints:
        if key == "excluded_word":
            query = queryWithAddedWhere(query, """ word != %s""", valueList, constraints[key])
        elif key == "excluded_lines":
            excludedLines = constraints[key]
            if len(excludedLines) is 1:
                query = queryWithAddedWhere(query, """ id != %s""", valueList, excludedLines[0])
            elif len(excludedLines) is not 0:
                format_strings = ','.join(['%s'] * len(excludedLines))
                query = queryWithAddedWhereAppend(query, """ id NOT IN (%s)""" % format_strings, valueList, list(excludedLines))
        else:
            query = queryWithAddedWhere(query, """ """ + key + """ = %s""", valueList, constraints[key])

    values = tuple(valueList)
    cur.execute(query, values)

def clearView(dbconn, temp_view_name='tv'):
    cur = dbconn.connection.cursor()
    cur.execute("DROP VIEW IF EXISTS {};".format(temp_view_name))

def searchForLines(dbconn, group=None, constraints=None, options=None):

    cur = dbconn.connection.cursor(dictionary=True)
    num = options.get('num', 1)
    brandom = options.get('random', False)
    optimized = options.get('optimized', False)
    view_constraints = options.get('view_constraints', [])
    table_name = options.get('view_name', 'iambic_lines')

    query = """SELECT {0}.id, {0}.page_id, word, rhyme_part, starts, ends, pos_m2, pos_m1, pos_0, pos_1, pos_len_m2, pos_len_m1, pos_len, pos_len_p1, word_len_m1, word_len FROM {0} """.format(table_name)
    valueList = [];
    continuing_where = False
    use_subquery = options.get('subquery', False)
    subquery = """SELECT id FROM {} """.format(table_name)
    if optimized:
        constraints = optimizedConstraints(constraints)
        if any([p in constraints for p in hashed_leading_names+hashed_lagging_names]):
            query = query + """ join pos_hashes on pos_hashes.line_id = {}.id""".format(table_name)
    if group:
        if 'page_major_category' in group or 'page_minor_category' in group:
            query = query + """ join page_categories on page_categories.id = {}.page_id""".format(table_name)
        if 'line_major_category' in group or 'line_minor_category' in group:
            query = query + """ join line_categories on line_categories.id = {}.id""".format(table_name)

    if use_subquery:
        ### QUERY ###
        for key in constraints:
            if key not in subquery_constraints:
                query = queryWithAddedConstraint(query, key, valueList, constraints)

        if group:
            if 'pageIDs' in group:
                pages = group['pageIDs']
                if len(pages) is 1:
                    query = queryWithAddedWhere(query, """ page_id = %s""", valueList, pages[0])
                else:
                    format_strings = ','.join(['%s'] * len(pages))
                    query = queryWithAddedWhereAppend(query, """ page_id IN (%s)""" % format_strings, valueList, list(pages))
            if 'page_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['page_minor_category'])
            if 'page_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['page_major_category'])
            if 'line_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['line_minor_category'])
            if 'line_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['line_major_category'])
        ### QUERY ###

        ### SUBQUERY ###
        subquery = """SELECT id FROM iambic_lines """
        for key in constraints:
            if key in subquery_constraints:
                subquery = queryWithAddedConstraint(subquery, key, valueList, constraints)
        ### SUBQUERY ###

    else:
        ### Building WHERE clause without subquery ###
        for key in [k for k in constraints if k not in view_constraints]:
            if key == "excluded_word":
                query = queryWithAddedWhere(query, """ word != %s""", valueList, constraints[key])
            elif key == "excluded_lines":
                excludedLines = constraints[key]
                if len(excludedLines) is 1:
                    query = queryWithAddedWhere(query, """ id != %s""", valueList, excludedLines[0])
                elif len(excludedLines) is not 0:
                    format_strings = ','.join(['%s'] * len(excludedLines))
                    query = queryWithAddedWhereAppend(query, """ id NOT IN (%s)""" % format_strings, valueList, list(excludedLines))
            else:
                if key in hashed_leading_names + hashed_lagging_names:
                    query = queryWithAddedWhere(query, """ pos_hashes.""" + key + """ = %s""", valueList, constraints[key])
                else:
                    query = queryWithAddedWhere(query, """ {}.""".format(table_name) + key + """ = %s""", valueList, constraints[key])

        if group:
            if 'pageIDs' in group:
                pages = group['pageIDs']
                if len(pages) is 1:
                    query = queryWithAddedWhere(query, """ page_id = %s""", valueList, pages[0])
                else:
                    format_strings = ','.join(['%s'] * len(pages))
                    query = queryWithAddedWhereAppend(query, """ page_id IN (%s)""" % format_strings, valueList, list(pages))
            if 'page_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['page_minor_category'])
            if 'page_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['page_major_category'])
            if 'line_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['line_minor_category'])
            if 'line_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['line_major_category'])
        ### /WHERE ###

    if use_subquery:
        if not """WHERE""" in query:
            query = query + """ WHERE {}.id IN (""".format(table_name) + subquery + """)"""
        else:
            query = query + """ AND {}.id IN (""".format(table_name) + subquery + """)"""

    if brandom:
        query = query + """ ORDER BY RAND()"""

    query = query + """ LIMIT %s;"""
    valueList.append(num)
    values = tuple(valueList)
    try:
        cur.execute(query, values)
    except Exception as e:
        try:
            print cur.statement
        except:
            print "Can't print statement"
        raise e
    if options.get('print_statement', False):
        print cur.statement
    res = cur.fetchall()
    cur.close()
    return res
<commit_msg>Don't search through page ID's if there are no links from the current page<commit_after>from __future__ import unicode_literals
import dbhash
import random
from util.benchmarking import Timer

def queryWithAddedWhere(query, phrase, valueList, appendedValue=None):
    continuing_where = False
    if query:
        continuing_where = """WHERE""" in query
    if continuing_where:
        query = query + """ AND"""
    else:
        query = query + """ WHERE"""
    valueList.append(appendedValue)
    return query + phrase

def queryWithAddedWhereAppend(query, phrase, valueList, appendedList=None):
    continuing_where = False
    if query:
        continuing_where = """WHERE""" in query
    if continuing_where:
        query = query + """ AND"""
    else:
        query = query + """ WHERE"""
    for v in appendedList:
        valueList.append(v)
    return query + phrase

def randomIndexedPage(dbconn):
    cur = dbconn.connection.cursor()
    offset = random.randint(0, 4961336)
    query = """SELECT id FROM page_categories LIMIT 1 OFFSET %s;"""
    values = (offset,)
    cur.execute(query, values)
    res = cur.fetchall()
    cur.close()
    return res[0][0]

def pageIDForPageTitle(dbconn, name, doCache=False):
    if name in dbconn.name_cache:
        return dbconn.name_cache[name]

    cursor = dbconn.connection.cursor()
    query = """SELECT page_id FROM page_names WHERE name = %s"""
    values = (name,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    cursor.close()
    if len(res) is not 0:
        pageID = res[0][0]
        if doCache:
            dbconn.name_cache[name] = pageID
        return pageID
    else:
        return None

def pageTitleForPageID(dbconn, pageID):
    cursor = dbconn.connection.cursor()
    query = """SELECT name FROM page_names WHERE page_id = %s"""
    values = (pageID,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res) is not 0:
        name = res[0][0]
    else:
        name = None
    cursor.close()
    return name.decode('utf-8')

def followRedirectForPageID(dbconn, pageID):
    cursor = dbconn.connection.cursor()
    query = """SELECT to_id FROM redirects WHERE from_id = (%s)"""
    values = (pageID,)
    cursor.execute(query, values)
    res = cursor.fetchall();
    cursor.close()

    if len(res) is not 0:
        pageID = res[0][0]
    return pageID

def pagesLinkedFromPageID(dbconn, pageID):
    cursor = dbconn.connection.cursor()
    query = """SELECT to_id FROM page_links WHERE from_id = %s"""
    values = (pageID,)
    cursor.execute(query, values)
    links = cursor.fetchall()
    cursor.close()
    links = [l[0] for l in links]
    return links

def categoryForPageID(dbconn, pageID, order='minor'):
    cursor = dbconn.connection.cursor()
    query = """SELECT minor_category, major_category FROM page_categories WHERE id=%s;"""
    values = (pageID,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res)==0:
        return None
    elif order=='minor':
        return res[0][0]
    elif order=='major':
        return res[0][1]
    else:
        raise ValueError("order argument must be one of ['minor', 'major']")

def textForLineID(dbconn, lineID):
    cursor = dbconn.connection.cursor()
    query = """SELECT line FROM iambic_lines WHERE id=%s;"""
    values = (lineID,)
    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res)==0:
        return None
    return res[0][0]

def rhymeCountForRhyme(dbconn, word, rhyme):
    cursor = dbconn.connection.cursor()
    query = """SELECT count FROM rhyme_counts WHERE word=%s"""
    values = (word,)
    cursor.execute(query, values)
    cnt = cursor.fetchall()
    if len(cnt)>0:
        cursor.close()
        return cnt[0][0]
    query = """SELECT COUNT(*) FROM iambic_lines WHERE word != %s AND rhyme_part = %s"""
    values = (word, rhyme)
    cursor.execute(query, values)
    cnt = cursor.fetchall()
    cursor.close()
    return cnt[0][0]

pos_prev_constraints = ["pos_0", "pos_1", "pos_m1", "pos_m2"]
pos_next_constraints = ["pos_len_m2", "pos_len_m1", "pos_len", "pos_len_p1"]
hashed_leading_names = ["leading_2gram", "leading_3gram", "leading_4gram"]
hashed_lagging_names = ["lagging_2gram", "lagging_3gram", "lagging_4gram"]
subquery_constraints = ['starts', 'ends', 'rhyme_part']
pos_config_sets = [{"constraints":pos_prev_constraints, "names":hashed_leading_names},
                    {"constraints":pos_next_constraints, "names":hashed_lagging_names}]

## line must be a dictionary containing the keys for either a lagging 4gram or a leading 4gram
def posCountsForLine(dbconn, line, position='leading'):
    cursor = dbconn.connection.cursor()

    if position=='leading':
        dict_to_hash = {k:line[k] for k in line if k in pos_next_constraints}
        query = """SELECT COUNT(*) FROM leading_pos_counts WHERE leading_4gram=%s"""
        values = (dbhash.columnsDictToSHA(dict_to_hash), )
    elif position=='lagging':
        dict_to_hash = {k:line[k] for k in line if k in pos_prev_constraints}
        query = """SELECT COUNT(*) FROM lagging_pos_counts WHERE lagging_4gram=%s"""
        values = (dbhash.columnsDictToSHA(dict_to_hash), )
    else:
        raise ValueError("position must be one of ['leading', 'lagging']")

    cursor.execute(query, values)
    res = cursor.fetchall()
    if len(res) > 0:
        return res[0][0]
    return 0

def optimizedConstraints(constraints):
    mod_constraints = {k:constraints[k] for k in constraints}

    for pos_config in pos_config_sets:

        pos_constraints = pos_config["constraints"]
        column_names = pos_config["names"]

        # Get the columns that we are going to combine into a precomputed hash for those columns
        pos_columns = filter(lambda x: x in pos_constraints, constraints.keys())

        # Check whether or not there are at least two parts of speech in question
        if len(pos_columns) >= 2:

            # Get a dictionary for which to compute a hash
            dict_to_hash = {col:constraints[col] for col in pos_columns}
            dict_as_sha = dbhash.columnsDictToSHA(dict_to_hash)

            # modify options to include the new keys
            hashed_column_name = column_names[len(pos_columns) - 2]
            for p in pos_columns:
                mod_constraints.pop(p)
            mod_constraints[hashed_column_name] = dict_as_sha

    return mod_constraints

def queryWithAddedConstraint(_query, key, valueList, constraints):
    if key == "excluded_word":
        _query = queryWithAddedWhere(_query, """ iambic_lines.word != %s""", valueList, constraints[key])
    elif key == "excluded_lines":
        excludedLines = constraints[key]
        if len(excludedLines) is 1:
            _query = queryWithAddedWhere(_query, """ iambic_lines.id != %s""", valueList, excludedLines[0])
        elif len(excludedLines) is not 0:
            format_strings = ','.join(['%s'] * len(excludedLines))
            _query = queryWithAddedWhereAppend(_query, """ iambic_lines.id NOT IN (%s)""" % format_strings, valueList, list(excludedLines))
    else:
        if key in hashed_leading_names + hashed_lagging_names:
            _query = queryWithAddedWhere(_query, """ pos_hashes.""" + key + """ = %s""", valueList, constraints[key])
        else:
            _query = queryWithAddedWhere(_query, """ iambic_lines.""" + key + """ = %s""", valueList, constraints[key])
    return _query

def createViewForConstraints(dbconn, constraints=None, temp_view_name='tv'):
    if constraints is None:
        return

    cur = dbconn.connection.cursor()
    query = """CREATE VIEW {} AS SELECT * FROM iambic_lines """.format(temp_view_name)
    valueList = [];
    for key in constraints:
        if key == "excluded_word":
            query = queryWithAddedWhere(query, """ word != %s""", valueList, constraints[key])
        elif key == "excluded_lines":
            excludedLines = constraints[key]
            if len(excludedLines) is 1:
                query = queryWithAddedWhere(query, """ id != %s""", valueList, excludedLines[0])
            elif len(excludedLines) is not 0:
                format_strings = ','.join(['%s'] * len(excludedLines))
                query = queryWithAddedWhereAppend(query, """ id NOT IN (%s)""" % format_strings, valueList, list(excludedLines))
        else:
            query = queryWithAddedWhere(query, """ """ + key + """ = %s""", valueList, constraints[key])

    values = tuple(valueList)
    cur.execute(query, values)

def clearView(dbconn, temp_view_name='tv'):
    cur = dbconn.connection.cursor()
    cur.execute("DROP VIEW IF EXISTS {};".format(temp_view_name))

def searchForLines(dbconn, group=None, constraints=None, options=None):

    cur = dbconn.connection.cursor(dictionary=True)
    num = options.get('num', 1)
    brandom = options.get('random', False)
    optimized = options.get('optimized', False)
    view_constraints = options.get('view_constraints', [])
    table_name = options.get('view_name', 'iambic_lines')

    query = """SELECT {0}.id, {0}.page_id, word, rhyme_part, starts, ends, pos_m2, pos_m1, pos_0, pos_1, pos_len_m2, pos_len_m1, pos_len, pos_len_p1, word_len_m1, word_len FROM {0} """.format(table_name)
    valueList = [];
    continuing_where = False
    use_subquery = options.get('subquery', False)
    subquery = """SELECT id FROM {} """.format(table_name)
    if optimized:
        constraints = optimizedConstraints(constraints)
        if any([p in constraints for p in hashed_leading_names+hashed_lagging_names]):
            query = query + """ join pos_hashes on pos_hashes.line_id = {}.id""".format(table_name)
    if group:
        if 'page_major_category' in group or 'page_minor_category' in group:
            query = query + """ join page_categories on page_categories.id = {}.page_id""".format(table_name)
        if 'line_major_category' in group or 'line_minor_category' in group:
            query = query + """ join line_categories on line_categories.id = {}.id""".format(table_name)

    if use_subquery:
        ### QUERY ###
        for key in constraints:
            if key not in subquery_constraints:
                query = queryWithAddedConstraint(query, key, valueList, constraints)

        if group:
            if 'pageIDs' in group:
                pages = group['pageIDs']
                if len(pages) is 1:
                    query = queryWithAddedWhere(query, """ page_id = %s""", valueList, pages[0])
                else:
                    format_strings = ','.join(['%s'] * len(pages))
                    query = queryWithAddedWhereAppend(query, """ page_id IN (%s)""" % format_strings, valueList, list(pages))
            if 'page_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['page_minor_category'])
            if 'page_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['page_major_category'])
            if 'line_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['line_minor_category'])
            if 'line_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['line_major_category'])
        ### QUERY ###

        ### SUBQUERY ###
        subquery = """SELECT id FROM iambic_lines """
        for key in constraints:
            if key in subquery_constraints:
                subquery = queryWithAddedConstraint(subquery, key, valueList, constraints)
        ### SUBQUERY ###

    else:
        ### Building WHERE clause without subquery ###
        for key in [k for k in constraints if k not in view_constraints]:
            if key == "excluded_word":
                query = queryWithAddedWhere(query, """ word != %s""", valueList, constraints[key])
            elif key == "excluded_lines":
                excludedLines = constraints[key]
                if len(excludedLines) is 1:
                    query = queryWithAddedWhere(query, """ id != %s""", valueList, excludedLines[0])
                elif len(excludedLines) is not 0:
                    format_strings = ','.join(['%s'] * len(excludedLines))
                    query = queryWithAddedWhereAppend(query, """ id NOT IN (%s)""" % format_strings, valueList, list(excludedLines))
            else:
                if key in hashed_leading_names + hashed_lagging_names:
                    query = queryWithAddedWhere(query, """ pos_hashes.""" + key + """ = %s""", valueList, constraints[key])
                else:
                    query = queryWithAddedWhere(query, """ {}.""".format(table_name) + key + """ = %s""", valueList, constraints[key])

        if group:
            if 'pageIDs' in group:
                pages = group['pageIDs']
                if len(pages) is 1:
                    query = queryWithAddedWhere(query, """ page_id = %s""", valueList, pages[0])
                elif len(pages) > 0:
                    format_strings = ','.join(['%s'] * len(pages))
                    query = queryWithAddedWhereAppend(query, """ page_id IN (%s)""" % format_strings, valueList, list(pages))
            if 'page_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['page_minor_category'])
            if 'page_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['page_major_category'])
            if 'line_minor_category' in group:
                query = queryWithAddedWhere(query, """ minor_category = %s""", valueList, group['line_minor_category'])
            if 'line_major_category' in group:
                query = queryWithAddedWhere(query, """ major_category = %s""", valueList, group['line_major_category'])
        ### /WHERE ###

    if use_subquery:
        if not """WHERE""" in query:
            query = query + """ WHERE {}.id IN (""".format(table_name) + subquery + """)"""
        else:
            query = query + """ AND {}.id IN (""".format(table_name) + subquery + """)"""

    if brandom:
        query = query + """ ORDER BY RAND()"""

    query = query + """ LIMIT %s;"""
    valueList.append(num)
    values = tuple(valueList)
    try:
        cur.execute(query, values)
    except Exception as e:
        try:
            print cur.statement
        except:
            print "Can't print statement"
        raise e
    if options.get('print_statement', False):
        print cur.statement
    res = cur.fetchall()
    cur.close()
    return res
<|endoftext|>


<commit_before>import logging
import pprint

from django.views import generic
from django.urls import reverse_lazy
from django.contrib.auth.forms import UserCreationForm

from .forms import CustomUserCreationForm
from users.models import UserProfile

# from django.contrib.auth import login
# from django.core.exceptions import ValidationError
# from django.shortcuts import render, redirect
# from django.contrib.auth import login, authenticate

logger = logging.getLogger('django.' + __name__)

class UserRegistrationView(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'registration/register.html'
<commit_msg>Empty UserProfile created on user registration.<commit_after>import logging
import pprint

from django.views import generic
from django.urls import reverse_lazy
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect

from .forms import CustomUserCreationForm
from users.models import UserProfile

# from django.contrib.auth import login
# from django.core.exceptions import ValidationError
# from django.shortcuts import render, redirect
# from django.contrib.auth import login, authenticate

logger = logging.getLogger('django.' + __name__)

class UserRegistrationView(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'registration/register.html'

    def post(self, request, *args, **kwargs):
        form = CustomUserCreationForm(request.POST)

        if form.is_valid():
            user = form.save()
            user.refresh_from_db()  # load the profile instance created by the signal
            UserProfile.objects.create(user=user)
            return redirect('login')
        else:
            form = CustomUserCreationForm()

        return render(request, self.template_name, {'form': form})
<|endoftext|>


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

import re
from .version import __version__
from functools import wraps

class ParseError(RuntimeError):
    pass

class Parser(object):
    def __init__(self, fn):
        self.fn = fn

    def __call__(self, stream, index, on_success, on_failure):
        return self.fn(stream, index, on_success, on_failure)

    def parse(self, string):
        (result, _) = (self << eof).parse_partial(string)
        return result

    def parse_partial(self, string):
        # this success function will be tail-called, so its
        # return value will come out of this function
        def success(index, result):
            return (result, string[index:])

        def failure(index, expected):
            raise ParseError('expected '+repr(expected)+' at '+repr(index))

        return self(string, 0, success, failure)

    def bind(self, bind_fn):
        @Parser
        def bound_parser(stream, index, on_success, on_failure):
            def success(new_index, result):
                next_parser = bind_fn(result)
                return next_parser(stream, new_index, on_success, on_failure)

            return self(stream, index, success, on_failure)

        return bound_parser

    def map(self, map_fn):
        return self.bind(lambda res: success(map_fn(res)))

    def then(self, other):
        return self.bind(lambda _: other)

    def skip(self, other):
        return self.bind(lambda res: other.result(res))

    def result(self, res):
        return self >> success(res)

    def many(self):
        @Parser
        def many_parser(stream, index, on_success, on_failure):
            aggregate = []
            def success(new_index, res):
                nonlocal index
                index = new_index
                aggregate.append(res)
                return True

            def failure(new_index, message):
                return False

            while self(stream, index, success, failure):
                pass

            return on_success(index, aggregate)

        return many_parser

    def times(self, min, max=None):
        if max is None:
            max = min

        @Parser
        def times_parser(stream, index, on_success, on_failure):
            aggregate = []
            fail_message = None

            def success(new_index, res):
                nonlocal index
                index = new_index
                aggregate.append(res)
                return True

            def first_failure(new_index, msg):
                nonlocal index
                nonlocal fail_message
                index = new_index
                fail_message = msg
                return False

            def second_failure(new_index, msg):
                return False

            for times in range(0, min):
                result = self(stream, index, success, first_failure)
                if not result:
                    return on_failure(index, fail_message)

            for times in range(min, max):
                result = self(stream, index, success, second_failure)
                if not result:
                    break

            return on_success(index, aggregate)

        return times_parser

    def at_most(self, n):
        return self.times(0, n)

    def at_least(self, n):
        @generate
        def at_least_parser():
            start = yield self.times(n)
            end = yield self.many()
            return start + end

        return at_least_parser

    def __or__(self, other):
        if not isinstance(other, Parser):
            raise TypeError('%s is not a parser!' % repr(Parser))

        @Parser
        def or_parser(stream, index, on_success, on_failure):
            def failure(new_index, message):
                # we use the closured index here so it backtracks
                return other(stream, index, on_success, on_failure)

            return self(stream, index, on_success, failure)

        return or_parser

    # haskelley operators, for fun #

    # >>
    def __rshift__(self, other):
        return self.then(other)

    # <<
    def __lshift__(self, other):
        return self.skip(other)

# combinator syntax
def generate(fn):
    def genparser():
        iterator = fn()

        def send(val):
            try:
                next_parser = iterator.send(val)
                return next_parser.bind(send)
            except StopIteration as result:
                if isinstance(result.value, Parser):
                    return result.value

                return success(result.value)

        return send(None)

    # this makes sure there is a separate instance of the generator
    # for each parse
    @wraps(fn)
    @Parser
    def generated(*args):
        return genparser()(*args)

    return generated

def success(val):
    @Parser
    def success_parser(stream, index, on_success, on_failure):
        return on_success(index, val)

    return success_parser

def string(s):
    slen = len(s)

    @Parser
    def string_parser(stream, index, on_success, on_failure):
        if stream[index:index+slen] == s:
            return on_success(index+slen, s)
        else:
            return on_failure(index, s)

    string_parser.__name__ = 'string_parser<%s>' % s

    return string_parser

def regex(exp, flags=0):
    if isinstance(exp, str):
        exp = re.compile(exp, flags)

    @Parser
    def regex_parser(stream, index, on_success, on_failure):
        match = exp.match(stream, index)
        if match:
            return on_success(match.end(), match.group(0))
        else:
            return on_failure(index, exp.pattern)

    regex_parser.__name__ = 'regex_parser<%s>' % exp.pattern

    return regex_parser

whitespace = regex(r'\s+')

@Parser
def eof(stream, index, on_success, on_failure):
    if index < len(stream):
        return on_failure(index, 'EOF')

    return on_success(index, None)
<commit_msg>refactor to eliminate CPS in the absence of TCO<commit_after># -*- coding: utf-8 -*- #

import re
from .version import __version__
from functools import wraps
from collections import namedtuple

class ParseError(RuntimeError):
    pass

Result = namedtuple('Result', ['success', 'index', 'value'])

class Parser(object):
    """
    doc
    """

    def __init__(self, fn):
        self.fn = fn

    def __call__(self, stream, index):
        return self.fn(stream, index)

    def parse(self, string):
        (result, _) = (self << eof).parse_partial(string)
        return result

    def parse_partial(self, string):
        (success, index, value) = self(string, 0)

        if success:
            return (value, string[index:])
        else:
            raise ParseError('expected '+repr(value)+' at '+repr(index))

    def bind(self, bind_fn):
        @Parser
        def bound_parser(stream, index):
            def success(new_index, result):
                next_parser = bind_fn(result)
                return next_parser(stream, new_index)

            (success, new_index, value) = self(stream, index)

            if success:
                next_parser = bind_fn(value)
                return next_parser(stream, new_index)
            else:
                return (False, index, value)

        return bound_parser

    def map(self, map_fn):
        return self.bind(lambda res: success(map_fn(res)))

    def then(self, other):
        return self.bind(lambda _: other)

    def skip(self, other):
        return self.bind(lambda res: other.result(res))

    def result(self, res):
        return self >> success(res)

    def many(self):
        @Parser
        def many_parser(stream, index):
            aggregate = []
            next_index = index

            while True:
                (status, next_index, value) = self(stream, index)
                if status:
                    aggregate.append(value)
                    index = next_index
                else:
                    break

            return (True, index, aggregate)

        return many_parser

    def times(self, min, max=None):
        if max is None:
            max = min

        @Parser
        def times_parser(stream, index):
            aggregate = []
            next_index = index

            for times in range(0, min):
                (status, next_index, value) = self(stream, index)
                index = next_index
                if status:
                    aggregate.append(value)
                else:
                    return (False, index, value)

            for times in range(min, max):
                (status, next_index, value) = self(stream, index)
                if status:
                    index = next_index
                    aggregate.append(value)
                else:
                    break

            return (True, index, aggregate)

        return times_parser

    def at_most(self, n):
        return self.times(0, n)

    def at_least(self, n):
        @generate
        def at_least_parser():
            start = yield self.times(n)
            end = yield self.many()
            return start + end

        return at_least_parser

    def __or__(self, other):
        if not isinstance(other, Parser):
            raise TypeError('%s is not a parser!' % repr(Parser))

        @Parser
        def or_parser(stream, index):
            def failure(new_index, message):
                # we use the closured index here so it backtracks
                return other(stream, index)

            (status, next_index, value) = self(stream, index)
            if status:
                return (True, next_index, value)
            else:
                return other(stream, index)

        return or_parser

    # haskelley operators, for fun #

    # >>
    def __rshift__(self, other):
        return self.then(other)

    # <<
    def __lshift__(self, other):
        return self.skip(other)

# combinator syntax
def generate(fn):
    @wraps(fn)
    @Parser
    def generated(stream, index):
        iterator = fn()
        value = None
        try:
            while True:
                next_parser = iterator.send(value)
                (status, index, value) = next_parser(stream, index)
                if not status:
                    return (False, index, value)
        except StopIteration as result:
            returnVal = result.value
            if isinstance(returnVal, Parser):
                return returnVal(stream, index)

            return (True, index, returnVal)

    return generated

def success(val):
    @Parser
    def success_parser(stream, index):
        return (True, index, val)

    return success_parser

def string(s):
    slen = len(s)

    @Parser
    def string_parser(stream, index):
        if stream[index:index+slen] == s:
            return (True, index+slen, s)
        else:
            return (False, index, s)

    string_parser.__name__ = 'string_parser<%s>' % s

    return string_parser

def regex(exp, flags=0):
    if isinstance(exp, str):
        exp = re.compile(exp, flags)

    @Parser
    def regex_parser(stream, index):
        match = exp.match(stream, index)
        if match:
            return (True, match.end(), match.group(0))
        else:
            return (False, index, exp.pattern)

    regex_parser.__name__ = 'regex_parser<%s>' % exp.pattern

    return regex_parser

whitespace = regex(r'\s+')

@Parser
def eof(stream, index):
    if index < len(stream):
        return (False, index, 'EOF')

    return (True, index, None)
<|endoftext|>


<commit_before>from datetime import datetime
from peewee import *

from src.extensions import db

def create_tables():
    db.connect()
    db.create_tables([PDFForm], True)

class BaseModel(Model):
    class Meta:
        database = db

class PDFForm(BaseModel):
    id = IntegerField(primary_key=True, index=True)
    added_on = DateTimeField(default=datetime.now)
    original_pdf = BlobField()
    original_pdf_title = CharField(max_length=128)
    fdf_mapping = TextField()
    post_count = IntegerField(default=0)
    latest_post = DateTimeField(default=datetime.now)
<commit_msg>back to a SQLAlchemy model<commit_after>from src.extensions import db

class PDFForm(db.Model):
    __tablename__ = 'pdfform'
    id = db.Column(db.Integer, primary_key=True, index=True)
    added_on = db.Column(db.DateTime(), server_default=db.func.now())
    original_pdf = db.Column(db.LargeBinary)
    original_pdf_title = db.Column(db.Text)
    fdf_mapping = db.Column(db.Text)
    post_count = db.Column(db.Integer, default=0)
    latest_post = db.Column(db.DateTime())

    def __repr__(self):
        return '<PDFForm:"[{}] {}">'.format(self.id, self.original_pdf_title)<|endoftext|>


<commit_before># Copyright (C) 2018 OpenMotics BVBA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
""""
The hardware_utils module contains various classes helping with Hardware and System abstraction
"""
import os
import sys
import subprocess

class Hardware(object):
    """
    Abstracts the hardware related functions
    """

    class Led(object):
        POWER = 'POWER'
        STATUS = 'STATUS'
        ALIVE = 'ALIVE'
        CLOUD = 'CLOUD'
        VPN = 'VPN'
        COMM_1 = 'COMM_1'
        COMM_2 = 'COMM_2'

    class BoardType(object):
        BB = 'BB'
        BBB = 'BBB'
        BBGW = 'BBGW'

    BoardTypes = [BoardType.BB, BoardType.BBB, BoardType.BBGW]
    IOCTL_I2C_SLAVE = 0x0703

    @staticmethod
    def get_board_type():
        try:
            with open('/proc/device-tree/model', 'r') as mfh:
                board_type = mfh.read().strip('\x00').replace(' ', '_')
                if board_type in ['TI_AM335x_BeagleBone', 'TI_AM335x_BeagleBone_Black']:
                    return Hardware.BoardType.BBB
                if board_type in ['TI_AM335x_BeagleBone_Green_Wireless']:
                    return Hardware.BoardType.BBGW
        except IOError:
            pass
        with open('/proc/meminfo', 'r') as memfh:
            mem_total = memfh.readline()
            if '254228 kB' in mem_total:
                return Hardware.BoardType.BB
            if '510716 kB' in mem_total:
                return Hardware.BoardType.BBB
        return  # Unknown

    @staticmethod
    def get_i2c_device():
        return '/dev/i2c-2' if Hardware.get_board_type() == Hardware.BoardType.BB else '/dev/i2c-1'

    @staticmethod
    def get_local_interface():
        board_type = Hardware.get_board_type()
        if board_type in [Hardware.BoardType.BB, Hardware.BoardType.BBB]:
            return 'eth0'
        elif board_type == Hardware.BoardType.BBGW:
            return 'wlan0'
        else:
            return 'lo'

    @staticmethod
    def get_i2c_led_config():
        if not Hardware.get_board_type() == Hardware.BoardType.BB:
            return {'COMM_1': 64,
                    'COMM_2': 128,
                    'VPN': 16,
                    'ALIVE': 1,
                    'CLOUD': 4}
        return {'COMM_1': 64,
                'COMM_2': 128,
                'VPN': 16,
                'CLOUD': 4}

    @staticmethod
    def get_gpio_led_config():
        if not Hardware.get_board_type() == Hardware.BoardType.BB:
            return {'POWER': 60,
                    'STATUS': 48}
        return {'POWER': 75,
                'STATUS': 60,
                'ALIVE': 49}

    @staticmethod
    def get_gpio_input():
        return 38 if Hardware.get_board_type() == Hardware.BoardType.BB else 26

class System(object):
    """
    Abstracts the system related functions
    """

    @staticmethod
    def _get_operating_system():
        operating_system = {}
        with open('/etc/os-release', 'r') as osfh:
            lines = osfh.readlines()
            for line in lines:
                k, v = line.strip().split('=')
                operating_system[k] = v
        return operating_system

    @staticmethod
    def get_ip_address():
        """ Get the local ip address. """
        interface = Hardware.get_local_interface()
        operating_system = System._get_operating_system()
        try:
            lines = subprocess.check_output('ifconfig {0}'.format(interface), shell=True)
            if operating_system['ID'] == 'angstrom':
                return lines.split('\n')[1].strip().split(' ')[1].split(':')[1]
            elif operating_system['ID'] == 'debian':
                return lines.split('\n')[1].strip().split(' ')[1]
            else:
                return
        except Exception:
            return

    @staticmethod
    def get_vpn_service():
        return 'openvpn.service' if System._get_operating_system()['ID'] == 'angstrom' else 'openvpn-client@omcloud'

    @staticmethod
    def _use_pyopenssl():
        return System._get_operating_system()['ID'] == 'angstrom'

    @staticmethod
    def get_ssl_socket(sock, private_key_filename, certificate_filename):
        if System._use_pyopenssl():
            from OpenSSL import SSL
            context = SSL.Context(SSL.SSLv23_METHOD)
            context.use_privatekey_file(private_key_filename)
            context.use_certificate_file(certificate_filename)
            return SSL.Connection(context, sock)
        import ssl
        return ssl.wrap_socket(sock,
                               keyfile=private_key_filename,
                               certfile=certificate_filename,
                               ssl_version=ssl.PROTOCOL_SSLv23,
                               do_handshake_on_connect=False,
                               suppress_ragged_eofs=False)

    @staticmethod
    def setup_cherrypy_ssl(https_server, private_key_filename, certificate_filename):
        if System._use_pyopenssl():
            https_server.ssl_module = 'pyopenssl'
        else:
            import ssl
            https_server.ssl_module = 'builtin'
            https_server.ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        https_server.ssl_certificate = certificate_filename
        https_server.ssl_private_key = private_key_filename

    @staticmethod
    def handle_socket_exception(connection, exception, logger):
        if System._use_pyopenssl():
            import select
            from OpenSSL import SSL
            if isinstance(exception, SSL.SysCallError):
                if exception[0] == 11:  # Temporarily unavailable
                    # This should be ok, just wait for more data to arrive
                    return True  # continue
                if exception[0] == -1:  # Unexpected EOF
                    logger.info('Got (unexpected) EOF, aborting due to lost connection')
                    return False  # break
            elif isinstance(exception, SSL.WantReadError):
                # This should be ok, just wait for more data to arrive
                select.select([connection], [], [], 1.0)
                return True  # continue
        else:
            import select
            import ssl
            if isinstance(exception, ssl.SSLEOFError):
                logger.info('Got SSLEOFError, aborting due to lost connection')
                return False  # break
            elif isinstance(exception, ssl.SSLError):
                if 'The read operation timed out' in str(exception):
                    # Got read timeout, just wait for data to arrive
                    return True  # continue
        raise exception

    @staticmethod
    def import_eggs():
        operating_system = System._get_operating_system()
        blacklisted_eggs = {'debian': ['requests-1.2.0-py2.7.egg']}.get(operating_system['ID'], [])
        os.environ['PYTHON_EGG_CACHE'] = '/tmp/.eggs-cache/'
        for egg in os.listdir('/opt/openmotics/python/eggs'):
            if egg.endswith('.egg') and egg not in blacklisted_eggs:
                sys.path.insert(0, '/opt/openmotics/python/eggs/{0}'.format(egg))
                # Patching where/if required
                if egg == 'requests-1.2.0-py2.7.egg':
                    from pkg_resources import resource_filename, resource_stream, Requirement
                    resource_stream(Requirement.parse('requests'), 'requests/cacert.pem')
                    os.environ['REQUESTS_CA_BUNDLE'] = resource_filename(Requirement.parse('requests'), 'requests/cacert.pem')
<commit_msg>Remove hard-coded path to load eggs<commit_after># Copyright (C) 2018 OpenMotics BVBA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
""""
The hardware_utils module contains various classes helping with Hardware and System abstraction
"""
import os
import sys
import subprocess

class Hardware(object):
    """
    Abstracts the hardware related functions
    """

    class Led(object):
        POWER = 'POWER'
        STATUS = 'STATUS'
        ALIVE = 'ALIVE'
        CLOUD = 'CLOUD'
        VPN = 'VPN'
        COMM_1 = 'COMM_1'
        COMM_2 = 'COMM_2'

    class BoardType(object):
        BB = 'BB'
        BBB = 'BBB'
        BBGW = 'BBGW'

    BoardTypes = [BoardType.BB, BoardType.BBB, BoardType.BBGW]
    IOCTL_I2C_SLAVE = 0x0703

    @staticmethod
    def get_board_type():
        try:
            with open('/proc/device-tree/model', 'r') as mfh:
                board_type = mfh.read().strip('\x00').replace(' ', '_')
                if board_type in ['TI_AM335x_BeagleBone', 'TI_AM335x_BeagleBone_Black']:
                    return Hardware.BoardType.BBB
                if board_type in ['TI_AM335x_BeagleBone_Green_Wireless']:
                    return Hardware.BoardType.BBGW
        except IOError:
            pass
        with open('/proc/meminfo', 'r') as memfh:
            mem_total = memfh.readline()
            if '254228 kB' in mem_total:
                return Hardware.BoardType.BB
            if '510716 kB' in mem_total:
                return Hardware.BoardType.BBB
        return  # Unknown

    @staticmethod
    def get_i2c_device():
        return '/dev/i2c-2' if Hardware.get_board_type() == Hardware.BoardType.BB else '/dev/i2c-1'

    @staticmethod
    def get_local_interface():
        board_type = Hardware.get_board_type()
        if board_type in [Hardware.BoardType.BB, Hardware.BoardType.BBB]:
            return 'eth0'
        elif board_type == Hardware.BoardType.BBGW:
            return 'wlan0'
        else:
            return 'lo'

    @staticmethod
    def get_i2c_led_config():
        if not Hardware.get_board_type() == Hardware.BoardType.BB:
            return {'COMM_1': 64,
                    'COMM_2': 128,
                    'VPN': 16,
                    'ALIVE': 1,
                    'CLOUD': 4}
        return {'COMM_1': 64,
                'COMM_2': 128,
                'VPN': 16,
                'CLOUD': 4}

    @staticmethod
    def get_gpio_led_config():
        if not Hardware.get_board_type() == Hardware.BoardType.BB:
            return {'POWER': 60,
                    'STATUS': 48}
        return {'POWER': 75,
                'STATUS': 60,
                'ALIVE': 49}

    @staticmethod
    def get_gpio_input():
        return 38 if Hardware.get_board_type() == Hardware.BoardType.BB else 26

class System(object):
    """
    Abstracts the system related functions
    """

    @staticmethod
    def _get_operating_system():
        operating_system = {}
        with open('/etc/os-release', 'r') as osfh:
            lines = osfh.readlines()
            for line in lines:
                k, v = line.strip().split('=')
                operating_system[k] = v
        return operating_system

    @staticmethod
    def get_ip_address():
        """ Get the local ip address. """
        interface = Hardware.get_local_interface()
        operating_system = System._get_operating_system()
        try:
            lines = subprocess.check_output('ifconfig {0}'.format(interface), shell=True)
            if operating_system['ID'] == 'angstrom':
                return lines.split('\n')[1].strip().split(' ')[1].split(':')[1]
            elif operating_system['ID'] == 'debian':
                return lines.split('\n')[1].strip().split(' ')[1]
            else:
                return
        except Exception:
            return

    @staticmethod
    def get_vpn_service():
        return 'openvpn.service' if System._get_operating_system()['ID'] == 'angstrom' else 'openvpn-client@omcloud'

    @staticmethod
    def _use_pyopenssl():
        return System._get_operating_system()['ID'] == 'angstrom'

    @staticmethod
    def get_ssl_socket(sock, private_key_filename, certificate_filename):
        if System._use_pyopenssl():
            from OpenSSL import SSL
            context = SSL.Context(SSL.SSLv23_METHOD)
            context.use_privatekey_file(private_key_filename)
            context.use_certificate_file(certificate_filename)
            return SSL.Connection(context, sock)
        import ssl
        return ssl.wrap_socket(sock,
                               keyfile=private_key_filename,
                               certfile=certificate_filename,
                               ssl_version=ssl.PROTOCOL_SSLv23,
                               do_handshake_on_connect=False,
                               suppress_ragged_eofs=False)

    @staticmethod
    def setup_cherrypy_ssl(https_server, private_key_filename, certificate_filename):
        if System._use_pyopenssl():
            https_server.ssl_module = 'pyopenssl'
        else:
            import ssl
            https_server.ssl_module = 'builtin'
            https_server.ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        https_server.ssl_certificate = certificate_filename
        https_server.ssl_private_key = private_key_filename

    @staticmethod
    def handle_socket_exception(connection, exception, logger):
        if System._use_pyopenssl():
            import select
            from OpenSSL import SSL
            if isinstance(exception, SSL.SysCallError):
                if exception[0] == 11:  # Temporarily unavailable
                    # This should be ok, just wait for more data to arrive
                    return True  # continue
                if exception[0] == -1:  # Unexpected EOF
                    logger.info('Got (unexpected) EOF, aborting due to lost connection')
                    return False  # break
            elif isinstance(exception, SSL.WantReadError):
                # This should be ok, just wait for more data to arrive
                select.select([connection], [], [], 1.0)
                return True  # continue
        else:
            import select
            import ssl
            if isinstance(exception, ssl.SSLEOFError):
                logger.info('Got SSLEOFError, aborting due to lost connection')
                return False  # break
            elif isinstance(exception, ssl.SSLError):
                if 'The read operation timed out' in str(exception):
                    # Got read timeout, just wait for data to arrive
                    return True  # continue
        raise exception

    @staticmethod
    def import_eggs():
        current_file_path = os.path.dirname(os.path.abspath(__file__))
        operating_system = System._get_operating_system()
        blacklisted_eggs = {'debian': ['requests-1.2.0-py2.7.egg']}.get(operating_system['ID'], [])
        os.environ['PYTHON_EGG_CACHE'] = '/tmp/.eggs-cache/'
        for egg in os.listdir('{0}/eggs'.format(current_file_path)):
            if egg.endswith('.egg') and egg not in blacklisted_eggs:
                sys.path.insert(0, '{0}/eggs/{1}'.format(current_file_path, egg))
                # Patching where/if required
                if egg == 'requests-1.2.0-py2.7.egg':
                    from pkg_resources import resource_filename, resource_stream, Requirement
                    resource_stream(Requirement.parse('requests'), 'requests/cacert.pem')
                    os.environ['REQUESTS_CA_BUNDLE'] = resource_filename(Requirement.parse('requests'), 'requests/cacert.pem')
<|endoftext|>


<commit_before>from pocket import Pocket, AuthException
from workflow import Workflow, PasswordNotFound

import config

def get_list(wf, access_token):
    pocket_instance = Pocket(config.CONSUMER_KEY, access_token)
    try:
        get = pocket_instance.get(sort='newest')
        get_list = get[0]['list']
        if get_list == []:
            return None

        # Unpack and sort items
        item_list = []
        for i in sorted(get_list.keys(), key=lambda x: int(x), reverse=True):
            item_list.append(get_list[i])

        return item_list

    except AuthException:
        return 'error1'
        wf.delete_password('pocket_access_token')
        wf.logger.error(
            'There was a problem receiving your Pocket list. The workflow has been deauthenticated automatically. Please try again!')
    except Exception:
        return 'error2'
        wf.logger.error(
            'Could not contact getpocket.com. Please check your Internet connection and try again!')

    return None

if __name__ == '__main__':
    wf = Workflow()

    try:
        access_token = wf.get_password('pocket_access_token')

        def wrapper():
            return get_list(wf, access_token)

        wf.cached_data('pocket_list', data_func=wrapper, max_age=1)

    except PasswordNotFound:
        wf.logger.error('Password not found!')
<commit_msg>Sort by 'time_added'<commit_after>from pocket import Pocket, AuthException
from workflow import Workflow, PasswordNotFound

import config

def get_list(wf, access_token):
    pocket_instance = Pocket(config.CONSUMER_KEY, access_token)
    try:
        get = pocket_instance.get(sort='newest')
        get_list = get[0]['list']
        if get_list == []:
            return None

        # Unpack and sort items
        item_list = []
        for item in sorted(get_list.values(), key=lambda x: int(x['time_added']), reverse=True):
            item_list.append(item)

        return item_list

    except AuthException:
        return 'error1'
        wf.delete_password('pocket_access_token')
        wf.logger.error(
            'There was a problem receiving your Pocket list. The workflow has been deauthenticated automatically. Please try again!')
    except Exception:
        return 'error2'
        wf.logger.error(
            'Could not contact getpocket.com. Please check your Internet connection and try again!')

    return None

if __name__ == '__main__':
    wf = Workflow()

    try:
        access_token = wf.get_password('pocket_access_token')

        def wrapper():
            return get_list(wf, access_token)

        wf.cached_data('pocket_list', data_func=wrapper, max_age=1)

    except PasswordNotFound:
        wf.logger.error('Password not found!')
<|endoftext|>


<commit_before>from unittest import TestCase
from tsdesktop import config

class TestConfig(TestCase):
    def setUp(self):
        config.read('/dev/null')

    def test_service_defaults(self):
        with self.assertRaises(KeyError):
            config.cfg['tsadm']

    def test_service_httpd(self):
        httpd = config.cfg['service:httpd']
        self.assertTrue(httpd.getboolean('enable'))

    def test_service_mysqld(self):
        mysqld = config.cfg['service:mysqld']
        self.assertTrue(mysqld.getboolean('enable'))
<commit_msg>check default user cachedir<commit_after>from unittest import TestCase
from tsdesktop import config

class TestConfig(TestCase):
    def setUp(self):
        config.read('/dev/null')

    def test_service_defaults(self):
        from os.path import expanduser
        with self.assertRaises(KeyError):
            config.cfg['tsadm']
        self.assertEqual(config.cfg.get('user', 'cachedir'),
            expanduser('~/.cache/tsdesktop'))

    def test_service_httpd(self):
        httpd = config.cfg['service:httpd']
        self.assertTrue(httpd.getboolean('enable'))

    def test_service_mysqld(self):
        mysqld = config.cfg['service:mysqld']
        self.assertTrue(mysqld.getboolean('enable'))
<|endoftext|>


<commit_before>"""
.. module:: processManager
    :synopsis: starts and stops all the processes
"""

import os
import sys
import subprocess
import time
import signal
scriptDir=os.path.dirname(os.path.realpath(__file__))
sys.path.append(scriptDir)
import processNodeUtils
import pdb

class ProcessManager():
    def __init__(self):
        """
        Constructor.  Initializes class variables.
        """
        self.processHandleList = []
        self.processInputList = []
        self.fullConfigPath = ''

    def getProcessList(self):
        """
        Gets current list.  Mainly used for testing to check accessor.
        :returns: list
        """
        return self.processInputList

    def getProcessStatusList(self):
        """
        Gets process status list. Need to further refine this to be more useful.
        :returns: list of type subprocess.Popen
        """
        return self.processHandleList

    def addProcess(self, inProcessDict):
        """
        Add process executable to managed list

        :param inProcessDict: dictionary containing 'processName' and 'endPoint'
        keys that correspond to the python file to run and the IP address of each
        node in the system respectively
        :type inProcessDict: dictionary
        :raises ValueError
        """
        if ('processPath' in inProcessDict and 'endPoint' in inProcessDict):
            self.processInputList.append(inProcessDict)
        else:
            raise ValueError("'processName' or 'endPoint' keys don't exist in input dictionary")

    def importProcessConfig(self, fullConfigPath):
        """
        Imports process configuration file and tries to add all the processes to the master
        list (to be used for spinning up each process).
        :param fullConfigPath: full file path to process config file
        :type fullConfigPath: str
        :raises: ValueError
        """
        self.fullConfigPath = fullConfigPath
        masterProcessConfig = processNodeUtils.importConfigJson(fullConfigPath)
        processList = masterProcessConfig['processList']

        for process in processList:
            #if ('processPath' in process):
            self.processInputList.append(process)
            #else:
            #    raise ValueError("'processPath' not provided in config file")

    def run(self):
        """
        Run all processes in process list.  This should evolve to be more responsive
        to subprocess handling.  For now, this class assumes python processes but could
        be extended to any executable if the permissions and sourced interpretor was
        configured properly

        """
        # start processes
        for idx in range(len(self.processInputList)):
            path = os.path.join(scriptDir,self.processInputList[idx]['processPath'])
            interpreter = processNodeUtils.getInterpreter(path)
            print ('Starting process: ' + str(path) + ' ' + self.processInputList[idx]['processName'])
            self.processHandleList.append(subprocess.Popen([
                interpreter, path, self.processInputList[idx]['processName'], self.fullConfigPath],
                stdout=True))

        # monitor processes to see if they're still alive
        while(True):
            try:
                if (len(self.processHandleList) == 0):
                    break
                for idx in range(len(self.processHandleList)):
                    returnValue = self.processHandleList[idx].poll()
                    if (returnValue != None):
                        print(self.processInputList[idx]['processName'] + \
                            ' process done with return value of ' + str(returnValue))
                        # only pop one at a time because doing so screws up indexing
                        self.processInputList.pop(idx)
                        self.processHandleList.pop(idx)
                        break
                time.sleep(0.1)
            except:
                print ('Exception Occurred.  KILLING ALL PROCESSES.')
                # if ctrl+c or exception occurs, ensure all processes killed
                for process in self.processHandleList:
                    os.kill(process.pid, signal.SIGTERM)

                sys.exit(1)<commit_msg>adding logging facilities to process Manager<commit_after>"""
.. module:: processManager
    :synopsis: starts and stops all the processes
"""

import os
import sys
import subprocess
import time
import signal
scriptDir=os.path.dirname(os.path.realpath(__file__))
sys.path.append(scriptDir)
import processNode
import processNodeUtils
import pdb

# reserved name
PROCESSMANAGERNAME = "processManager"

class ProcessManager():
    def __init__(self):
        """
        Constructor.  Initializes class variables.
        """
        self.processHandleList = []
        self.processInputList = []
        self.fullConfigPath = ''

    def getProcessList(self):
        """
        Gets current list.  Mainly used for testing to check accessor.
        :returns: list
        """
        return self.processInputList

    def getProcessStatusList(self):
        """
        Gets process status list. Need to further refine this to be more useful.
        :returns: list of type subprocess.Popen
        """
        return self.processHandleList

    def addProcess(self, inProcessDict):
        """
        Add process executable to managed list

        :param inProcessDict: dictionary containing 'processName' and 'endPoint'
        keys that correspond to the python file to run and the IP address of each
        node in the system respectively
        :type inProcessDict: dictionary
        :raises ValueError
        """
        if ('processPath' in inProcessDict and 'endPoint' in inProcessDict):
            self.processInputList.append(inProcessDict)
        else:
            raise ValueError("'processName' or 'endPoint' keys don't exist in input dictionary")

    def importProcessConfig(self, fullConfigPath):
        """
        Imports process configuration file and tries to add all the processes to the master
        list (to be used for spinning up each process).
        :param fullConfigPath: full file path to process config file
        :type fullConfigPath: str
        :raises: ValueError
        """
        self.fullConfigPath = fullConfigPath
        self.processNode = processNode.ProcessNode(fullConfigPath, PROCESSMANAGERNAME)
        masterProcessConfig = processNodeUtils.importConfigJson(fullConfigPath)
        processList = masterProcessConfig['processList']

        for process in processList:
            #if ('processPath' in process):
            if (process['processName'] != PROCESSMANAGERNAME):
                self.processInputList.append(process)
            #else:
            #    raise ValueError("'processPath' not provided in config file")
            #
    def startProcess(self, processDict):
        path = os.path.join(scriptDir,processDict['processPath'])
        interpreter = processNodeUtils.getInterpreter(path)
        logMsg = 'Starting process: ' + str(path) + ' ' + processDict['processName']
        print (logMsg)
        self.processNode.log(logLevel=1, message=logMsg)
        self.processHandleList.append(subprocess.Popen([
            interpreter, path, processDict['processName'], self.fullConfigPath],
            stdout=True))

    def run(self, autoRestart=False):
        """
        Run all processes in process list.  This should evolve to be more responsive
        to subprocess handling.  For now, this class assumes python processes but could
        be extended to any executable if the permissions and sourced interpretor was
        configured properly
        """
        # start processes
        for idx in range(len(self.processInputList)):
            self.startProcess(self.processInputList[idx])

        # monitor processes to see if they're still alive
        while(True):
            try:
                if (len(self.processHandleList) == 0):
                    break
                idx = 0
                while (idx < len(self.processHandleList)):
                    returnValue = self.processHandleList[idx].poll()
                    if (returnValue != None):
                        stoppedProcessDict = self.processInputList[idx]
                        logMsg = self.processInputList[idx]['processName'] + \
                            ' process done with return value of ' + str(returnValue)
                        print(logMsg)
                        self.processNode.log(logLevel=3, message=logMsg)
                        # only pop one at a time because doing so screws up indexing
                        self.processInputList.pop(idx)
                        self.processHandleList.pop(idx)
                        if (returnValue != 0 and autoRestart):
                            self.processInputList.append(stoppedProcessDict)
                            self.startProcess(stoppedProcessDict)
                        break
                    idx += 1
                time.sleep(1)
            except:
                print ('Exception Occurred.  KILLING ALL PROCESSES.')
                # if ctrl+c or exception occurs, ensure all processes killed
                for process in self.processHandleList:
                    os.kill(process.pid, signal.SIGTERM)

                sys.exit(1)<|endoftext|>


<commit_before>from django.db import models

from base.models import ImagemTag, Tamanho

class GtinRange(models.Model):
    ordem = models.IntegerField(
        unique=True,
        verbose_name='Ordem de adoção do range')
    pais = models.CharField(
        max_length=3,
        verbose_name='Código do país do range')
    codigo = models.CharField(
        max_length=5, unique=True,
        verbose_name='Código identificador do range')

    def __str__(self):
        return '{} - ({}){}'.format(self.ordem, self.pais, self.codigo)

    class Meta:
        db_table = "fo2_gtin_range"
        verbose_name = "Range de GTIN"

class Composicao(models.Model):
    descricao = models.CharField(
        'descrição',
        max_length=200)

    def __str__(self):
        return self.descricao

    class Meta:
        db_table = "fo2_composicao"
        verbose_name = "Composição"
        verbose_name_plural = "Composições"

class Produto(models.Model):
    nivel = models.IntegerField(
        db_index=True,
        verbose_name='Nível')
    referencia = models.CharField(
        db_index=True, max_length=5,
        verbose_name='Referência')
    descricao = models.CharField(
        'descrição', default='',
        max_length=200)
    ativo = models.BooleanField(default=True)
    imp_cod = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Código impresso')
    composicao = models.ForeignKey(
        Composicao, on_delete=models.SET_NULL, null=True, blank=True,
        verbose_name='Composição')
    imagem_tag = models.ForeignKey(
        ImagemTag, on_delete=models.SET_NULL, null=True, blank=True,
        verbose_name='Imagem no TAG')
    cor_no_tag = models.BooleanField(
        default=True, verbose_name='Imprime cor no TAG')
    ncm = models.CharField(
        max_length=8, default='',
        verbose_name='NCM')
    unidade = models.CharField(
        max_length=2, default='')

    def __str__(self):
        ativo = '' if self.ativo else '--'
        return '{}{}'.format(ativo, self.referencia)

    class Meta:
        db_table = "fo2_produto"
        verbose_name = "Produto"

class ProdutoCor(models.Model):
    produto = models.ForeignKey(
        Produto, on_delete=models.CASCADE,
        verbose_name='Referência')
    cor = models.CharField(
        db_index=True, max_length=6, verbose_name='Cor')
    descricao = models.CharField(
        'descrição', default='',
        max_length=200)
    ativa = models.BooleanField(default=True)
    imp_cod = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Código impresso')
    imp_descr = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Descrição impressa')
    composicao = models.ForeignKey(
        Composicao, on_delete=models.SET_NULL, null=True, blank=True,
        verbose_name='Composição')

    def __str__(self):
        ativa = '' if self.ativa else '--'
        return '{}({}) {}'.format(ativa, self.produto, self.cor)

    class Meta:
        db_table = "fo2_produto_cor"
        verbose_name = "Cor de um produto"
        verbose_name_plural = "Cores de um produto"

class ProdutoTamanho(models.Model):
    produto = models.ForeignKey(
        Produto, on_delete=models.CASCADE,
        verbose_name='Referência')
    tamanho = models.ForeignKey(
        Tamanho, on_delete=models.PROTECT)
    descricao = models.CharField(
        'descrição', default='',
        max_length=200)
    ativo = models.BooleanField(default=True)
    imp_cod = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Código impresso')
    imp_descr = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Descrição impressa')

    def __str__(self):
        ativo = '' if self.ativo else '--'
        return '{}({}) {}'.format(ativo, self.produto, self.tamanho.nome)

    class Meta:
        db_table = "fo2_produto_tamanho"
        verbose_name = "Tamanho de um produto"
        verbose_name_plural = "Tamanhos de um produto"

class ProdutoItem(models.Model):
    produto = models.ForeignKey(
        Produto, on_delete=models.CASCADE,
        verbose_name='Referência')
    cor = models.ForeignKey(
        ProdutoCor, on_delete=models.CASCADE)
    tamanho = models.ForeignKey(
        ProdutoTamanho, on_delete=models.CASCADE)
    gtin_tag = models.CharField(
        max_length=13, null=True, blank=True,
        verbose_name='GTIN no TAG')
    gtin = models.CharField(
        max_length=13, null=True, blank=True,
        verbose_name='GTIN no XML')
    custo = models.DecimalField(
        decimal_places=4, max_digits=12,
        null=True, blank=True)

    def __str__(self):
        return '{} {} {}'.format(
            self.produto.referencia, self.cor.cor, self.tamanho.tamanho.nome)

    class Meta:
        db_table = "fo2_produto_item"
        verbose_name = "Item do produto"
        verbose_name_plural = "Itens do produto"

class ComposicaoLinha(models.Model):
    composicao = models.ForeignKey(
        Composicao, on_delete=models.CASCADE,
        verbose_name='Composição')
    ordem = models.IntegerField()
    linha = models.CharField(max_length=100)

    def __str__(self):
        return '({}) {}: {}'.format(self.composicao, self.ordem, self.linha)

    class Meta:
        db_table = "fo2_composicao_linha"
        verbose_name = "Linha da Composição"
        verbose_name_plural = "Linhas da Composição"
<commit_msg>validando nível de produto<commit_after>from django.core.exceptions import ValidationError
from django.db import models

from base.models import ImagemTag, Tamanho

class GtinRange(models.Model):
    ordem = models.IntegerField(
        unique=True,
        verbose_name='Ordem de adoção do range')
    pais = models.CharField(
        max_length=3,
        verbose_name='Código do país do range')
    codigo = models.CharField(
        max_length=5, unique=True,
        verbose_name='Código identificador do range')

    def __str__(self):
        return '{} - ({}){}'.format(self.ordem, self.pais, self.codigo)

    class Meta:
        db_table = "fo2_gtin_range"
        verbose_name = "Range de GTIN"

class Composicao(models.Model):
    descricao = models.CharField(
        'descrição',
        max_length=200)

    def __str__(self):
        return self.descricao

    class Meta:
        db_table = "fo2_composicao"
        verbose_name = "Composição"
        verbose_name_plural = "Composições"

def validate_produto_nivel(value):
    if value in [1, 2, 9]:
        return value
    else:
        raise ValidationError("Nível deve ser 1, 2 ou 9")

class Produto(models.Model):
    nivel = models.IntegerField(
        db_index=True, validators=[validate_produto_nivel],
        verbose_name='Nível')
    referencia = models.CharField(
        db_index=True, max_length=5,
        verbose_name='Referência')
    descricao = models.CharField(
        'descrição', default='',
        max_length=200)
    ativo = models.BooleanField(default=True)
    imp_cod = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Código impresso')
    composicao = models.ForeignKey(
        Composicao, on_delete=models.SET_NULL, null=True, blank=True,
        verbose_name='Composição')
    imagem_tag = models.ForeignKey(
        ImagemTag, on_delete=models.SET_NULL, null=True, blank=True,
        verbose_name='Imagem no TAG')
    cor_no_tag = models.BooleanField(
        default=True, verbose_name='Imprime cor no TAG')
    ncm = models.CharField(
        max_length=8, default='',
        verbose_name='NCM')
    unidade = models.CharField(
        max_length=2, default='')

    def __str__(self):
        ativo = '' if self.ativo else '--'
        return '{}{}'.format(ativo, self.referencia)

    class Meta:
        db_table = "fo2_produto"
        verbose_name = "Produto"

class ProdutoCor(models.Model):
    produto = models.ForeignKey(
        Produto, on_delete=models.CASCADE,
        verbose_name='Referência')
    cor = models.CharField(
        db_index=True, max_length=6, verbose_name='Cor')
    descricao = models.CharField(
        'descrição', default='',
        max_length=200)
    ativa = models.BooleanField(default=True)
    imp_cod = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Código impresso')
    imp_descr = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Descrição impressa')
    composicao = models.ForeignKey(
        Composicao, on_delete=models.SET_NULL, null=True, blank=True,
        verbose_name='Composição')

    def __str__(self):
        ativa = '' if self.ativa else '--'
        return '{}({}) {}'.format(ativa, self.produto, self.cor)

    class Meta:
        db_table = "fo2_produto_cor"
        verbose_name = "Cor de um produto"
        verbose_name_plural = "Cores de um produto"

class ProdutoTamanho(models.Model):
    produto = models.ForeignKey(
        Produto, on_delete=models.CASCADE,
        verbose_name='Referência')
    tamanho = models.ForeignKey(
        Tamanho, on_delete=models.PROTECT)
    descricao = models.CharField(
        'descrição', default='',
        max_length=200)
    ativo = models.BooleanField(default=True)
    imp_cod = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Código impresso')
    imp_descr = models.CharField(
        max_length=6, null=True, blank=True,
        verbose_name='Descrição impressa')

    def __str__(self):
        ativo = '' if self.ativo else '--'
        return '{}({}) {}'.format(ativo, self.produto, self.tamanho.nome)

    class Meta:
        db_table = "fo2_produto_tamanho"
        verbose_name = "Tamanho de um produto"
        verbose_name_plural = "Tamanhos de um produto"

class ProdutoItem(models.Model):
    produto = models.ForeignKey(
        Produto, on_delete=models.CASCADE,
        verbose_name='Referência')
    cor = models.ForeignKey(
        ProdutoCor, on_delete=models.CASCADE)
    tamanho = models.ForeignKey(
        ProdutoTamanho, on_delete=models.CASCADE)
    gtin_tag = models.CharField(
        max_length=13, null=True, blank=True,
        verbose_name='GTIN no TAG')
    gtin = models.CharField(
        max_length=13, null=True, blank=True,
        verbose_name='GTIN no XML')
    custo = models.DecimalField(
        decimal_places=4, max_digits=12,
        null=True, blank=True)

    def __str__(self):
        return '{} {} {}'.format(
            self.produto.referencia, self.cor.cor, self.tamanho.tamanho.nome)

    class Meta:
        db_table = "fo2_produto_item"
        verbose_name = "Item do produto"
        verbose_name_plural = "Itens do produto"

class ComposicaoLinha(models.Model):
    composicao = models.ForeignKey(
        Composicao, on_delete=models.CASCADE,
        verbose_name='Composição')
    ordem = models.IntegerField()
    linha = models.CharField(max_length=100)

    def __str__(self):
        return '({}) {}: {}'.format(self.composicao, self.ordem, self.linha)

    class Meta:
        db_table = "fo2_composicao_linha"
        verbose_name = "Linha da Composição"
        verbose_name_plural = "Linhas da Composição"
<|endoftext|>


<commit_before>from itertools import chain
import re

from marathoner.contest.base import BaseContest
from marathoner.scores import Score

class Contest(BaseContest):
    score_re = re.compile(r'^Score\s*=\s*(\d+(?:\.\d+)?)\s*$')
    run_time_re = re.compile(r'^Run time\s*=\s*(\d+(?:\.\d+)?)\s*$')

    def extract_score(self, seed, visualizer_stdout, solution_stderr):
        '''From visualizer standard output and solution error output
        extract score and run time information.'''
        score, run_time = None, 0.0
        for line in chain(visualizer_stdout, solution_stderr):
            score_match = self.score_re.match(line)
            if score_match:
                score = float(score_match.group(1))
            run_time_match = self.run_time_re.match(line)
            if run_time_match:
                run_time = float(run_time_match.group(1))
        if score is None:
            raise RuntimeError('Unable to extract score from seed %s' % seed)
        return Score(seed, score, run_time)

    def single_test_starting(self, seed):
        print 'Running single test %s...' % seed

    def single_test_ending(self, seed, visualizer_stdout, solution_stderr, best_score, current_score):
        print '\tRun time: %f' % current_score.run_time
        print '\tNew score: %f' % current_score.score
        print '\tOld score: %f' % best_score.score

        if not best_score.score or not current_score.score:
            return

        if self.maximize:
            ratio = 100.0 * (current_score.score - best_score.score) / best_score.score
        else:
            ratio = 100.0 * (best_score.score - current_score.score) / best_score.score
        if ratio >= 0:
            print '\t+%.2f%% improvement' % ratio
        else:
            print '\t%.2f%% decreese' % ratio

    def multiple_tests_starting(self, num_tests):
        print 'Running %s tests...' % num_tests
        self.sum_best = self.num_best = 0
        self.sum_current = self.num_current = 0

    def one_test_starting(self, seed):
        pass

    def one_test_ending(self, seed, visualizer_stdout, solution_stderr, best_score, current_score):
        self.sum_best += best_score.score
        if best_score.score:
            self.num_best += 1
        self.sum_current += current_score.score
        if current_score.score:
            self.num_current += 1

        seed_str = 'Seed %s:' % seed
        run_time_str = 'Run time: %.2f' % current_score.run_time

        if not best_score.score or not current_score.score:
            score_str = 'Score: %.2f' % current_score.score
        else:
            if self.maximize:
                ratio = 100.0 * (current_score.score - best_score.score) / best_score.score
            else:
                ratio = 100.0 * (best_score.score - current_score.score) / best_score.score

            if ratio >= 0:
                score_str = 'Score: %-9.2f (+%.2f%%)' % (current_score.score, ratio)
            else:
                score_str = 'Score: %-9.2f (%.2f%%)' % (current_score.score, ratio)

        print '%-10s %-28s %s' % (seed_str, score_str, run_time_str)

    def multiple_tests_ending(self, num_tests):
        print
        print 'Average new score for %s seeds: %.2f' % (self.num_current, self.sum_current / self.num_current)
        if self.num_best:
            print 'Average best score for %s seeds: %.2f' % (self.num_best, self.sum_best / self.num_best)
<commit_msg>Save run from multiple tests into .log file. Got rid of average score message from multiple tests - it is useless.<commit_after>from itertools import chain
from os import path
import re

from marathoner.contest.base import BaseContest
from marathoner.scores import Score

class Contest(BaseContest):
    score_re = re.compile(r'^Score\s*=\s*(\d+(?:\.\d+)?)\s*$')
    run_time_re = re.compile(r'^Run time\s*=\s*(\d+(?:\.\d+)?)\s*$')

    def extract_score(self, seed, visualizer_stdout, solution_stderr):
        '''From visualizer standard output and solution error output
        extract score and run time information.'''
        score, run_time = None, 0.0
        for line in chain(visualizer_stdout, solution_stderr):
            score_match = self.score_re.match(line)
            if score_match:
                score = float(score_match.group(1))
            run_time_match = self.run_time_re.match(line)
            if run_time_match:
                run_time = float(run_time_match.group(1))
        if score is None:
            raise RuntimeError('Unable to extract score from seed %s' % seed)
        return Score(seed, score, run_time)

    def single_test_starting(self, seed):
        print 'Running single test %s...' % seed

    def single_test_ending(self, seed, visualizer_stdout, solution_stderr, best_score, current_score):
        print '\tRun time: %f' % current_score.run_time
        print '\tNew score: %f' % current_score.score
        print '\tOld score: %f' % best_score.score

        if not best_score.score or not current_score.score:
            return

        if self.maximize:
            ratio = 100.0 * (current_score.score - best_score.score) / best_score.score
        else:
            ratio = 100.0 * (best_score.score - current_score.score) / best_score.score
        if ratio >= 0:
            print '\t+%.2f%% improvement' % ratio
        else:
            print '\t%.2f%% decreese' % ratio

    def multiple_tests_starting(self, num_tests):
        print 'Running %s tests...' % num_tests
        log_filename = path.join(self.project.project_dir, 'multiple_tests.log')
        self.log_file = open(log_filename, 'wb')

    def one_test_starting(self, seed):
        pass

    def one_test_ending(self, seed, visualizer_stdout, solution_stderr, best_score, current_score):
        seed_str = 'Seed %s:' % seed
        run_time_str = 'Run time: %.2f' % current_score.run_time

        if not best_score.score or not current_score.score:
            score_str = 'Score: %.2f' % current_score.score
        else:
            if self.maximize:
                ratio = 100.0 * (current_score.score - best_score.score) / best_score.score
            else:
                ratio = 100.0 * (best_score.score - current_score.score) / best_score.score

            if ratio >= 0:
                score_str = 'Score: %-9.2f (+%.2f%%)' % (current_score.score, ratio)
            else:
                score_str = 'Score: %-9.2f (%.2f%%)' % (current_score.score, ratio)

        s = '%-10s %-28s %s' % (seed_str, score_str, run_time_str)
        self.log_file.write(s + '\n')
        self.log_file.flush()
        print s

    def multiple_tests_ending(self, num_tests):
        self.log_file.close()
<|endoftext|>