<commit_before>from __future__ import unicode_literals

from ...language import Language
from ...compat import pickle, unicode_

def test_pickle_single_doc():
    nlp = Language()
    doc = nlp(u'pickle roundtrip')
    data = pickle.dumps(doc, 1)
    doc2 = pickle.loads(data)
    assert doc2.text == 'pickle roundtrip'

def test_list_of_docs_pickles_efficiently():
    nlp = Language()
    for i in range(10000):
        _ = nlp.vocab[unicode_(i)]
    one_pickled = pickle.dumps(nlp(u'0'), -1)
    docs = list(nlp.pipe(unicode_(i) for i in range(100)))
    many_pickled = pickle.dumps(docs, -1)
    assert len(many_pickled) < (len(one_pickled) * 2)
    many_unpickled = pickle.loads(many_pickled)
    assert many_unpickled[0].text == '0'
    assert many_unpickled[-1].text == '99'
    assert len(many_unpickled) == 100
<commit_msg>Test user_data deserialization, re #1085<commit_after>from __future__ import unicode_literals

from ...language import Language
from ...compat import pickle, unicode_

def test_pickle_single_doc():
    nlp = Language()
    doc = nlp(u'pickle roundtrip')
    data = pickle.dumps(doc, 1)
    doc2 = pickle.loads(data)
    assert doc2.text == 'pickle roundtrip'

def test_list_of_docs_pickles_efficiently():
    nlp = Language()
    for i in range(10000):
        _ = nlp.vocab[unicode_(i)]
    one_pickled = pickle.dumps(nlp(u'0'), -1)
    docs = list(nlp.pipe(unicode_(i) for i in range(100)))
    many_pickled = pickle.dumps(docs, -1)
    assert len(many_pickled) < (len(one_pickled) * 2)
    many_unpickled = pickle.loads(many_pickled)
    assert many_unpickled[0].text == '0'
    assert many_unpickled[-1].text == '99'
    assert len(many_unpickled) == 100

def test_user_data_from_disk():
    nlp = Language()
    doc = nlp(u'Hello')
    doc.user_data[(0, 1)] = False
    b = doc.to_bytes()
    doc2 = doc.__class__(doc.vocab).from_bytes(b)
    assert doc2.user_data[(0, 1)] == False

def test_user_data_unpickles():
    nlp = Language()
    doc = nlp(u'Hello')
    doc.user_data[(0, 1)] = False
    b = pickle.dumps(doc)
    doc2 = pickle.loads(b)
    assert doc2.user_data[(0, 1)] == False
<|endoftext|>


<commit_before>from rest_framework import serializers

from .models import (
    ETDRSGridAnnotation,
    MeasurementAnnotation,
    BooleanClassificationAnnotation,
    PolygonAnnotationSet,
    SinglePolygonAnnotation,
    LandmarkAnnotationSet,
    SingleLandmarkAnnotation,
)
from .validators import validate_grader_is_current_retina_user

class ETDRSGridAnnotationSerializer(serializers.ModelSerializer):
    class Meta:
        model = ETDRSGridAnnotation
        fields = ("grader", "created", "image", "fovea", "optic_disk")

class MeasurementAnnotationSerializer(serializers.ModelSerializer):
    class Meta:
        model = MeasurementAnnotation
        fields = ("image", "grader", "created", "start_voxel", "end_voxel")

class BooleanClassificationAnnotationSerializer(serializers.ModelSerializer):
    class Meta:
        model = BooleanClassificationAnnotation
        fields = ("image", "grader", "created", "name", "value")

class SinglePolygonAnnotationSerializer(serializers.ModelSerializer):
    annotation_set = serializers.PrimaryKeyRelatedField(
        queryset=PolygonAnnotationSet.objects.all()
    )

    def validate(self, data):
        """
        Validate that the user that is creating this object equals the annotation_set.grader for retina_graders
        """
        validate_grader_is_current_retina_user(data["annotation_set"].grader, self.context)
        return data

    class Meta:
        model = SinglePolygonAnnotation
        fields = ("id", "value", "annotation_set")

class PolygonAnnotationSetSerializer(serializers.ModelSerializer):
    singlepolygonannotation_set = SinglePolygonAnnotationSerializer(
        many=True, read_only=True
    )

    def validate_grader(self, value):
        """
        Validate that grader is the user creating the object for retina_graders group
        """
        validate_grader_is_current_retina_user(value, self.context)
        return value

    class Meta:
        model = PolygonAnnotationSet
        fields = (
            "id",
            "image",
            "grader",
            "created",
            "name",
            "singlepolygonannotation_set",
        )

class LandmarkAnnotationSetSerializer(serializers.ModelSerializer):
    class Meta:
        model = LandmarkAnnotationSet
        fields = ("grader", "created")

class SingleLandmarkAnnotationSerializer(serializers.ModelSerializer):
    class Meta:
        model = SingleLandmarkAnnotation
        fields = ("image", "annotation_set", "landmarks")
<commit_msg>Move validators to abstract serializers to implement in all annotation models<commit_after>from rest_framework import serializers

from .models import (
    ETDRSGridAnnotation,
    MeasurementAnnotation,
    BooleanClassificationAnnotation,
    PolygonAnnotationSet,
    SinglePolygonAnnotation,
    LandmarkAnnotationSet,
    SingleLandmarkAnnotation,
)
from .validators import validate_grader_is_current_retina_user

class AbstractAnnotationSerializer(serializers.ModelSerializer):
    def validate_grader(self, value):
        """
        Validate that grader is the user creating the object for retina_graders group
        """
        validate_grader_is_current_retina_user(value, self.context)
        return value

    class Meta:
        abstract = True

class AbstractSingleAnnotationSerializer(serializers.ModelSerializer):
    def validate(self, data):
        """
        Validate that the user that is creating this object equals the annotation_set.grader for retina_graders
        """
        validate_grader_is_current_retina_user(data["annotation_set"].grader, self.context)
        return data

    class Meta:
        abstract = True

class ETDRSGridAnnotationSerializer(AbstractAnnotationSerializer):
    class Meta:
        model = ETDRSGridAnnotation
        fields = ("grader", "created", "image", "fovea", "optic_disk")

class MeasurementAnnotationSerializer(AbstractAnnotationSerializer):
    class Meta:
        model = MeasurementAnnotation
        fields = ("image", "grader", "created", "start_voxel", "end_voxel")

class BooleanClassificationAnnotationSerializer(AbstractAnnotationSerializer):
    class Meta:
        model = BooleanClassificationAnnotation
        fields = ("image", "grader", "created", "name", "value")

class SinglePolygonAnnotationSerializer(AbstractSingleAnnotationSerializer):
    annotation_set = serializers.PrimaryKeyRelatedField(
        queryset=PolygonAnnotationSet.objects.all()
    )

    class Meta:
        model = SinglePolygonAnnotation
        fields = ("id", "value", "annotation_set")

class PolygonAnnotationSetSerializer(AbstractAnnotationSerializer):
    singlepolygonannotation_set = SinglePolygonAnnotationSerializer(
        many=True, read_only=True
    )

    class Meta:
        model = PolygonAnnotationSet
        fields = (
            "id",
            "image",
            "grader",
            "created",
            "name",
            "singlepolygonannotation_set",
        )

class LandmarkAnnotationSetSerializer(AbstractAnnotationSerializer):
    class Meta:
        model = LandmarkAnnotationSet
        fields = ("grader", "created")

class SingleLandmarkAnnotationSerializer(AbstractSingleAnnotationSerializer):
    class Meta:
        model = SingleLandmarkAnnotation
        fields = ("image", "annotation_set", "landmarks")
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-16.html#%_thm_2.66
"""
import random
from itertools import repeat

from Chapter2.exercises.exercise2_63 import tree_to_list_2
from Chapter2.exercises.exercise2_64 import list_to_tree
from Chapter2.themes.lisp_list_structured_data import car, lisp_list, nil, cdr
from Chapter2.themes.sets_as_binary_trees import (
    element_of_set, make_tree,
    entry,
    left_branch,
    right_branch,
)
from Chapter2.themes.mapping_over_trees import map
from utils import repeatfunc, let

def lookup(given_key, set_of_records):
    """Lookup a given_key in records, where records are implemented as binary
    tree sets"""
    if set_of_records is nil:
        return nil
    with let(entry(set_of_records)) as (top_node,):
        with let(key(top_node)) as (top_node_key,):
            if given_key == top_node_key:
                return top_node
            if given_key < top_node_key:
                return lookup(given_key, left_branch(set_of_records))
            if given_key > top_node_key:
                return lookup(given_key, right_branch(set_of_records))

def key(record):
    """Retrieve the key that uniquely identifies the record (we implement a
    record as a Python dict object. See the body of run_the_magic)"""
    return record['key']

def run_the_magic():
    records = make_tree(
        {'key': 5, 'value': 'item1'},
        make_tree(
            {'key': 2, 'value': 'item2'},
            make_tree(
                {'key': 1, 'value': 'item3'},
                {'key': 0, 'value': 'item9'},
                nil
            ),
            {'key': 3, 'value': 'item6'},
        ),
        make_tree(
            {'key': 40, 'value': 'item134'},
            {'key': 6, 'value': 'item43'},
            make_tree(
                {'key': 78, 'value': 'item97'},
                {'key': 60, 'value': 'item10'},
                {'key': 80, 'value': 'item120'},
            ),
        )
    )

    def get_given_key(records_as_list):
        """Utility function to get keys from the records"""
        if records_as_list is nil:
            return
        yield car(records_as_list)
        yield from get_given_key(cdr(records_as_list))

    for given_key in get_given_key(map(key, tree_to_list_2(records))):
        print('Looking for key: {}  -->  '.format(given_key), end='')
        print(lookup(given_key, list_to_tree(tree_to_list_2(records))))

if __name__ == "__main__":
    run_the_magic()

<commit_msg>removes unused imports in exercise 2_66<commit_after># -*- coding: utf-8 -*-
"""
https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-16.html#%_thm_2.66
"""

from Chapter2.exercises.exercise2_63 import tree_to_list_2
from Chapter2.exercises.exercise2_64 import list_to_tree
from Chapter2.themes.lisp_list_structured_data import car, cdr, nil
from Chapter2.themes.mapping_over_trees import map
from Chapter2.themes.sets_as_binary_trees import (
    entry, left_branch, make_tree, right_branch,
)
from utils import let

def lookup(given_key, set_of_records):
    """Lookup a given_key in records, where records are implemented as binary
    tree sets"""
    if set_of_records is nil:
        return nil
    with let(entry(set_of_records)) as (top_node,):
        with let(key(top_node)) as (top_node_key,):
            if given_key == top_node_key:
                return top_node
            if given_key < top_node_key:
                return lookup(given_key, left_branch(set_of_records))
            if given_key > top_node_key:
                return lookup(given_key, right_branch(set_of_records))

def key(record):
    """Retrieve the key that uniquely identifies the record (we implement a
    record as a Python dict object. See the body of run_the_magic)"""
    return record['key']

def run_the_magic():
    records = make_tree(
        {'key': 5, 'value': 'item1'},
        make_tree(
            {'key': 2, 'value': 'item2'},
            make_tree(
                {'key': 1, 'value': 'item3'},
                {'key': 0, 'value': 'item9'},
                nil
            ),
            {'key': 3, 'value': 'item6'},
        ),
        make_tree(
            {'key': 40, 'value': 'item134'},
            {'key': 6, 'value': 'item43'},
            make_tree(
                {'key': 78, 'value': 'item97'},
                {'key': 60, 'value': 'item10'},
                {'key': 80, 'value': 'item120'},
            ),
        )
    )

    def get_given_key(records_as_list):
        """Utility function to get keys from the records"""
        if records_as_list is nil:
            return
        yield car(records_as_list)
        yield from get_given_key(cdr(records_as_list))

    for given_key in get_given_key(map(key, tree_to_list_2(records))):
        print('Looking for key: {}  -->  '.format(given_key), end='')
        print(lookup(given_key, list_to_tree(tree_to_list_2(records))))

if __name__ == "__main__":
    run_the_magic()

<|endoftext|>


<commit_before>import datetime
import os
import pickle
from socket import (socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR,
                    error as sockerror)
from threading import Thread

from settings import HIVEHOST, HIVEPORT, HIVELOGIN, HIVEPASS
from faces import faces
from httphandler import HTTPRequest
from myenc import AESCipher
from bearstorage import BearStorage
from server import DumpToFile

def send_report(data, client, password):
    ciper = AESCipher(password)
    message = client + ":"
    message += ciper.encrypt(pickle.dumps(data))
    s = socket(AF_INET, SOCK_STREAM)
    try:
        s.connect((HIVEHOST, HIVEPORT))
        s.sendall(message)
        response = s.recv(1024)
        s.close()
    except sockerror:
        DumpToFile(data)
        print "Hive server is not responding :("
    print response

def compile_banner(msgsize=0,
                   code="HTTP/1.1 200 OK",
                   serverver="Apache/1.3.42 (Unix)  (Red Hat/Linux)  "
                             "OpenSSL/1.0.1e PHP/5.5.9 ",
                   contenttype='text/html; charset=UTF-8',
                   connection="close",
                   date=str(datetime.datetime.now()),
                   nlcount=2):
    """
    This function creates an HTTP banner and returns it as string. Works well
    with default arguments in most faces, any of them can be overridden.
    `msgsize` needs to be equal to len() of the content string (will work with
    incorrect values in some browsers, but not in YaBrowser, so maybe in all
    of the chrome based browsers).
    `nlcount` is the number of blank lines in the end of server banner. Number
    of the lines depends on Content-Type of the response. Should be:
    2 - `text/html`; 1 - `application/xml`
    """
    banner = ""
    if code != 0:
        banner += code + '\r\n'
    if serverver != 0:
        banner += 'Server: ' + serverver + '\r\n'
    if contenttype != 0:
        banner += 'Content-Type: ' + contenttype + '\r\n'
    if connection != 0:
        banner += 'Connection: ' + connection + '\r\n'
    if date != 0:
        banner += 'Date: ' + date + '\r\n'
    if msgsize != 0:
        banner += 'Content-Length: ' + str(msgsize)
    for i in range(nlcount):
        banner += '\r\n'
    return banner

def get_honey_http(args, request, ip_addr):
    """
    This is the place where magic happens. Function receives parsed HTTP
    request as an argument and returns an output as a string. If it
    is kind of static content, its being read from responses/. In some kind of
    harder case i use if-else to determine which code should i use. As an
    example, WEBDAV protocol uses different server banner and Content-Type of
    robots.txt should be text/plain(they are also dynamically generated).
    """
    outputdata = ""
    stringfile = ""
    if request.path in faces:  # If we know what to do with request
        respfilename = faces[request.path]
        if respfilename == "webdav.xml":  # Compile response for WEBDAV listing
            with file('responses/' + faces[request.path]) as f:
                stringfile = f.read()
            outputdata += compile_banner(code='HTTP/1.1 207 Multi-Status',
                                         contenttype='application/xml; '
                                         'charset=utf-8',
                                         connection=0, date=0, serverver=0,
                                         nlcount=1)
            outputdata += stringfile
        elif respfilename == "robots":  # Generate robots.txt from faces dict
            stringfile = 'User-Agent: *\r\nAllow: /\r\n'
            for url in known_faces:
                stringfile += 'Disallow: ' + url + "\r\n"
            outputdata += compile_banner(msgsize=len(stringfile),
                                         contenttype="text/plain"
                                         "; charset=UTF-8")
            outputdata += stringfile
        else:  # If our request doesnt require special treatment, it goes here
            with file('responses/' + faces[request.path]) as f:
                stringfile = f.read()
            outputdata += compile_banner(msgsize=len(stringfile))
            outputdata += stringfile
        if args.verbose:
            print ip_addr + " " + request.path + " gotcha!"
    else:  # If we dont know what to do with that request
        if args.verbose:
            print ip_addr + " " + request.path[:50] + " not detected..."
        if request.path not in unknown_faces:
            # Lets add him to our todo list ;) (if we dont already have it)
            unknown_faces.append(request.path)
            with open("local_faces.txt", "a") as f:
                f.write(request.path + "\n")
        # Send default response
        with file('responses/' + faces["zero"]) as f:
            stringfile = f.read()
        outputdata += compile_banner(msgsize=len(stringfile))
        outputdata += stringfile
    return outputdata, request.path in faces

def main(args, update_event):
    # Get our unimplemented requests list, so we can add something to it
    global unknown_faces
    if not os.path.isfile("local_faces.txt"):
        f = file("local_faces.txt", "w")
        f.close()
    unknown_faces = [line.rstrip('\n') for line in open('faces.txt')]
    unknown_faces += [line.rstrip('\n') for line in open('local_faces.txt')]
    # Create a known_faces set, so we can generate robots.txt
    global known_faces
    known_faces = set()
    for url in faces:
        known_faces.add(url)
    # Create socket
    serverSocket = socket(AF_INET, SOCK_STREAM)
    # Need to set setsockopt to resolve "port already in use" issue
    serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    # Bind our socket to a port
    # Will use one from settings, if not given in args
    serverSocket.bind(('', args.client))
    serverSocket.listen(1)
    if args.verbose:
        print "Serving honey on port %s" % args.client
    # Endless loop for handling requests
    while True:
        if update_event.is_set():
            break
        try:
            connectionSocket, addr = serverSocket.accept()
        except KeyboardInterrupt:
            if 'connectionSocket' in locals():
                connectionSocket.close()
            break
        # Need to use try, because socket will generate a lot of exceptions
        try:
            # Argument is the number of bytes to recieve from client
            message = connectionSocket.recv(4000)
        except sockerror:
            print "Failed to recieve data from bot"
            continue
        ip_addr = connectionSocket.getpeername()[0]
        dt = str(datetime.datetime.utcnow())
        # Try to parse request parameters from message
        request = HTTPRequest(message)
        if request.error_code is None:
            if hasattr(request, 'path'):
                outputdata, detected = get_honey_http(
                    args, request, ip_addr)
            else:
                outputdata = message
                detected = -1
        # If it's not an HTTP request, it goes here
        else:
            if args.verbose:
                print "Got non-http request"
            detected = -2
            outputdata = message
        bs = BearStorage(ip_addr, message,
                         dt,
                         request, detected, HIVELOGIN)
        try:
            connectionSocket.send(outputdata)
            connectionSocket.close()
        except sockerror:
            print "Failed to send response to bot"
            continue
        response = Thread(
            args=(bs, HIVELOGIN, HIVEPASS,),
            target=send_report,
            )
        response.start()
        response.join()
    serverSocket.close()
<commit_msg>Change prints back to returns<commit_after>import datetime
import os
import pickle
from socket import (socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_REUSEADDR,
                    error as sockerror)
from threading import Thread

from settings import HIVEHOST, HIVEPORT, HIVELOGIN, HIVEPASS
from faces import faces
from httphandler import HTTPRequest
from myenc import AESCipher
from bearstorage import BearStorage
from server import DumpToFile

def send_report(data, client, password):
    ciper = AESCipher(password)
    message = client + ":"
    message += ciper.encrypt(pickle.dumps(data))
    s = socket(AF_INET, SOCK_STREAM)
    try:
        s.connect((HIVEHOST, HIVEPORT))
        s.sendall(message)
        response = s.recv(1024)
        s.close()
    except sockerror:
        DumpToFile(data)
        return "Hive server is not responding :("
    return response

def compile_banner(msgsize=0,
                   code="HTTP/1.1 200 OK",
                   serverver="Apache/1.3.42 (Unix)  (Red Hat/Linux)  "
                             "OpenSSL/1.0.1e PHP/5.5.9 ",
                   contenttype='text/html; charset=UTF-8',
                   connection="close",
                   date=str(datetime.datetime.now()),
                   nlcount=2):
    """
    This function creates an HTTP banner and returns it as string. Works well
    with default arguments in most faces, any of them can be overridden.
    `msgsize` needs to be equal to len() of the content string (will work with
    incorrect values in some browsers, but not in YaBrowser, so maybe in all
    of the chrome based browsers).
    `nlcount` is the number of blank lines in the end of server banner. Number
    of the lines depends on Content-Type of the response. Should be:
    2 - `text/html`; 1 - `application/xml`
    """
    banner = ""
    if code != 0:
        banner += code + '\r\n'
    if serverver != 0:
        banner += 'Server: ' + serverver + '\r\n'
    if contenttype != 0:
        banner += 'Content-Type: ' + contenttype + '\r\n'
    if connection != 0:
        banner += 'Connection: ' + connection + '\r\n'
    if date != 0:
        banner += 'Date: ' + date + '\r\n'
    if msgsize != 0:
        banner += 'Content-Length: ' + str(msgsize)
    for i in range(nlcount):
        banner += '\r\n'
    return banner

def get_honey_http(args, request, ip_addr):
    """
    This is the place where magic happens. Function receives parsed HTTP
    request as an argument and returns an output as a string. If it
    is kind of static content, its being read from responses/. In some kind of
    harder case i use if-else to determine which code should i use. As an
    example, WEBDAV protocol uses different server banner and Content-Type of
    robots.txt should be text/plain(they are also dynamically generated).
    """
    outputdata = ""
    stringfile = ""
    if request.path in faces:  # If we know what to do with request
        respfilename = faces[request.path]
        if respfilename == "webdav.xml":  # Compile response for WEBDAV listing
            with file('responses/' + faces[request.path]) as f:
                stringfile = f.read()
            outputdata += compile_banner(code='HTTP/1.1 207 Multi-Status',
                                         contenttype='application/xml; '
                                         'charset=utf-8',
                                         connection=0, date=0, serverver=0,
                                         nlcount=1)
            outputdata += stringfile
        elif respfilename == "robots":  # Generate robots.txt from faces dict
            stringfile = 'User-Agent: *\r\nAllow: /\r\n'
            for url in known_faces:
                stringfile += 'Disallow: ' + url + "\r\n"
            outputdata += compile_banner(msgsize=len(stringfile),
                                         contenttype="text/plain"
                                         "; charset=UTF-8")
            outputdata += stringfile
        else:  # If our request doesnt require special treatment, it goes here
            with file('responses/' + faces[request.path]) as f:
                stringfile = f.read()
            outputdata += compile_banner(msgsize=len(stringfile))
            outputdata += stringfile
        if args.verbose:
            print ip_addr + " " + request.path + " gotcha!"
    else:  # If we dont know what to do with that request
        if args.verbose:
            print ip_addr + " " + request.path[:50] + " not detected..."
        if request.path not in unknown_faces:
            # Lets add him to our todo list ;) (if we dont already have it)
            unknown_faces.append(request.path)
            with open("local_faces.txt", "a") as f:
                f.write(request.path + "\n")
        # Send default response
        with file('responses/' + faces["zero"]) as f:
            stringfile = f.read()
        outputdata += compile_banner(msgsize=len(stringfile))
        outputdata += stringfile
    return outputdata, request.path in faces

def main(args, update_event):
    # Get our unimplemented requests list, so we can add something to it
    global unknown_faces
    if not os.path.isfile("local_faces.txt"):
        f = file("local_faces.txt", "w")
        f.close()
    unknown_faces = [line.rstrip('\n') for line in open('faces.txt')]
    unknown_faces += [line.rstrip('\n') for line in open('local_faces.txt')]
    # Create a known_faces set, so we can generate robots.txt
    global known_faces
    known_faces = set()
    for url in faces:
        known_faces.add(url)
    # Create socket
    serverSocket = socket(AF_INET, SOCK_STREAM)
    # Need to set setsockopt to resolve "port already in use" issue
    serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    # Bind our socket to a port
    # Will use one from settings, if not given in args
    serverSocket.bind(('', args.client))
    serverSocket.listen(1)
    if args.verbose:
        print "Serving honey on port %s" % args.client
    # Endless loop for handling requests
    while True:
        if update_event.is_set():
            break
        try:
            connectionSocket, addr = serverSocket.accept()
        except KeyboardInterrupt:
            if 'connectionSocket' in locals():
                connectionSocket.close()
            break
        # Need to use try, because socket will generate a lot of exceptions
        try:
            # Argument is the number of bytes to recieve from client
            message = connectionSocket.recv(4000)
        except sockerror:
            print "Failed to recieve data from bot"
            continue
        ip_addr = connectionSocket.getpeername()[0]
        dt = str(datetime.datetime.utcnow())
        # Try to parse request parameters from message
        request = HTTPRequest(message)
        if request.error_code is None:
            if hasattr(request, 'path'):
                outputdata, detected = get_honey_http(
                    args, request, ip_addr)
            else:
                outputdata = message
                detected = -1
        # If it's not an HTTP request, it goes here
        else:
            if args.verbose:
                print "Got non-http request"
            detected = -2
            outputdata = message
        bs = BearStorage(ip_addr, message,
                         dt,
                         request, detected, HIVELOGIN)
        try:
            connectionSocket.send(outputdata)
            connectionSocket.close()
        except sockerror:
            print "Failed to send response to bot"
            continue
        response = Thread(
            args=(bs, HIVELOGIN, HIVEPASS,),
            target=send_report,
            )
        response.start()
        response.join()
    serverSocket.close()
<|endoftext|>


<commit_before>import types
import textwrap
import inspect
import sys
import importlib
import simplejson as json
import time
import cmd
import docopt
import yaml
import subprocess
import getpass
from bson.json_util import dumps
from cmd3.shell import command
from cloudmesh.user.cm_user import cm_user
from cloudmesh.cm_mongo import cm_mongo
from cloudmesh.config.cm_config import cm_config
from pprint import pprint
from prettytable import PrettyTable
from cm_shell_defaults import cm_shell_defaults

from cloudmesh_common.logger import LOGGER

log = LOGGER(__file__)

class cm_shell_register:

    def activate_cm_shell_register(self):
        self.register_command_topic('cloud','register')
        pass

    @command
    def do_reg(self, args, arguments):
        '''
        Usage:
          reg NOTIMPLEMENTED [options] NAME

        Arguments:
          NAME      Name of the cloud to be registered

        Options:
          -a --act      Activate the cloud to be registered
          -d --deact    Deactivate the cloud
        '''

        config = cm_config()
        cm_user_id = config.username()
        user_obj = cm_user()
        user = user_obj.info(cm_user_id)

        cloudtypes = {}
        error = {}
        registered = {}

        cloudname = arguments['NAME']

        # check all the registered clouds.
        for cloud in user['defaults']['registered_clouds']:
            registered[cloud] = True
            if (cloud == cloudname and not (arguments['--act'] or
                                            arguments['--deact'])):
                print "Cloud {0} is already registered.".format(cloud)
                return

        for cloud in config.get("cloudmesh.clouds"):
            cloudtypes[cloud] = \
                config['cloudmesh']['clouds'][cloud]['cm_type']

        # get credentials from cm_user -- files.
        credentials = user_obj.get_credentials(cm_user_id)

        # find if credentials for cloudname are present.
        if cloudname in credentials:
            if 'credential' in credentials[cloudname]:
                # Credentials specified
                credential = credentials[cloudname]['credential']
            else:
                # Credentials not present in files
                credentials[cloudname] = None
        else:
            print 'Please specify the right cloud name.'

        error[cloudname] = ''
        if cloudtypes[cloudname] == "openstack":
            d = {}
            # credentials not present in files.
            if credentials[cloudname] is None:
                print 'This will set credentials.'
                d = {'OS_USERNAME': cm_user_id,
                     'OS_PASSWORD': '',
                     'OS_TENANT_NAME': ''
                     }
                d['OS_PASSWORD'] = getpass.getpass("Please enter password: ")
                d['OS_TENANT_NAME'] = raw_input("Please specify the tenant: ")
                d['CM_CLOUD_TYPE'] = cloudtypes[cloudname]
                user_obj.set_credential(cm_user_id, cloudname, d)
                #

        elif cloudtypes[cloudname] == "ec2":
            error[cloudname] = ''

            d = {"CM_CLOUD_TYPE": cloudtypes[cloudname]}

            if credentials[cloudname] is None:
                d = {'EC2_URL': credential['EC2_URL'],
                     'EC2_ACCESS_KEY': credential['EC2_ACCESS_KEY'],
                     'EC2_SECRET_KEY': credential['EC2_SECRET_KEY']
                     }

                user_obj.set_credential(cm_user_id, cloudname, d)

        mongoClass = cm_mongo()
        cloud = mongoClass.get_cloud(
            cm_user_id=cm_user_id, cloud_name=cloudname, force=True)
        if cloud:
            registered[cloudname] = True
            if cloudname not in user['defaults']['registered_clouds']:
                user['defaults']['registered_clouds'].append(cloudname)
                user_obj.set_defaults(cm_user_id, user['defaults'])
        else:
            registered[cloudname] = False
            print "The cloud could not be registered."

        if registered[cloudname] is True:
            if arguments['--act']:
                try:
                    if cloudname not in user['defaults']['activeclouds']:
                        (user['defaults']['activeclouds']).append(cloudname)
                        user_obj.set_defaults(cm_user_id, user['defaults'])
                        print "Cloud {0} set to active".format(cloudname)
                except:
                    # create_dict(user, "defaults", "activeclouds")
                    log.info("ERROR user defaults activecloud does not exist")
            if arguments['--deact']:
                try:
                    if cloudname in user['defaults']['activeclouds']:
                        active = user['defaults']['activeclouds']
                        active.remove(cloudname)
                        user['defaults']['activeclouds'] = active
                        user_obj.set_defaults(cm_user_id, user['defaults'])
                        print "Cloud {0} deactived".format(cloudname)
                except:
                    # create_dict(user, "defaults", "activeclouds")
                    log.info("ERROR user defaults activecloud does not exist")

        return

def main():
    print "test correct"

if __name__ == "__main__":
    main()
<commit_msg>wrong register command<commit_after>import types
import textwrap
import inspect
import sys
import importlib
import simplejson as json
import time
import cmd
import docopt
import yaml
import subprocess
import getpass
from bson.json_util import dumps
from cmd3.shell import command
from cloudmesh.user.cm_user import cm_user
from cloudmesh.cm_mongo import cm_mongo
from cloudmesh.config.cm_config import cm_config
from pprint import pprint
from prettytable import PrettyTable
from cm_shell_defaults import cm_shell_defaults

from cloudmesh_common.logger import LOGGER

log = LOGGER(__file__)

class cm_shell_register:

    def activate_cm_shell_register(self):
        self.register_command_topic('cloud','register')
        pass

    @command
    def do_register(self, args, arguments):
        '''
        Usage:
          register [options] NAME

        Arguments:
          NAME      Name of the cloud to be registered

        Options:
          -a --act      Activate the cloud to be registered
          -d --deact    Deactivate the cloud
        '''

        config = cm_config()
        cm_user_id = config.username()
        user_obj = cm_user()
        user = user_obj.info(cm_user_id)

        cloudtypes = {}
        error = {}
        registered = {}

        cloudname = arguments['NAME']

        # check all the registered clouds.
        for cloud in user['defaults']['registered_clouds']:
            registered[cloud] = True
            if (cloud == cloudname and not (arguments['--act'] or
                                            arguments['--deact'])):
                print "Cloud {0} is already registered.".format(cloud)
                return

        for cloud in config.get("cloudmesh.clouds"):
            cloudtypes[cloud] = \
                config['cloudmesh']['clouds'][cloud]['cm_type']

        # get credentials from cm_user -- files.
        credentials = user_obj.get_credentials(cm_user_id)

        # find if credentials for cloudname are present.
        if cloudname in credentials:
            if 'credential' in credentials[cloudname]:
                # Credentials specified
                credential = credentials[cloudname]['credential']
            else:
                # Credentials not present in files
                credentials[cloudname] = None
        else:
            print 'Please specify the right cloud name.'

        error[cloudname] = ''
        if cloudtypes[cloudname] == "openstack":
            d = {}
            # credentials not present in files.
            if credentials[cloudname] is None:
                print 'This will set credentials.'
                d = {'OS_USERNAME': cm_user_id,
                     'OS_PASSWORD': '',
                     'OS_TENANT_NAME': ''
                     }
                d['OS_PASSWORD'] = getpass.getpass("Please enter password: ")
                d['OS_TENANT_NAME'] = raw_input("Please specify the tenant: ")
                d['CM_CLOUD_TYPE'] = cloudtypes[cloudname]
                user_obj.set_credential(cm_user_id, cloudname, d)
                #

        elif cloudtypes[cloudname] == "ec2":
            error[cloudname] = ''

            d = {"CM_CLOUD_TYPE": cloudtypes[cloudname]}

            if credentials[cloudname] is None:
                d = {'EC2_URL': credential['EC2_URL'],
                     'EC2_ACCESS_KEY': credential['EC2_ACCESS_KEY'],
                     'EC2_SECRET_KEY': credential['EC2_SECRET_KEY']
                     }

                user_obj.set_credential(cm_user_id, cloudname, d)

        mongoClass = cm_mongo()
        cloud = mongoClass.get_cloud(
            cm_user_id=cm_user_id, cloud_name=cloudname, force=True)
        if cloud:
            registered[cloudname] = True
            if cloudname not in user['defaults']['registered_clouds']:
                user['defaults']['registered_clouds'].append(cloudname)
                user_obj.set_defaults(cm_user_id, user['defaults'])
        else:
            registered[cloudname] = False
            print "The cloud could not be registered."

        if registered[cloudname] is True:
            if arguments['--act']:
                try:
                    if cloudname not in user['defaults']['activeclouds']:
                        (user['defaults']['activeclouds']).append(cloudname)
                        user_obj.set_defaults(cm_user_id, user['defaults'])
                        print "Cloud {0} set to active".format(cloudname)
                except:
                    # create_dict(user, "defaults", "activeclouds")
                    log.info("ERROR user defaults activecloud does not exist")
            if arguments['--deact']:
                try:
                    if cloudname in user['defaults']['activeclouds']:
                        active = user['defaults']['activeclouds']
                        active.remove(cloudname)
                        user['defaults']['activeclouds'] = active
                        user_obj.set_defaults(cm_user_id, user['defaults'])
                        print "Cloud {0} deactived".format(cloudname)
                except:
                    # create_dict(user, "defaults", "activeclouds")
                    log.info("ERROR user defaults activecloud does not exist")

        return

def main():
    print "test correct"

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before># Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: anze@reciprocitylabs.com
# Maintained By: anze@reciprocitylabs.com

from ggrc import db, settings
from .mixins import deferred, Base, Stateful
from functools import wraps
from flask import request
from flask.wrappers import Response

class Task(Base, Stateful, db.Model):
  __tablename__ = 'tasks'

  VALID_STATES = [
    "Pending",
    "Running",
    "Success",
    "Failure"
  ]
  name = deferred(db.Column(db.String), 'Task')
  parameters = deferred(db.Column(db.PickleType), 'Task')
  result = deferred(db.Column(db.PickleType), 'Task')

  _publish_attrs = [
      'name',
      'result'
  ]

  def start(self):
    self.status = "Running"
    db.session.add(self)
    db.session.commit()

  def finish(self, status, result):
    if isinstance(result, Response):

      self.result = {'content': result.response[0],
                     'status_code': result.status_code,
                     'headers': result.headers.items()}
    else:
      self.result = {'content': result,
                     'status_code': 200,
                     'headers': [('Content-Type', 'text/html')]}
    self.status = status
    db.session.add(self)
    db.session.commit()

  def make_response(self, default=None):
    if self.result == None:
      return default
    from ggrc.app import app
    return app.make_response((self.result['content'], self.result['status_code'],
                              self.result['headers']))

def create_task(name, queued_task, parameters={}):
  from time import time
  task = Task(name = name + str(int(time()))) # task name must be unique
  task.parameters = parameters
  from ggrc.app import db
  db.session.add(task)
  db.session.commit()

  # schedule a task queue
  if getattr(settings, 'APP_ENGINE', False):
    from google.appengine.api import taskqueue
    from flask import url_for
    cookie_header = [h for h in request.headers if h[0] == 'Cookie']
    taskqueue = taskqueue.add(url=url_for(queued_task.__name__), name=task.name,
                              params={'task_id': task.id},
                              headers = cookie_header)
  else:
    queued_task(task)

  return task

def make_task_response(id):
  task = Task.query.get(id)
  return task.make_response()

def queued_task(func):

  @wraps(func)
  def decorated_view(*args, **kwargs):
    if len(args) > 0 and isinstance(args[0], Task):
      task = args[0]
    else:
      task = Task.query.get(request.form.get("task_id"))
    task.start()
    try:
      result = func(task)
    except:
      import traceback
      from ggrc.app import app
      task.finish("Failure", app.make_response((
        traceback.format_exc(), 200, [('Content-Type', 'text/html')])))

      # Return 200 so that the task is not retried
      return app.make_response((
        'failure', 200, [('Content-Type', 'text/html')]))
    task.finish("Success", result)
    return result
  return decorated_view

<commit_msg>Add `db.rollback` so unintended changes don't persist<commit_after># Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: anze@reciprocitylabs.com
# Maintained By: anze@reciprocitylabs.com

from ggrc import db, settings
from .mixins import deferred, Base, Stateful
from functools import wraps
from flask import request
from flask.wrappers import Response

class Task(Base, Stateful, db.Model):
  __tablename__ = 'tasks'

  VALID_STATES = [
    "Pending",
    "Running",
    "Success",
    "Failure"
  ]
  name = deferred(db.Column(db.String), 'Task')
  parameters = deferred(db.Column(db.PickleType), 'Task')
  result = deferred(db.Column(db.PickleType), 'Task')

  _publish_attrs = [
      'name',
      'result'
  ]

  def start(self):
    self.status = "Running"
    db.session.add(self)
    db.session.commit()

  def finish(self, status, result):
    # Ensure to not commit any not-yet-committed changes
    db.session.rollback()

    if isinstance(result, Response):

      self.result = {'content': result.response[0],
                     'status_code': result.status_code,
                     'headers': result.headers.items()}
    else:
      self.result = {'content': result,
                     'status_code': 200,
                     'headers': [('Content-Type', 'text/html')]}
    self.status = status
    db.session.add(self)
    db.session.commit()

  def make_response(self, default=None):
    if self.result == None:
      return default
    from ggrc.app import app
    return app.make_response((self.result['content'], self.result['status_code'],
                              self.result['headers']))

def create_task(name, queued_task, parameters={}):
  from time import time
  task = Task(name = name + str(int(time()))) # task name must be unique
  task.parameters = parameters
  from ggrc.app import db
  db.session.add(task)
  db.session.commit()

  # schedule a task queue
  if getattr(settings, 'APP_ENGINE', False):
    from google.appengine.api import taskqueue
    from flask import url_for
    cookie_header = [h for h in request.headers if h[0] == 'Cookie']
    taskqueue = taskqueue.add(url=url_for(queued_task.__name__), name=task.name,
                              params={'task_id': task.id},
                              headers = cookie_header)
  else:
    queued_task(task)

  return task

def make_task_response(id):
  task = Task.query.get(id)
  return task.make_response()

def queued_task(func):

  @wraps(func)
  def decorated_view(*args, **kwargs):
    if len(args) > 0 and isinstance(args[0], Task):
      task = args[0]
    else:
      task = Task.query.get(request.form.get("task_id"))
    task.start()
    try:
      result = func(task)
    except:
      import traceback
      from ggrc.app import app
      task.finish("Failure", app.make_response((
        traceback.format_exc(), 200, [('Content-Type', 'text/html')])))

      # Return 200 so that the task is not retried
      return app.make_response((
        'failure', 200, [('Content-Type', 'text/html')]))
    task.finish("Success", result)
    return result
  return decorated_view

<|endoftext|>


<commit_before># Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module contains the Program Model."""

from google.appengine.ext import blobstore
from google.appengine.ext import db

from django.utils import translation

from soc.models import document as document_model
from soc.models import linkable as linkable_model
from soc.models import sponsor as sponsor_model
from soc.models import timeline as timeline_model

# Allowed values for program status.
STATUS_VISIBLE = 'visible'  # The program is visible to everyone.
STATUS_INVISIBLE = 'invisible'  # The program is visible to hosts and devs.
STATUS_INVALID = 'invalid'  # The program is not visible to anyone.
# TODO(nathaniel): Is STATUS_INVALID used or useful any more?

GENERAL_INFO_GROUP = translation.ugettext(
    '1. General Info')
CONTACT_INFO_GROUP = translation.ugettext(
    '2. Contact Info')
AGE_REQUIREMENTS_GROUP = translation.ugettext(
    '3. Age Requirements')
PROGRAM_DOCUMENTS_GROUP = translation.ugettext(
    '4. Program Documents')

class ProgramMessages(db.Model):
  """The ProgramMessages model.

  This model contains the specific messages whose content may be customized
  by program administrators and which may be sent because of various reasons
  throughout the program.
  """

  #: Message sent to the organizations that are accepted for the program.
  accepted_orgs_msg = db.TextProperty(required=False,
      verbose_name=translation.ugettext('Accepted Organizations Message'))

  #: Message sent to the organizations that are rejected for the program.
  rejected_orgs_msg = db.TextProperty(required=False,
      verbose_name=translation.ugettext('Rejected Organizations Message'))

  #: Message sent to a mentor/org admin the first time they join the program.
  mentor_welcome_msg = db.TextProperty(required=False,
      verbose_name=translation.ugettext('Mentor Welcome Message'))

# TODO(daniel): drop inheritance from Linkable
class Program(linkable_model.Linkable):
  """The Program model, representing a Program ran by a Sponsor."""

  _messages_model = ProgramMessages

  #: string used as a prefix of various key names for other models
  #TODO(daniel): eliminate this
  prefix = 'program'

  #: Identifier of the program which is be the last part of its unique key name
  program_id = db.StringProperty(required=True,
      verbose_name=translation.ugettext('Program ID'))
  program_id.help_text = translation.ugettext(
      'Used as part of various URL links throughout the site.')

  #: Reference to the sponsor of the program. Its key_name is used as the first
  #: part of program's unique key name
  sponsor = db.ReferenceProperty(required=True,
      reference_class=sponsor_model.Sponsor, collection_name='programs',
      verbose_name=translation.ugettext('Sponsor'))

  #: Required field storing name of the group.
  name = db.StringProperty(required=True,
      verbose_name=translation.ugettext('Name'))
  name.group = GENERAL_INFO_GROUP
  name.help_text = translation.ugettext(
      'Complete, formal name of the program.')

  #: Required field storing short name of the group.
  #: It can be used for displaying group as sidebar menu item.
  short_name = db.StringProperty(required=True,
      verbose_name=translation.ugettext('Short name'))
  short_name.group = GENERAL_INFO_GROUP
  short_name.help_text = translation.ugettext(
      'Short name used for sidebar menu')

  #: Required field storing description of the group.
  description = db.TextProperty(required=True,
      verbose_name=translation.ugettext('Description'))
  description.group = GENERAL_INFO_GROUP
  description.help_text = translation.ugettext(
      'for example:
'
      '<tt>GSoC 2009 is the Google Summer of Code,'
      ' but in 2009!</tt>

'
      '(rich text formatting is supported)')

  #: Number of accepted organizations
  nr_accepted_orgs = db.IntegerProperty(
      required=False, verbose_name=translation.ugettext('#accepted orgs'))
  nr_accepted_orgs.group = GENERAL_INFO_GROUP
  nr_accepted_orgs.help_text = translation.ugettext(
      'The number of accepted organizations.')

  #: Property that contains the minimum age of a student allowed to
  #: participate
  student_min_age = db.IntegerProperty(
      required=False, verbose_name=translation.ugettext('Student minimum age'))
  student_min_age.group = AGE_REQUIREMENTS_GROUP
  student_min_age.help_text = translation.ugettext(
      'Minimum age of students.')

  #: Property that contains the maximum age of a student allowed to
  #: participate
  student_max_age = db.IntegerProperty(default=200,
      required=False, verbose_name=translation.ugettext('Student maximum age'))
  student_max_age.group = AGE_REQUIREMENTS_GROUP
  student_max_age.help_text = translation.ugettext(
      'Maximum whole-year age of students.')

  #: Property that contains the date as of which above student
  #: minimum/maximum age requirement holds.
  student_min_age_as_of = db.DateProperty(
      required=False, verbose_name=translation.ugettext('Age as of'))
  student_min_age_as_of.group = AGE_REQUIREMENTS_GROUP
  student_min_age_as_of.help_text = translation.ugettext(
      'Date on which students must satisfy age requirements.')

  #: Required 1:1 relationship indicating the Program the Timeline
  #: belongs to.
  timeline = db.ReferenceProperty(
      reference_class=timeline_model.Timeline,
      required=True, collection_name="program",
      verbose_name=translation.ugettext('Timeline'))

  #: Document reference property used for the Org Admin Agreement
  org_admin_agreement = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Organization Admin Agreement'),
      collection_name='org_admin_agreement')
  org_admin_agreement.group = PROGRAM_DOCUMENTS_GROUP
  org_admin_agreement.help_text = translation.ugettext(
      'Document containing optional Mentor Agreement for participating as a '
      'Organization admin.')

  #: Document reference property used for the Mentor Agreement
  mentor_agreement = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Mentor Agreement'),
      collection_name='mentor_agreement')
  mentor_agreement.group = PROGRAM_DOCUMENTS_GROUP
  mentor_agreement.help_text = translation.ugettext(
      'Document containing optional Mentor Agreement for participating as a '
      'Mentor.')

  #: Document reference property used for the Student Agreement
  student_agreement = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Student Agreement'),
      collection_name='student_agreement')
  student_agreement.group = PROGRAM_DOCUMENTS_GROUP
  student_agreement.help_text = translation.ugettext(
      'Document containing optional Student Agreement for participating as a '
      'Student.')

  #: Status of the program
  status = db.StringProperty(required=True, default=STATUS_INVISIBLE,
      verbose_name=translation.ugettext('Program Status'),
      choices=[STATUS_INVISIBLE, STATUS_VISIBLE, STATUS_INVALID])
  status.group = GENERAL_INFO_GROUP
  status.help_text = translation.ugettext(
      # TODO(nathaniel): Someone got their HTML in this Python.
      '<tt>%s: Program Stealth-Mode Visible to Hosts and Devs only.
'
      '%s: Visible to everyone.
'
      '%s: Not visible or editable by anyone.</tt>' % (
          STATUS_INVISIBLE, STATUS_VISIBLE, STATUS_INVALID))

  #: The document entity which contains the "About" page for the program
  about_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('About page document'),
      collection_name = 'about_page')
  about_page.group = PROGRAM_DOCUMENTS_GROUP
  about_page.help_text = translation.ugettext('The document with About')

  #: The document entity which contains "Events & Timeline" page
  #: for the program
  events_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Events page document'),
      collection_name = 'events_page')
  events_page.group = PROGRAM_DOCUMENTS_GROUP
  events_page.help_text = translation.ugettext(
      'The document for the Events & Timeline page')

  #: The url which contains the "Events & Timeline" frame
  events_frame_url = db.LinkProperty(
      verbose_name=translation.ugettext('Events page iframe url'))
  events_frame_url.group = GENERAL_INFO_GROUP
  events_frame_url.help_text = translation.ugettext(
      'The iframe url for the Events & Timeline page')

  #: The document entity which contains the "Connect With Us" page
  #: for the program
  connect_with_us_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Connect with us document'),
      collection_name = 'connect_with_us_page')
  connect_with_us_page.group = PROGRAM_DOCUMENTS_GROUP
  connect_with_us_page.help_text = translation.ugettext(
      'The document for the Connect With Us page')

  #: The document entity which contains the "Help" page
  #: for the program
  help_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Help document'),
      collection_name = 'help_page')
  help_page.group = PROGRAM_DOCUMENTS_GROUP
  help_page.help_text = translation.ugettext(
      'The document for the Help page')

  privacy_policy_url = db.LinkProperty(
      verbose_name=translation.ugettext("Privacy Policy"))
  privacy_policy_url.group = GENERAL_INFO_GROUP
  privacy_policy_url.help_text = translation.ugettext(
      "The url for the Privacy Policy")

  #: ATOM or RSS feed URL. Feed entries are shown on the site
  #: page using Google's JavaScript blog widget
  feed_url = db.LinkProperty(verbose_name=translation.ugettext('Feed URL'))
  feed_url.group = CONTACT_INFO_GROUP
  feed_url.help_text = translation.ugettext(
      'The URL should be a valid ATOM or RSS feed. '
      'Feed entries are shown on the program home page.')

  blogger = db.LinkProperty(
      required=False, verbose_name=translation.ugettext("Blogger URL"))
  blogger.group = CONTACT_INFO_GROUP
  blogger.help_text = translation.ugettext(
      "URL of the Blogger home page for the program")

  gplus = db.LinkProperty(
      required=False, verbose_name=translation.ugettext("Google+ URL"))
  gplus.group = CONTACT_INFO_GROUP
  gplus.help_text = translation.ugettext(
      "URL of the Google+ home page for the program")

  email = db.EmailProperty(
      required=False, verbose_name=translation.ugettext("Program email"))
  email.group = CONTACT_INFO_GROUP
  email.help_text = translation.ugettext(
      "Contact email address for the program")

  irc = db.EmailProperty(
      required=False, verbose_name=translation.ugettext("IRC URL"))
  irc.group = CONTACT_INFO_GROUP
  irc.help_text = translation.ugettext(
      "URL of the irc channel for the program in "
      "the format irc://<channel>@server")

  #: Whether the messaging system is enabled
  messaging_enabled = db.BooleanProperty(default=False,
      verbose_name=translation.ugettext('Messaging Enabled'))
  messaging_enabled.group = GENERAL_INFO_GROUP
  messaging_enabled.help_text = translation.ugettext(
      'Indicates if the messaging system should be enabled.')

  #: Property pointing to the file with predefined schools.
  schools = blobstore.BlobReferenceProperty()

  def getProgramMessages(self):
    def get_or_create_txn():
      entity = type(self)._messages_model.all().ancestor(self).get()

      if not entity:
        entity = self._messages_model(parent=self)
        entity.put()
      return entity

    return db.run_in_transaction(get_or_create_txn)
<commit_msg>Key helper functions added to program model.<commit_after># Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This module contains the Program Model."""

from google.appengine.ext import blobstore
from google.appengine.ext import db

from django.utils import translation

from soc.models import document as document_model
from soc.models import linkable as linkable_model
from soc.models import sponsor as sponsor_model
from soc.models import timeline as timeline_model

# Allowed values for program status.
STATUS_VISIBLE = 'visible'  # The program is visible to everyone.
STATUS_INVISIBLE = 'invisible'  # The program is visible to hosts and devs.
STATUS_INVALID = 'invalid'  # The program is not visible to anyone.
# TODO(nathaniel): Is STATUS_INVALID used or useful any more?

GENERAL_INFO_GROUP = translation.ugettext(
    '1. General Info')
CONTACT_INFO_GROUP = translation.ugettext(
    '2. Contact Info')
AGE_REQUIREMENTS_GROUP = translation.ugettext(
    '3. Age Requirements')
PROGRAM_DOCUMENTS_GROUP = translation.ugettext(
    '4. Program Documents')

class ProgramMessages(db.Model):
  """The ProgramMessages model.

  This model contains the specific messages whose content may be customized
  by program administrators and which may be sent because of various reasons
  throughout the program.
  """

  #: Message sent to the organizations that are accepted for the program.
  accepted_orgs_msg = db.TextProperty(required=False,
      verbose_name=translation.ugettext('Accepted Organizations Message'))

  #: Message sent to the organizations that are rejected for the program.
  rejected_orgs_msg = db.TextProperty(required=False,
      verbose_name=translation.ugettext('Rejected Organizations Message'))

  #: Message sent to a mentor/org admin the first time they join the program.
  mentor_welcome_msg = db.TextProperty(required=False,
      verbose_name=translation.ugettext('Mentor Welcome Message'))

def getSponsorId(program_key):
  """Returns sponsor ID based on the specified program key.

  Args:
    program_key: Program key.

  Returns:
    A string that represents sponsor ID.
  """
  key_name = program_key.name()
  return key_name.split('/')[0]

def getProgramId(program_key):
  """Returns program ID based on the specified program key.

  Args:
    program_key: Program key.

  Returns:
    A string that represents program ID.
  """
  key_name = program_key.name()
  return key_name.split('/')[1]

# TODO(daniel): drop inheritance from Linkable
class Program(linkable_model.Linkable):
  """The Program model, representing a Program ran by a Sponsor."""

  _messages_model = ProgramMessages

  #: string used as a prefix of various key names for other models
  #TODO(daniel): eliminate this
  prefix = 'program'

  #: Identifier of the program which is be the last part of its unique key name
  program_id = db.StringProperty(required=True,
      verbose_name=translation.ugettext('Program ID'))
  program_id.help_text = translation.ugettext(
      'Used as part of various URL links throughout the site.')

  #: Reference to the sponsor of the program. Its key_name is used as the first
  #: part of program's unique key name
  sponsor = db.ReferenceProperty(required=True,
      reference_class=sponsor_model.Sponsor, collection_name='programs',
      verbose_name=translation.ugettext('Sponsor'))

  #: Required field storing name of the group.
  name = db.StringProperty(required=True,
      verbose_name=translation.ugettext('Name'))
  name.group = GENERAL_INFO_GROUP
  name.help_text = translation.ugettext(
      'Complete, formal name of the program.')

  #: Required field storing short name of the group.
  #: It can be used for displaying group as sidebar menu item.
  short_name = db.StringProperty(required=True,
      verbose_name=translation.ugettext('Short name'))
  short_name.group = GENERAL_INFO_GROUP
  short_name.help_text = translation.ugettext(
      'Short name used for sidebar menu')

  #: Required field storing description of the group.
  description = db.TextProperty(required=True,
      verbose_name=translation.ugettext('Description'))
  description.group = GENERAL_INFO_GROUP
  description.help_text = translation.ugettext(
      'for example:
'
      '<tt>GSoC 2009 is the Google Summer of Code,'
      ' but in 2009!</tt>

'
      '(rich text formatting is supported)')

  #: Number of accepted organizations
  nr_accepted_orgs = db.IntegerProperty(
      required=False, verbose_name=translation.ugettext('#accepted orgs'))
  nr_accepted_orgs.group = GENERAL_INFO_GROUP
  nr_accepted_orgs.help_text = translation.ugettext(
      'The number of accepted organizations.')

  #: Property that contains the minimum age of a student allowed to
  #: participate
  student_min_age = db.IntegerProperty(
      required=False, verbose_name=translation.ugettext('Student minimum age'))
  student_min_age.group = AGE_REQUIREMENTS_GROUP
  student_min_age.help_text = translation.ugettext(
      'Minimum age of students.')

  #: Property that contains the maximum age of a student allowed to
  #: participate
  student_max_age = db.IntegerProperty(default=200,
      required=False, verbose_name=translation.ugettext('Student maximum age'))
  student_max_age.group = AGE_REQUIREMENTS_GROUP
  student_max_age.help_text = translation.ugettext(
      'Maximum whole-year age of students.')

  #: Property that contains the date as of which above student
  #: minimum/maximum age requirement holds.
  student_min_age_as_of = db.DateProperty(
      required=False, verbose_name=translation.ugettext('Age as of'))
  student_min_age_as_of.group = AGE_REQUIREMENTS_GROUP
  student_min_age_as_of.help_text = translation.ugettext(
      'Date on which students must satisfy age requirements.')

  #: Required 1:1 relationship indicating the Program the Timeline
  #: belongs to.
  timeline = db.ReferenceProperty(
      reference_class=timeline_model.Timeline,
      required=True, collection_name="program",
      verbose_name=translation.ugettext('Timeline'))

  #: Document reference property used for the Org Admin Agreement
  org_admin_agreement = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Organization Admin Agreement'),
      collection_name='org_admin_agreement')
  org_admin_agreement.group = PROGRAM_DOCUMENTS_GROUP
  org_admin_agreement.help_text = translation.ugettext(
      'Document containing optional Mentor Agreement for participating as a '
      'Organization admin.')

  #: Document reference property used for the Mentor Agreement
  mentor_agreement = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Mentor Agreement'),
      collection_name='mentor_agreement')
  mentor_agreement.group = PROGRAM_DOCUMENTS_GROUP
  mentor_agreement.help_text = translation.ugettext(
      'Document containing optional Mentor Agreement for participating as a '
      'Mentor.')

  #: Document reference property used for the Student Agreement
  student_agreement = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Student Agreement'),
      collection_name='student_agreement')
  student_agreement.group = PROGRAM_DOCUMENTS_GROUP
  student_agreement.help_text = translation.ugettext(
      'Document containing optional Student Agreement for participating as a '
      'Student.')

  #: Status of the program
  status = db.StringProperty(required=True, default=STATUS_INVISIBLE,
      verbose_name=translation.ugettext('Program Status'),
      choices=[STATUS_INVISIBLE, STATUS_VISIBLE, STATUS_INVALID])
  status.group = GENERAL_INFO_GROUP
  status.help_text = translation.ugettext(
      # TODO(nathaniel): Someone got their HTML in this Python.
      '<tt>%s: Program Stealth-Mode Visible to Hosts and Devs only.
'
      '%s: Visible to everyone.
'
      '%s: Not visible or editable by anyone.</tt>' % (
          STATUS_INVISIBLE, STATUS_VISIBLE, STATUS_INVALID))

  #: The document entity which contains the "About" page for the program
  about_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('About page document'),
      collection_name = 'about_page')
  about_page.group = PROGRAM_DOCUMENTS_GROUP
  about_page.help_text = translation.ugettext('The document with About')

  #: The document entity which contains "Events & Timeline" page
  #: for the program
  events_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Events page document'),
      collection_name = 'events_page')
  events_page.group = PROGRAM_DOCUMENTS_GROUP
  events_page.help_text = translation.ugettext(
      'The document for the Events & Timeline page')

  #: The url which contains the "Events & Timeline" frame
  events_frame_url = db.LinkProperty(
      verbose_name=translation.ugettext('Events page iframe url'))
  events_frame_url.group = GENERAL_INFO_GROUP
  events_frame_url.help_text = translation.ugettext(
      'The iframe url for the Events & Timeline page')

  #: The document entity which contains the "Connect With Us" page
  #: for the program
  connect_with_us_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Connect with us document'),
      collection_name = 'connect_with_us_page')
  connect_with_us_page.group = PROGRAM_DOCUMENTS_GROUP
  connect_with_us_page.help_text = translation.ugettext(
      'The document for the Connect With Us page')

  #: The document entity which contains the "Help" page
  #: for the program
  help_page = db.ReferenceProperty(
      reference_class=document_model.Document,
      verbose_name=translation.ugettext('Help document'),
      collection_name = 'help_page')
  help_page.group = PROGRAM_DOCUMENTS_GROUP
  help_page.help_text = translation.ugettext(
      'The document for the Help page')

  privacy_policy_url = db.LinkProperty(
      verbose_name=translation.ugettext("Privacy Policy"))
  privacy_policy_url.group = GENERAL_INFO_GROUP
  privacy_policy_url.help_text = translation.ugettext(
      "The url for the Privacy Policy")

  #: ATOM or RSS feed URL. Feed entries are shown on the site
  #: page using Google's JavaScript blog widget
  feed_url = db.LinkProperty(verbose_name=translation.ugettext('Feed URL'))
  feed_url.group = CONTACT_INFO_GROUP
  feed_url.help_text = translation.ugettext(
      'The URL should be a valid ATOM or RSS feed. '
      'Feed entries are shown on the program home page.')

  blogger = db.LinkProperty(
      required=False, verbose_name=translation.ugettext("Blogger URL"))
  blogger.group = CONTACT_INFO_GROUP
  blogger.help_text = translation.ugettext(
      "URL of the Blogger home page for the program")

  gplus = db.LinkProperty(
      required=False, verbose_name=translation.ugettext("Google+ URL"))
  gplus.group = CONTACT_INFO_GROUP
  gplus.help_text = translation.ugettext(
      "URL of the Google+ home page for the program")

  email = db.EmailProperty(
      required=False, verbose_name=translation.ugettext("Program email"))
  email.group = CONTACT_INFO_GROUP
  email.help_text = translation.ugettext(
      "Contact email address for the program")

  irc = db.EmailProperty(
      required=False, verbose_name=translation.ugettext("IRC URL"))
  irc.group = CONTACT_INFO_GROUP
  irc.help_text = translation.ugettext(
      "URL of the irc channel for the program in "
      "the format irc://<channel>@server")

  #: Whether the messaging system is enabled
  messaging_enabled = db.BooleanProperty(default=False,
      verbose_name=translation.ugettext('Messaging Enabled'))
  messaging_enabled.group = GENERAL_INFO_GROUP
  messaging_enabled.help_text = translation.ugettext(
      'Indicates if the messaging system should be enabled.')

  #: Property pointing to the file with predefined schools.
  schools = blobstore.BlobReferenceProperty()

  def getProgramMessages(self):
    def get_or_create_txn():
      entity = type(self)._messages_model.all().ancestor(self).get()

      if not entity:
        entity = self._messages_model(parent=self)
        entity.put()
      return entity

    return db.run_in_transaction(get_or_create_txn)
<|endoftext|>


<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Main application file.
"""
from lib.config import AppConf

appConf = AppConf()
<commit_msg>style: Add blank line to app.py<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Main application file.
"""
from lib.config import AppConf

appConf = AppConf()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import datetime
from simplekv.memory.redisstore import RedisStore

import dotenv
from flask import Flask
from flask_jwt_extended import JWTManager
from flask_redis import FlaskRedis

from app.views import BLUEPRINTS
from extensions import (db, migrate)

# Load dotenv
dotenv_path = os.path.join(
    os.path.dirname(__file__),
    '../.env.' + os.getenv('PYTHON_ENV', 'development')
)
dotenv.load_dotenv(dotenv_path)

class Config(object):
    DEBUG = bool(int(os.getenv('DEBUG')))
    ERROR_404_HELP = bool(int(os.getenv('ERROR_404_HELP')))

    # sqlalchemy setting
    SQLALCHEMY_ECHO = bool(int(os.getenv('SQLALCHEMY_ECHO', 0)))
    SQLALCHEMY_TRACK_MODIFICATIONS = bool(
        int(os.getenv('SQLALCHEMY_TRACK_MODIFICATIONS', 0)))
    SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL')

    SECRET_KEY = os.getenv('SECRET_KEY')
    JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(hours=1)
    JWT_REFRESH_TOKEN_EXPIRES = datetime.datetime(days=30)
    JWT_BLACKLIST_ENABLED = bool(int(os.getenv('JWT_BLACKLIST_ENABLED')))
    REDIS_URL = os.getenv('REDIS_URL')

    # Check all tokens (access and refresh) to see if they have been revoked.
    # You can alternately check only the refresh tokens here, by setting this
    # to 'refresh' instead of 'all'
    JWT_BLACKLIST_TOKEN_CHECKS = os.getenv('JWT_BLACKLIST_TOKEN_CHECKS')

def create_app(app_name='app', blueprints=None):
    app = Flask(app_name)
    app.config.from_object(Config())

    if blueprints is None:
        blueprints = BLUEPRINTS
    blueprints_resister(app, blueprints)
    extensions_load(app)

    return app

def blueprints_resister(app, blueprints):
    for bp in blueprints:
        app.register_blueprint(bp)

def extensions_load(app):
    db.init_app(app)
    migrate.init_app(app, db)
    JWTManager(app)
    redis_store = FlaskRedis()
    redis_store.init_app(app)
    app.config['JWT_BLACKLIST_STORE'] = RedisStore(redis_store)
<commit_msg>fix refresh toekn expire time.<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import os
import datetime
from simplekv.memory.redisstore import RedisStore

import dotenv
from flask import Flask
from flask_jwt_extended import JWTManager
from flask_redis import FlaskRedis

from app.views import BLUEPRINTS
from extensions import (db, migrate)

# Load dotenv
dotenv_path = os.path.join(
    os.path.dirname(__file__),
    '../.env.' + os.getenv('PYTHON_ENV', 'development')
)
dotenv.load_dotenv(dotenv_path)

class Config(object):
    DEBUG = bool(int(os.getenv('DEBUG')))
    ERROR_404_HELP = bool(int(os.getenv('ERROR_404_HELP')))

    # sqlalchemy setting
    SQLALCHEMY_ECHO = bool(int(os.getenv('SQLALCHEMY_ECHO', 0)))
    SQLALCHEMY_TRACK_MODIFICATIONS = bool(
        int(os.getenv('SQLALCHEMY_TRACK_MODIFICATIONS', 0)))
    SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL')

    SECRET_KEY = os.getenv('SECRET_KEY')
    JWT_ACCESS_TOKEN_EXPIRES = datetime.timedelta(hours=1)
    JWT_REFRESH_TOKEN_EXPIRES = datetime.timedelta(days=30)
    JWT_BLACKLIST_ENABLED = bool(int(os.getenv('JWT_BLACKLIST_ENABLED')))
    REDIS_URL = os.getenv('REDIS_URL')

    # Check all tokens (access and refresh) to see if they have been revoked.
    # You can alternately check only the refresh tokens here, by setting this
    # to 'refresh' instead of 'all'
    JWT_BLACKLIST_TOKEN_CHECKS = os.getenv('JWT_BLACKLIST_TOKEN_CHECKS')

def create_app(app_name='app', blueprints=None):
    app = Flask(app_name)
    app.config.from_object(Config())

    if blueprints is None:
        blueprints = BLUEPRINTS
    blueprints_resister(app, blueprints)
    extensions_load(app)

    return app

def blueprints_resister(app, blueprints):
    for bp in blueprints:
        app.register_blueprint(bp)

def extensions_load(app):
    db.init_app(app)
    migrate.init_app(app, db)
    JWTManager(app)
    redis_store = FlaskRedis()
    redis_store.init_app(app)
    app.config['JWT_BLACKLIST_STORE'] = RedisStore(redis_store)
<|endoftext|>


<commit_before>import asyncio
import discord
import datetime
from   discord.ext import commands
from   Cogs import Settings
from   Cogs import DisplayName

class Time:

	# Init with the bot reference, and a reference to the settings var
	def __init__(self, bot, settings):
		self.bot = bot
		self.settings = settings

	@commands.command(pass_context=True)
	async def setoffset(self, ctx, *, offset : str = None):
		"""Set your UTC offset."""

		if offset == None:
			self.settings.setUserStat(ctx.message.author, "UTCOffset", None)
			msg = '*{}*, your UTC offset has been removed!'.format(DisplayName.name(ctx.message.author))
			await ctx.channel.send(msg)
			return

		offset = offset.replace('+', '')

		# Split time string by : and get hour/minute values
		try:
			hours, minutes = map(int, offset.split(':'))
		except Exception:
			try:
				hours = int(offset)
				minutes = 0
			except Exception:
				await ctx.channel.send('Offset has to be in +-H:M!')
				return
		off = "{}:{}".format(hours, minutes)
		self.settings.setGlobalUserStat(ctx.message.author, "UTCOffset", off)
		msg = '*{}*, your UTC offset has been set to *{}!*'.format(DisplayName.name(ctx.message.author), off)
		await ctx.channel.send(msg)

	@commands.command(pass_context=True)
	async def offset(self, ctx, *, member = None):
		"""See a member's UTC offset."""

		# Check if we're suppressing @here and @everyone mentions
		if self.settings.getServerStat(ctx.message.guild, "SuppressMentions").lower() == "yes":
			suppress = True
		else:
			suppress = False

		if member == None:
			member = ctx.message.author

		if type(member) == str:
			# Try to get a user first
			memberName = member
			member = DisplayName.memberForName(memberName, ctx.message.guild)
			if not member:
				msg = 'Couldn\'t find user *{}*.'.format(memberName)
				# Check for suppress
				if suppress:
					msg = Nullify.clean(msg)
				await ctx.channel.send(msg)
				return

		# We got one
		offset = self.settings.getUserStat(member, ctx.message.guild, "UTCOffset")
		if offset == None:
			msg = '*{}* hasn\'t set their offset yet - they can do so with the `{}setoffset [+-offset]` command.'.format(DisplayName.name(member), ctx.prefix)
			await ctx.channel.send(msg)
			return

		# Split time string by : and get hour/minute values
		try:
			hours, minutes = map(int, offset.split(':'))
		except Exception:
			try:
				hours = int(offset)
				minutes = 0
			except Exception:
				await ctx.channel.send('Offset has to be in +-H:M!')
				return

		msg = 'UTC'
		# Apply offset
		if hours > 0:
			# Apply positive offset
			msg += '+{}'.format(offset)
		elif hours < 0:
			# Apply negative offset
			msg += '{}'.format(offset)

		msg = '*{}\'s* offset is *{}*'.format(DisplayName.name(member), msg)
		await ctx.channel.send(msg)

	@commands.command(pass_context=True)
	async def time(self, ctx, *, offset : str = None):
		"""Get UTC time +- an offset."""

		if offset == None:
			member = ctx.message.author
		else:
			# Try to get a user first
			member = DisplayName.memberForName(offset, ctx.message.guild)

		if member:
			# We got one
			offset = self.settings.getUserStat(member, ctx.message.guild, "UTCOffset")

		if offset == None:
			msg = '*{}* hasn\'t set their offset yet - they can do so with the `{}setoffset [+-offset]` command.\nThe current UTC time is *{}*.'.format(DisplayName.name(member), ctx.prefix, datetime.datetime.utcnow().strftime("%I:%M %p"))
			await ctx.channel.send(msg)
			return

		offset = offset.replace('+', '')

		# Split time string by : and get hour/minute values
		try:
			hours, minutes = map(int, offset.split(':'))
		except Exception:
			try:
				hours = int(offset)
				minutes = 0
			except Exception:
				await ctx.channel.send('Offset has to be in +-H:M!')
				return

		msg = 'UTC'
		# Get the time
		t = datetime.datetime.utcnow()
		# Apply offset
		if hours > 0:
			# Apply positive offset
			msg += '+{}'.format(offset)
			td = datetime.timedelta(hours=hours, minutes=minutes)
			newTime = t + td
		elif hours < 0:
			# Apply negative offset
			msg += '{}'.format(offset)
			td = datetime.timedelta(hours=(-1*hours), minutes=(-1*minutes))
			newTime = t - td
		else:
			# No offset
			newTime = t

		if member:
			msg = '{}; where *{}* is, it\'s currently *{}*'.format(msg, DisplayName.name(member), newTime.strftime("%I:%M %p"))
		else:
			msg = '{} is currently *{}*'.format(msg, newTime.strftime("%I:%M %p"))
		# Say message
		await ctx.channel.send(msg)
<commit_msg>Update Time.py<commit_after>import asyncio
import discord
import datetime
from   discord.ext import commands
from   Cogs import Settings
from   Cogs import DisplayName

class Time:

	# Init with the bot reference, and a reference to the settings var
	def __init__(self, bot, settings):
		self.bot = bot
		self.settings = settings

	@commands.command(pass_context=True)
	async def setoffset(self, ctx, *, offset : str = None):
		"""Set your UTC offset."""

		if offset == None:
			self.settings.setGlobalUserStat(ctx.message.author, "UTCOffset", None)
			msg = '*{}*, your UTC offset has been removed!'.format(DisplayName.name(ctx.message.author))
			await ctx.channel.send(msg)
			return

		offset = offset.replace('+', '')

		# Split time string by : and get hour/minute values
		try:
			hours, minutes = map(int, offset.split(':'))
		except Exception:
			try:
				hours = int(offset)
				minutes = 0
			except Exception:
				await ctx.channel.send('Offset has to be in +-H:M!')
				return
		off = "{}:{}".format(hours, minutes)
		self.settings.setGlobalUserStat(ctx.message.author, "UTCOffset", off)
		msg = '*{}*, your UTC offset has been set to *{}!*'.format(DisplayName.name(ctx.message.author), off)
		await ctx.channel.send(msg)

	@commands.command(pass_context=True)
	async def offset(self, ctx, *, member = None):
		"""See a member's UTC offset."""

		# Check if we're suppressing @here and @everyone mentions
		if self.settings.getServerStat(ctx.message.guild, "SuppressMentions").lower() == "yes":
			suppress = True
		else:
			suppress = False

		if member == None:
			member = ctx.message.author

		if type(member) == str:
			# Try to get a user first
			memberName = member
			member = DisplayName.memberForName(memberName, ctx.message.guild)
			if not member:
				msg = 'Couldn\'t find user *{}*.'.format(memberName)
				# Check for suppress
				if suppress:
					msg = Nullify.clean(msg)
				await ctx.channel.send(msg)
				return

		# We got one
		offset = self.settings.getUserStat(member, ctx.message.guild, "UTCOffset")
		if offset == None:
			msg = '*{}* hasn\'t set their offset yet - they can do so with the `{}setoffset [+-offset]` command.'.format(DisplayName.name(member), ctx.prefix)
			await ctx.channel.send(msg)
			return

		# Split time string by : and get hour/minute values
		try:
			hours, minutes = map(int, offset.split(':'))
		except Exception:
			try:
				hours = int(offset)
				minutes = 0
			except Exception:
				await ctx.channel.send('Offset has to be in +-H:M!')
				return

		msg = 'UTC'
		# Apply offset
		if hours > 0:
			# Apply positive offset
			msg += '+{}'.format(offset)
		elif hours < 0:
			# Apply negative offset
			msg += '{}'.format(offset)

		msg = '*{}\'s* offset is *{}*'.format(DisplayName.name(member), msg)
		await ctx.channel.send(msg)

	@commands.command(pass_context=True)
	async def time(self, ctx, *, offset : str = None):
		"""Get UTC time +- an offset."""

		if offset == None:
			member = ctx.message.author
		else:
			# Try to get a user first
			member = DisplayName.memberForName(offset, ctx.message.guild)

		if member:
			# We got one
			offset = self.settings.getUserStat(member, ctx.message.guild, "UTCOffset")

		if offset == None:
			msg = '*{}* hasn\'t set their offset yet - they can do so with the `{}setoffset [+-offset]` command.\nThe current UTC time is *{}*.'.format(DisplayName.name(member), ctx.prefix, datetime.datetime.utcnow().strftime("%I:%M %p"))
			await ctx.channel.send(msg)
			return

		offset = offset.replace('+', '')

		# Split time string by : and get hour/minute values
		try:
			hours, minutes = map(int, offset.split(':'))
		except Exception:
			try:
				hours = int(offset)
				minutes = 0
			except Exception:
				await ctx.channel.send('Offset has to be in +-H:M!')
				return

		msg = 'UTC'
		# Get the time
		t = datetime.datetime.utcnow()
		# Apply offset
		if hours > 0:
			# Apply positive offset
			msg += '+{}'.format(offset)
			td = datetime.timedelta(hours=hours, minutes=minutes)
			newTime = t + td
		elif hours < 0:
			# Apply negative offset
			msg += '{}'.format(offset)
			td = datetime.timedelta(hours=(-1*hours), minutes=(-1*minutes))
			newTime = t - td
		else:
			# No offset
			newTime = t

		if member:
			msg = '{}; where *{}* is, it\'s currently *{}*'.format(msg, DisplayName.name(member), newTime.strftime("%I:%M %p"))
		else:
			msg = '{} is currently *{}*'.format(msg, newTime.strftime("%I:%M %p"))
		# Say message
		await ctx.channel.send(msg)
<|endoftext|>


<commit_before>import json
from functools import partial

try:
    from django.contrib.postgres.fields import JSONField as NativeJSONField
except ImportError:  # pragma: no cover
    # If using Django 1.8, don't worry about trying to be correct.
    # We won't be using the native field.
    NativeJSONField = object

from django.conf import settings
from django.utils import six
from django.utils.module_loading import import_string
from psycopg2.extras import Json

def _get_jsonfield_encoder_class():
    encoder_class = getattr(settings, 'JSONFIELD_ENCODER_CLASS', None)
    if encoder_class:
        if isinstance(encoder_class, six.string_types):
            encoder_class = import_string(encoder_class)

        return encoder_class

def _get_dumps():
    return partial(json.dumps, cls=_get_jsonfield_encoder_class())

class _JSONField(NativeJSONField):
    """ Patches Django's JSONField to use django-jsonfield's
        JSONFIELD_ENCODER_CLASS setting
    """
    def get_prep_value(self, value):
        if value is not None:
            return Json(value, dumps=_get_dumps())
        return value

    def get_prep_lookup(self, lookup_type, value):
        if lookup_type in ('has_key', 'has_keys', 'has_any_keys'):
            return value
        if isinstance(value, (dict, list)):
            return Json(value, dumps=_get_dumps())
        return super(_JSONField, self).get_prep_lookup(lookup_type, value)

    def deconstruct(self):
        name, path, args, kwargs = super(_JSONField, self).deconstruct()
        kwargs.setdefault('default', dict)
        path = 'jsonfield_compat.JSONField'
        return name, path, args, kwargs
<commit_msg>Use full path to JSONField import location<commit_after>import json
from functools import partial

try:
    from django.contrib.postgres.fields import JSONField as NativeJSONField
except ImportError:  # pragma: no cover
    # If using Django 1.8, don't worry about trying to be correct.
    # We won't be using the native field.
    NativeJSONField = object

from django.conf import settings
from django.utils import six
from django.utils.module_loading import import_string
from psycopg2.extras import Json

def _get_jsonfield_encoder_class():
    encoder_class = getattr(settings, 'JSONFIELD_ENCODER_CLASS', None)
    if encoder_class:
        if isinstance(encoder_class, six.string_types):
            encoder_class = import_string(encoder_class)

        return encoder_class

def _get_dumps():
    return partial(json.dumps, cls=_get_jsonfield_encoder_class())

class _JSONField(NativeJSONField):
    """ Patches Django's JSONField to use django-jsonfield's
        JSONFIELD_ENCODER_CLASS setting
    """
    def get_prep_value(self, value):
        if value is not None:
            return Json(value, dumps=_get_dumps())
        return value

    def get_prep_lookup(self, lookup_type, value):
        if lookup_type in ('has_key', 'has_keys', 'has_any_keys'):
            return value
        if isinstance(value, (dict, list)):
            return Json(value, dumps=_get_dumps())
        return super(_JSONField, self).get_prep_lookup(lookup_type, value)

    def deconstruct(self):
        name, _, args, kwargs = super(_JSONField, self).deconstruct()
        kwargs.setdefault('default', dict)
        path = 'jsonfield_compat.fields.JSONField'
        return name, path, args, kwargs
<|endoftext|>


<commit_before><commit_msg>add in other scaling option and cleanup<commit_after><|endoftext|>


<commit_before><commit_msg>7370 fix copy paste that would have left source object id unchanged<commit_after><|endoftext|>


<commit_before><commit_msg>skew5<commit_after><|endoftext|>