Beispiel #1
0
 def saveDayBar():
     for inst in TickController.inst_current_tick.keys():
         tick = TickController.inst_current_tick[inst]
         print(tick)
         DatabaseController.insert_DayBar(tick)
         #update all indicators!!!
         inst_thread[inst].InitIndicator()
Beispiel #2
0
def archive():
    db = DatabaseController()

    q = request.args.get('q')
    search = False
    if q:
        search = True
    page = request.args.get(get_page_parameter(), type=int, default=1)

    all_archived_members = db.get_row(DatabaseTables.CLAN, 'aktivan', 0, return_all=True)

    pagination = Pagination(page=page, per_page=ARCHIVED_MEMBERS_PER_PAGE, total=len(all_archived_members),
                            search=search, record_name='members', css_framework='foundation')

    start_page = (page - 1) * ARCHIVED_MEMBERS_PER_PAGE
    end_page = (page - 1) * ARCHIVED_MEMBERS_PER_PAGE + ARCHIVED_MEMBERS_PER_PAGE

    members_list = {}
    for i, member in enumerate(sorted(all_archived_members, key=lambda x: x[10])):
        if start_page <= i < end_page:
            members_list[member[0]] = (member[Utilities.UserDBIndex.IME], member[Utilities.UserDBIndex.PREZIME],
                                       member[Utilities.UserDBIndex.NADIMAK],
                                       member[Utilities.UserDBIndex.DATUM_UCLANJENJA],
                                       member[Utilities.UserDBIndex.BROJ_ISKAZNICE],
                                       member[Utilities.UserDBIndex.EMAIL],
                                       db.get_member_primary_section(member[0]),
                                       member[Utilities.UserDBIndex.DATUM_DEAKTIVACIJE])
        elif i >= end_page:
            break

    return render_template('/members/archive.html', members_list=members_list, pagination=pagination)
Beispiel #3
0
def member_already_joined(member_id, section_name):
    db = DatabaseController()
    possible_sections = [x for x in db.get_all_members_sections(member_id)]
    for section in possible_sections:
        if section == section_name:
            return True
    return False
Beispiel #4
0
def get_member_info(member_id):
    db = DatabaseController()
    member = db.get_table_row(DatabaseTables.CLAN, int(member_id))
    member_info = (member[1], member[2], member[3])
    date_joined = member[7]
    section = member[10]
    return member_info, date_joined, section
Beispiel #5
0
def add_activity_type():
    db = DatabaseController()
    if request.method == 'POST':
        name = request.form['name']
        description = request.form['description']
        try:
            section = request.form['section']
        except HTTPException:
            section = session["section"]

        error = None

        if not name:
            error = 'Username is required.'
        if not description:
            description = '-'

        if error is None:
            entry_values = (name, description, section)
            db.add_activity_type_entry(entry_values)
            flash("Tip aktivnosti %s je uspješno dodan!" % name, 'success')
            return redirect(url_for('index'))

        flash(error, 'danger')

    return render_template('/activities/add_type.html')
Beispiel #6
0
def activate(member_id=None):
    if request.method == 'POST':
        db = DatabaseController()
        db.activate_member(member_id)
        flash('Član je uspješno aktiviran', 'success')

    return "1"
Beispiel #7
0
def login():
    if request.method == 'POST':

        username = request.form['username']
        password = request.form['password']

        db = DatabaseController()

        error = None
        user = db.get_row(DatabaseTables.KORISNICKI_RACUNI, 'username', username)

        if user is None:
            error = 'Incorrect username or password.'
        elif not check_password_hash(user[2], password):  # user[2] - password
            error = 'Incorrect username or password.'

        if error is None:
            session.clear()
            session["user_id"] = user[0]  # user[0] - id
            session["username"] = user[1]  # user[1] - username
            session["access_level"] = int(user[3])  # user[3] - access level
            session["section"] = user[4]  # user[4] - sekcija
            return redirect(url_for('index'))

        flash(error)

    return render_template('index.html')
Beispiel #8
0
def list_activity_types():
    db = DatabaseController()

    q = request.args.get('q')
    search = False
    if q:
        search = True
    page = request.args.get(get_page_parameter(), type=int, default=1)

    if session['access_level'] == AccessLevels.ADMIN:
        activities = db.get_all_rows_from_table(DatabaseTables.TIP_AKTIVNOSTI)
    else:
        activities = db.get_all_section_activitiy_types()

    pagination = Pagination(page=page,
                            per_page=ACTIVITY_TYPES_PER_PAGE,
                            total=len(activities),
                            search=search,
                            record_name='members',
                            css_framework='foundation')

    activities_list = {}
    start_page = (page - 1) * ACTIVITY_TYPES_PER_PAGE
    end_page = (page - 1) * ACTIVITY_TYPES_PER_PAGE + ACTIVITY_TYPES_PER_PAGE
    for i, activity in enumerate(sorted(activities, key=lambda x: x[1])):
        if start_page <= i < end_page:
            activities_list[activity[0]] = activity[1:]
        elif i >= end_page:
            break

    return render_template("/activities/list_types.html",
                           list_records=activities_list,
                           pagination=pagination)
Beispiel #9
0
def get_member_activities(member_id, activity_types, start_date=None, end_date=None):
    db = DatabaseController()
    member_activities = db.get_all_activities_for_member(member_id, start_date, end_date)
    member_hours_by_activity_type = {}
    member_hours_by_activity_type_weighted = {}
    member_attendance = {}
    for activity in member_activities:
        if activity[0] not in member_hours_by_activity_type:
            member_hours_by_activity_type[activity[0]] = activity[2]
        else:
            member_hours_by_activity_type[activity[0]] += activity[2]

        if activity[0] not in member_hours_by_activity_type_weighted:
            member_hours_by_activity_type_weighted[activity[0]] = activity[2] * activity[3]
        else:
            member_hours_by_activity_type_weighted[activity[0]] += (activity[2] * activity[3])

        if activity[0] not in member_attendance:
            member_attendance[activity[0]] = 1
        else:
            member_attendance[activity[0]] += 1

    # Dodaj 0 sati na tipove na koje član nije došao
    for _type in activity_types:
        if _type not in member_hours_by_activity_type:
            member_hours_by_activity_type[_type] = 0

        if _type not in member_hours_by_activity_type_weighted:
            member_hours_by_activity_type_weighted[_type] = 0

        if _type not in member_attendance:
            member_attendance[_type] = 0

    return member_hours_by_activity_type, member_hours_by_activity_type_weighted, member_attendance
Beispiel #10
0
def list_activities():
    db = DatabaseController()
    q = request.args.get('q')
    search = False
    if q:
        search = True
    page = request.args.get(get_page_parameter(), type=int, default=1)
    activities = db.get_full_activity_info()
    pagination = Pagination(page=page,
                            per_page=ACTIVITIES_PER_PAGE,
                            total=len(activities),
                            search=search,
                            record_name='members',
                            css_framework='foundation')

    activities_list = {}
    start_page = (page - 1) * ACTIVITIES_PER_PAGE
    end_page = (page - 1) * ACTIVITIES_PER_PAGE + ACTIVITIES_PER_PAGE
    for i, activity in enumerate(
            sorted(activities, reverse=True, key=lambda x: x[3])):
        if start_page <= i < end_page:
            activities_list[activity[0]] = activity[1:]
        elif i >= end_page:
            break

    return render_template("/activities/list.html",
                           list_records=activities_list,
                           pagination=pagination)
Beispiel #11
0
def _get_activity_types(section):
    db = DatabaseController()
    all_types = db.get_all_rows_from_table(DatabaseTables.TIP_AKTIVNOSTI)
    activity_types = {}
    for _type in sorted(all_types, key=lambda x: x[1]):
        if _type[3] == 'svi' or _type[3] == section:
            activity_types[_type[0]] = _type[1]
    return activity_types
Beispiel #12
0
class DatabaseBuilder2:

	def __init__(self):
		self.db = DatabaseController('../res/mysql_data.cfg')
		self.cin = Cinematerial()

	def run(self):
		netflix_code = constants.services['netflix']
		
		for genre in constants.genre_codes:
			code = constants.genre_codes[genre]
			filename = 'netflix_{}.txt'.format(genre)
			self.build('movie_lists/{}'.format(filename), netflix_code + code)

	def get_poster(self, film_name, imdb_id):
		self.cin.get_poster(film_name, imdb_id);

	def build_top_100(self):
		imdb = MyMDb()
		film_count = 10100000
		for f in listdir('C:/Users/David/Desktop/imdb_top_100/'):
			film_id = film_count
			imdb_id = f.split('.')[0]
			info = imdb.get_film_info_ii(imdb_id)
			self.db.insert_film('imdb_top_100', film_id, info)
			film_count = film_count + 1


	def build(self, input_file, start_film_id):
		imdb = MyMDb()
		films = {}
		film_count = 0
		try:
			movie_list = open(input_file, 'r', encoding='utf-8')
		except:
			print('ERROR: Could not open file: ', input_file)
			sys.exit()

		with movie_list:
			print('Reading From: ', input_file)
			for movie in movie_list:
				if movie in films:
					print('Skipping {} - Already in Database'.format(movie))
				else:
					films[movie] = True
					film_id = start_film_id + film_count
					info = imdb.get_film_info(movie)
					print(film_id)
					film_count = film_count + 1
					self.insert_film(film_id, info)
					self.get_poster(movie, info.imdb_id)

	def insert_film(self, film_id, film_info):
		try:
			self.db.insert_film('netflix', film_id, film_info)
		except:
			print('ERROR: Failed to insert: ', film_info.name)
Beispiel #13
0
    def sendOrder(self, order):
        global mutex
        mutex.acquire()

        Strategy.traderSpi.ReqOrderInsert(order, Strategy.traderSpi.inc_request_id())
        DatabaseController.insert_SendOrder(order.to_dict()) # dict will change bytes to str
        print ('sendOrder = ' + order.to_dict()['InstrumentID'] + ' dir = ' + order.to_dict()['Direction'] + ' price = '+ str(order.to_dict()['LimitPrice']) + ' strategy = ' + self.__module__)
        time.sleep(1)
        mutex.release()
Beispiel #14
0
    def sendOrder(self, order):
        global mutex
        mutex.acquire()

        Strategy.traderSpi.ReqOrderInsert(order, Strategy.traderSpi.inc_request_id())
        DatabaseController.insert_SendOrder(order)

        print 'sendOrder = ' + order.InstrumentID + ' dir = ' + order.Direction + ' strategy = ' + self.__module__
        time.sleep(1)
        mutex.release()
	def __Login_Event__(self, event):
		self.__saveLoginConfig()
		self.__DBController = DatabaseController()
		self.__DBController.setConnectArgue(
			self.__IPInputString.GetLineText(0),
			self.__PortInputString.GetLineText(0),
			self.__UserInputString.GetLineText(0),
			self.__PasswordInputString.GetLineText(0))
		if self.__DBController.open() == True:
			self.Show(False)
		else:
			fail = wx.MessageDialog(self, self.__DBController.getErrorMessage(), "登录失败", wx.ICON_ERROR)
			fail.ShowModal()
Beispiel #16
0
def erase_member(member_id):

    if request.method == 'POST':

        db = DatabaseController()
        if not db.entry_exists(DatabaseTables.CLAN, member_id):
            error = 'Neuspješno brisanje. Zapis ne postoji u bazi.'
            flash(error, 'danger')
        else:
            db.remove_entry(DatabaseTables.CLAN, member_id)
            flash('Član uspješno izbrisan', 'success')

    return "1"
Beispiel #17
0
def remove_activity_type(activity_type_id):

    if request.method == 'POST':

        db = DatabaseController()
        if not db.entry_exists(DatabaseTables.TIP_AKTIVNOSTI,
                               activity_type_id):
            error = 'Neuspješno brisanje. Zapis ne postoji u bazi.'
            flash(error, 'danger')
        else:
            db.remove_entry(DatabaseTables.TIP_AKTIVNOSTI, activity_type_id)
            flash('Tip aktivnosti uspješno izbrisana', 'success')

    return "1"
Beispiel #18
0
class IDumpParser(object):
    __metaclass__ = ABCMeta

    def __init__(self):
        self.dump_id = ''
        self.dump_url = ''
        self.dbAccess = DatabaseController()

    def parse(self, url, withSaving=True):
        self.dump_url = url
        if withSaving:
            self.saveDump()
        return self.extractInfosFromDump(withSaving)


    def extractInfosFromDump(self,withSaving):
        rawDump = self.getRawDump()

        regexExprs = [r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\b",
                      r"\n[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}\n"]
        aggregatedMatches = []

        for expr in regexExprs:
            p = re.compile(expr)
            matches = p.findall(repr(rawDump))
            aggregatedMatches.extend(matches)

        if withSaving:
            self.saveMails(aggregatedMatches)
        return aggregatedMatches

    @abstractmethod
    def getRawDump(self):
        pass

    def saveMails(self, mails):
        for mail in mails:
            self.saveMail(mail)

    def saveMail(self, mail):
        dumpItem = {'mail': mail, 'dump_id': self.dump_id}
        self.dbAccess.insertDumpItem(dumpItem)

    def saveDump(self):
        self.dump_id = self.dbAccess.insertDump(self.dump_url)


    def getDumpUrl(self):
        return self.dump_url
Beispiel #19
0
def get_max_possible_activities(activity_types, section, start_date, end_date=None):
    db = DatabaseController()
    activities_after_member_joined = db.get_all_activities_after_date(section, start_date, end_date)
    activity_types_count = {}
    for activity in activities_after_member_joined:
        if activity[0] not in activity_types_count:
            activity_types_count[activity[0]] = 1
        else:
            activity_types_count[activity[0]] += 1

    for _type in activity_types:
        if _type not in activity_types_count:
            activity_types_count[_type] = 0

    return activity_types_count
Beispiel #20
0
class DumpFinder(object):

    def __init__(self, url):
        self.url = url
        self.dbAccess = DatabaseController()

    def checkIfAlreadyProcessed(self,url):
        return self.dbAccess.dumpExists(url)

    def getOldDumpsUrls(self):
        return self.dbAccess.getDumpsUrls()

    @abstractmethod
    def getNewDumpsUrls(self):
        raise NotImplementedError()
Beispiel #21
0
def list_activity_members(activity_id):
    db = DatabaseController()
    activity_members = db.get_activity_members(activity_id)
    activity_name = db.get_table_row(DatabaseTables.AKTIVNOST,
                                     int(activity_id))[1]
    activity_date = get_croatian_date_format(
        db.get_table_row(DatabaseTables.AKTIVNOST, int(activity_id))[3])
    members_list = {}
    for member in sorted(activity_members, reverse=True, key=lambda x: x[3]):
        members_list[member[0]] = member[1:]
    return render_template("/activities/list_activity_members.html",
                           members_list=members_list,
                           activity_name=activity_name,
                           activity_date=activity_date,
                           activity_id=activity_id)
Beispiel #22
0
    def run(self):
        Strategy.strategy_state[self.tick['InstrumentID']] = True
        daybar_list = Indicators.getDayBarList(self.tick['InstrumentID'])
        if not daybar_list:
            print ('Need one bar at least in database')
            threading.Thread.__init__(self)
            Strategy.strategy_state[self.tick['InstrumentID']] = False
            return

        last_daybar = daybar_list[-1]
        #open, high, low, close, volume
        last_high = last_daybar[1]
        last_low = last_daybar[2]

        b_sendorder, s_sendorder = DatabaseController.getSendOrderCount(self.tick['InstrumentID'])

        if self.tick['LastPrice'] > last_high : # close short and open long
            if s_sendorder > 0 :
                self.PrepareOrder(self.tick['InstrumentID'], D_Buy, OF_Close, s_sendorder, self.tick['UpperLimitPrice']) # LastPrice
            elif b_sendorder < self.volume :
                self.PrepareOrder(self.tick['InstrumentID'], D_Buy, OF_Open, self.volume-b_sendorder, self.tick['UpperLimitPrice'])

        if self.tick['LastPrice'] < last_low : # close long and open short
            if b_sendorder > 0:
                self.PrepareOrder(self.tick['InstrumentID'], D_Sell, OF_Close, s_sendorder, self.tick['LowerLimitPrice'])  # LastPrice
            elif s_sendorder < self.volume :
                self.PrepareOrder(self.tick['InstrumentID'], D_Sell, OF_Open, self.volume-s_sendorder, self.tick['LowerLimitPrice'])

        # in case restart/reuse thread since python not support
        threading.Thread.__init__(self)
        Strategy.strategy_state[self.tick['InstrumentID']] = False
Beispiel #23
0
def load_logged_in_user():
    user_id = session.get('user_id')

    if user_id is None:
        g.user = None
    else:
        g.user = DatabaseController().get_row(DatabaseTables.KORISNICKI_RACUNI,
                                              'id', user_id)
Beispiel #24
0
def edit_activity_member_hours(activity_id):
    db = DatabaseController()
    if session['access_level'] == AccessLevels.ADMIN:
        all_members = db.get_activity_members(activity_id)
    else:
        all_members = db.get_activity_members(activity_id,
                                              section_specific=True)
    members_list = {}
    for member in all_members:
        members_list[member[0]] = member[1:]

    activity_name = db.get_table_row(DatabaseTables.AKTIVNOST,
                                     int(activity_id))[1]
    activity_date = get_croatian_date_format(
        db.get_table_row(DatabaseTables.AKTIVNOST, int(activity_id))[3])

    if request.method == 'POST':
        changed = False
        for member_id, member_data in members_list.items():
            member_name, member_last_name, member_hours, member_factor = member_data
            hours_worked = request.form["hoursworked%s" % member_id]
            factor = request.form["factor%s" % member_id]
            if not hours_worked or float(hours_worked) == 0:
                db.remove_member_activity_entry(activity_id, member_id)
                changed = True
            elif float(hours_worked) != member_hours or float(
                    factor) != member_factor:
                entry_values = (float(hours_worked), float(factor), member_id,
                                activity_id)
                db.edit_member_activity_entry(entry_values)
                changed = True
            else:
                pass

        if changed:
            flash("Sati volontiranja uspješno izmjenjeni", 'success')
        else:
            flash("Sati volontiranja su ostali isti", "info")

        return redirect(url_for('index'))

    return render_template('/activities/edit_member_hours.html',
                           members_list=members_list,
                           activity_name=activity_name,
                           activity_date=activity_date,
                           activity_id=activity_id)
def answer_operation_response(intent, session):
    card_title = "Choose number"
    speech_output = ""
    reprompt_text = "Oops. That is an invalid number. Please, choose a number from one to ten."
    should_end_session = False

    print(session)
    session_attributes = session['attributes']

    response = int(intent['slots']['number']['value'])

    #The number entered by the user is a result of an operation
    if response == session_attributes['expected_result']:
        session_attributes['grade'] += 1

    session_attributes['iterations'] -= 1

    if session_attributes['iterations'] == 0:
        #If the number of questions are over, finish it
        grade = session_attributes['grade']
        username = session_attributes['user']['name']
        userId = session['user']['userId']
        '''Update the user's general score in the database'''
        databaseController = DatabaseController()
        databaseController.update_score(userId, username, grade)
        '''Get the new user's score'''
        new_score = int(session_attributes['user']['total_score'] + grade)

        speech_output = """I hope you enjoyed playing with me, {}. Your grade was {} of 10! 
                           Your general score is {}. Say play again to start over or exit to quit.""".format(
            username, grade, new_score)
        should_end_session = False

        return build_response(
            session_attributes,
            build_speechlet_response(card_title, speech_output, reprompt_text,
                                     should_end_session))

    expected_result, speech_output = get_random_operation(session_attributes)
    session_attributes['expected_result'] = int(expected_result)

    return build_response(
        session_attributes,
        build_speechlet_response(card_title, speech_output, reprompt_text,
                                 should_end_session))
Beispiel #26
0
def get_interval_member_activity(start_date=None, end_date=None, section=None):
    db = DatabaseController()

    if section is None:
        section = session['section']

    if start_date is None:
        start_date = datetime.date.today().replace(day=1)
    if end_date is None:
        end_date = start_date + relativedelta(months=+1, days=-1)
    members_activity = db.get_period_activity(start_date=start_date,
                                              end_date=end_date)

    total_activity = {}
    total_activity_weight = {}
    for member in members_activity:
        key = member[0]
        # Prikaži člana ako je član tvoje sekcije ili ako si admin
        test = db.get_all_members_sections(key)
        if section in test or session['access_level'] == AccessLevels.ADMIN:
            if key not in total_activity:
                total_activity[key] = 0
                total_activity_weight[key] = 0
            total_activity[key] += member[3]  # Beztežinski sati
            total_activity_weight[key] += (member[3] * member[4])  # Množi sate sa faktorom

    if session['access_level'] == AccessLevels.ADMIN:
        members = db.get_all_rows_from_table(DatabaseTables.CLAN)
    else:
        members = db.get_all_members()

    members_list = {}
    for member in members:
        if db.is_member_active(member[0]):
            key = member[0]
            if key not in total_activity:
                member_hours = 0
                member_hours_w = 0
            else:
                member_hours = total_activity[key]
                member_hours_w = total_activity_weight[key]

            members_list[member[0]] = member[1:4] + (member_hours, member_hours_w) + (db.get_member_primary_section(member[0]),)

    if session["access_level"] >= AccessLevels.SAVJETNIK:
        sorted_list = sorted(members_list.items(), reverse=True,
                             key=lambda x: (x[1][3], x[1][4]))  # Sati -> Težinski -> Prezime
    else:  # Sekcija -> Sati -> Težinski
        sorted_list = sorted(members_list.items(), reverse=True, key=lambda x: (x[1][-1], x[1][3], x[1][4]))

    sorted_members = {}
    for k, v in sorted_list:
        section_tmp = Utilities.sections[v[-1]]
        if section_tmp not in sorted_members:
            sorted_members[section_tmp] = {}
        sorted_members[section_tmp][k] = v[:-1]

    return sorted_members
Beispiel #27
0
def section_stats():
    db = DatabaseController()
    if request.method == 'POST':
        if request.form['start_date'] == '':
            start = datetime.date.today() + relativedelta(months=-1)
            start_year, start_month, start_day = start.year, start.month, start.day
        else:
            start_year, start_month, start_day = [int(x) for x in request.form['start_date'].split('-')]

        if request.form['end_date'] == '':
            current_date = datetime.date.today()
            end_year = current_date.year
            end_month = current_date.month
            end_day = current_date.day
        else:
            end_year, end_month, end_day = [int(x) for x in request.form['end_date'].split('-')]
    else:
        current_date = datetime.date.today()
        end_year = current_date.year
        end_month = current_date.month
        end_day = current_date.day

        start = datetime.date(end_year, end_month, end_day) + relativedelta(months=-1)
        start_year, start_month, start_day = start.year, start.month, start.day

    start_date = datetime.date(start_year, start_month, start_day)
    end_date = datetime.date(end_year, end_month, end_day)

    start_monthd = start_date.strftime("%m")  # monthd - formatiran mjesec kao dvoznamenkasti broj (zbog defult inputa)
    end_monthd = end_date.strftime("%m")

    section_event_count = {}
    for section in Utilities.sections:
        section_events = db.get_all_volunteering_events(section, start_date, end_date)
        section_event_count[section] = len(section_events)

    months = (start_month, end_month)
    monthds = (start_monthd, end_monthd)
    years = (start_year, end_year)
    days = (start_day, end_day)
    return render_template('/statistics/section_stats.html', days=days, months=months, monthds=monthds,
                           sections=Utilities.sections, years=years, section_count=section_event_count)
Beispiel #28
0
def index():
    db = DatabaseController()
    start_date = datetime.datetime.today().replace(day=1).date()
    end_date = datetime.datetime.today().replace(month=datetime.datetime.today().month % 12 + 1, day=1).date()
    members_activity = db.get_period_activity(start_date, end_date)
    total_activity = {}
    member_names = {}
    for member in members_activity:
        key = member[0]
        if key not in total_activity:
            total_activity[key] = 0
        total_activity[key] += member[3]  # Beztežinski sati
        member_names[key] = "%s %s" % (member[1], member[2])

    sorted_list = sorted(total_activity.items(), reverse=True, key=lambda x: x[1])

    sorted_activity = {}
    for k, v in sorted_list:
        sorted_activity[k] = v

    return render_template("index.html", list_records=sorted_activity, names=member_names)
Beispiel #29
0
def edit_activity_type(type_id):
    db = DatabaseController()
    activity_type = db.get_row(DatabaseTables.TIP_AKTIVNOSTI, 'id', type_id)
    if request.method == 'POST':
        name = request.form['name']
        description = request.form['description']

        try:
            section = request.form['section']
        except HTTPException:
            section = session["section"]

        error = None

        if not name:
            error = 'Name is required.'
        if not description:
            description = '-'
        if activity_type[1] != name and db.activity_type_exists(name):
            error = 'Tip aktivnosti %s već postoji u bazi podataka' % name

        if error is None:
            entry_values = (name, description, section)
            db.edit_activity_type(type_id, entry_values)
            flash("Tip aktivnosti %s je uspješno izmjenjen!" % name, 'success')
            return redirect(url_for('index'))

        flash(error, 'danger')

    return render_template('/activities/edit_type.html',
                           activity_type=activity_type[1:],
                           activity_type_id=activity_type[0],
                           sections=Utilities.sections_and_teams)
Beispiel #30
0
def add_activity():
    db = DatabaseController()
    if request.method == 'POST':
        name = request.form['name']
        description = request.form['description']
        activity_start_date = request.form['date']
        type = request.form['type']
        try:
            section = request.form['section']
        except HTTPException:
            section = session["section"]

        error = None

        if not name:
            error = 'Username is required.'
        if not description:
            description = '-'
        if not activity_start_date:
            error = 'Date is required'
        if not type:
            error = 'Activity type value is required.'

        if error is None:
            activity_start_date = get_date_object(activity_start_date)
            entry_values = (name, description, activity_start_date, section,
                            type)

            if db.activity_exists(name, activity_start_date, section):
                flash(
                    "Aktivnost sa istim imenom i datumom za ovu sekciju već postoji u bazi",
                    "info")
                return redirect(url_for('activities.add_activity'))

            db.add_activity_entry(entry_values)
            activity_id = db.get_last_row_id()

            flash("Aktivnost %s je uspješno dodana!" % name, 'success')
            if request.form['action'] == "member_to_activity":
                flash(
                    "Preusmjeren si na formu za dodavanje članova koji su došli na aktivnost.",
                    'info')
                return redirect(
                    url_for('activities.add_members_to_activity',
                            activity_id=activity_id))
            return redirect(url_for('index'))

        flash(error, 'danger')

    activity_types = {}
    for row in db.get_all_rows_from_table(DatabaseTables.TIP_AKTIVNOSTI):
        activity_types[row[0]] = (row[1], row[3])

    return render_template('/activities/add.html',
                           activity_types=activity_types)
Beispiel #31
0
def add_members_to_activity(activity_id):
    db = DatabaseController()
    if session[
            "access_level"] >= AccessLevels.SAVJETNIK:  # Ako je razina ovlasti savjetnik ili manja, dohvati matičnu sekciju samo
        all_members = db.get_all_members()
    else:
        all_members = db.get_all_members_admin(
        )  # Za admina dohvati sve članove
    members_list = {}
    for member in all_members:
        member_id = member[0]
        if db.is_member_active(member_id) and not db.member_activity_exists(
                member_id, activity_id):
            members_list[member[0]] = member[1:]

    if session["access_level"] >= AccessLevels.SAVJETNIK:
        sorted_list = sorted(
            members_list.items(),
            key=lambda x: x[1][1])  # Sort po prezimenu ako je unutar sekcije
    else:
        sorted_list = sorted(
            members_list.items(), key=lambda x:
            (x[1][-1], x[1][1]))  # Sort po sekciji prvo pa prezimenu

    sorted_members = {}
    for k, v in sorted_list:
        sorted_members[k] = v

    if request.method == 'POST':
        for member_id, _ in sorted_members.items():
            if request.form.get('checkbox%s' % member_id):
                hours_worked = request.form["hoursworked%s" % member_id]
                factor = request.form["factor%s" % member_id]
                if float(hours_worked) != 0:
                    if not db.member_activity_exists(member_id, activity_id):
                        db.add_member_activity_entry(
                            (member_id, activity_id, hours_worked, factor))

        flash("Volonterski sati su uspješno izmjenjeni!", "success")
        return redirect("/activities/list")

    return render_template("/activities/add_members_to_activity.html",
                           activity_id=activity_id,
                           members_list=sorted_members)
 def __init__(self, threadid, filepath, sliceStart, sliceEnd):
     threading.Thread.__init__(self)
     self.sliceStart = sliceStart
     self.sliceEnd = sliceEnd
     self.threadid = threadid
     self.itemsToInsert = []
     self.dbCtrl = DatabaseController()
     self.dump_id = self.dbCtrl.insertDump('Adobe 2013 Mega dump')
     self.lineCounter = 0
     try:
         self.file = open(filepath, 'r')
     except IOError, e:
         print('opening failed: ' + e)
Beispiel #33
0
class ThreadedUploader:
    def __init__(self, queueSize=4500):
        self.stopped = False
        self.queue = Queue(maxsize=queueSize)
        self.db_updater = DatabaseController()
        self.s3 = S3Controller()

    def start(self):
        thread = Thread(target=self.work, args=())
        thread.daemon = True
        thread.start()
        return self

    def work(self):
        while True:
            if self.stopped:
                return

            if self.queue.empty():
                time.sleep(2)
                continue

            # upload stuff
            (frame, attr) = self.queue.get()
            now = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
            frame_name = "{}_{}_{}.jpg".format(attr['job_id'],
                                               attr['frame_no'], now)
            self.db_updater.insertOutput(attr['job_id'], attr['vid_time'],
                                         attr['frame_no'], attr['ssim'],
                                         frame_name)
            jpg_byte = cv2.imencode('.jpg', frame)[1].tostring()
            self.s3.upload(frame_name, jpg_byte, 'image/jpg')

    def put(self, frame, attr):
        self.queue.put((frame, attr))

    def stop(self):
        self.stopped = True
Beispiel #34
0
 def __init__(self):
     self.dump_id = ''
     self.dump_url = ''
     self.dbAccess = DatabaseController()
Beispiel #35
0
    def main(self, argv):

        #
        # 1. Argument parsing
        #
        self.logger.debug("The total numbers of args passed to the script: %d " % len(argv))
        self.logger.debug("Args: " + str(argv))

        # TODO: flesh out argument parsing and help, -h
        # Parse command line arguments
        sDescription = "no description yet"

        sEpilog = "Returns exit code 0 on success, non-zero on error.\n\n" \
                  "Use app.conf to change script configuration.\n" \
                  "Use logging.conf to change logging information."

        parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=sDescription,
                                         epilog=sEpilog)
        parser.add_argument('-host', help='hostname to ping')
        parser.add_argument('-f', '--file', help='file containing list of hosts to ping')
        parser.add_argument('-c', '--conf', default='app.conf', help='application config file to use (default: %(default)s)')
        args = parser.parse_args()
        self.logger.debug("Parsed args: " + str(args))

        appConfFile = os.path.join(os.path.dirname(__file__), args.conf)
        if not os.path.isfile(appConfFile):
            self.logger.error(appConfFile + " not found.")
            return

        hostnames = []
        if args.host:
            hostnames.append(args.host)

        # Also get the list of hosts from a file
        if args.file:
            if not os.path.isfile(args.file):
                self.logger.error("File not found: " + args.file)
                return
            else:
                with open(args.file, 'r') as file:
                    for line in file:
                        line = line.strip()
                        if line:
                            hostnames.append(line)

        #
        # 2. Controller setup
        #

        # Get the configuration parameters
        self.configController = ConfigController()
        self.configController.loadConfiguration(appConfFile)

        # Establish a database connection
        self.databaseController = DatabaseController(True)
        self.databaseController.connect(self.configController.getDbConnectString())
        self.devices = self.databaseController.getDevices()

        # Get a ping controller instance
        self.pingController = PingController()
        self.pingCount = self.configController.getPingCount()

        # Setup email controller
        self.emailConf = self.configController.getEmailTuple()
        self.emailController = EmailController(self.emailConf.username, self.emailConf.password, self.emailConf.server, self.emailConf.port, self.emailConf.sender_name)

        #
        # 3. Script main run
        #
        # Finally we use the list of hostnames
        self.logic(hostnames)

        self.databaseController.disconnect()

        return 0
Beispiel #36
0
 def OnRtnTrade(self, pTrade):
     logger.info('OnRtnTrade %s' % str(pTrade))
     DatabaseController.insert_RtnOrder(pTrade)
Beispiel #37
0
class Controller:
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.devices = {}
        self.configController = None
        self.databaseController = None
        self.pingController = None
        self.emailController = None
        self.emailConf = None
        self.pingCount = None
        return

    def __del__(self):
        return

    def main(self, argv):

        #
        # 1. Argument parsing
        #
        self.logger.debug("The total numbers of args passed to the script: %d " % len(argv))
        self.logger.debug("Args: " + str(argv))

        # TODO: flesh out argument parsing and help, -h
        # Parse command line arguments
        sDescription = "no description yet"

        sEpilog = "Returns exit code 0 on success, non-zero on error.\n\n" \
                  "Use app.conf to change script configuration.\n" \
                  "Use logging.conf to change logging information."

        parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=sDescription,
                                         epilog=sEpilog)
        parser.add_argument('-host', help='hostname to ping')
        parser.add_argument('-f', '--file', help='file containing list of hosts to ping')
        parser.add_argument('-c', '--conf', default='app.conf', help='application config file to use (default: %(default)s)')
        args = parser.parse_args()
        self.logger.debug("Parsed args: " + str(args))

        appConfFile = os.path.join(os.path.dirname(__file__), args.conf)
        if not os.path.isfile(appConfFile):
            self.logger.error(appConfFile + " not found.")
            return

        hostnames = []
        if args.host:
            hostnames.append(args.host)

        # Also get the list of hosts from a file
        if args.file:
            if not os.path.isfile(args.file):
                self.logger.error("File not found: " + args.file)
                return
            else:
                with open(args.file, 'r') as file:
                    for line in file:
                        line = line.strip()
                        if line:
                            hostnames.append(line)

        #
        # 2. Controller setup
        #

        # Get the configuration parameters
        self.configController = ConfigController()
        self.configController.loadConfiguration(appConfFile)

        # Establish a database connection
        self.databaseController = DatabaseController(True)
        self.databaseController.connect(self.configController.getDbConnectString())
        self.devices = self.databaseController.getDevices()

        # Get a ping controller instance
        self.pingController = PingController()
        self.pingCount = self.configController.getPingCount()

        # Setup email controller
        self.emailConf = self.configController.getEmailTuple()
        self.emailController = EmailController(self.emailConf.username, self.emailConf.password, self.emailConf.server, self.emailConf.port, self.emailConf.sender_name)

        #
        # 3. Script main run
        #
        # Finally we use the list of hostnames
        self.logic(hostnames)

        self.databaseController.disconnect()

        return 0

    # TODO: need a better function name than "logic"...
    def logic(self, hostnames):

        self.logger.debug("Hostnames: " + str(hostnames))

        # list of ping result tuples for failed pings
        failedPingResults = []

        # list of ping result tuples for successful pings
        successfulPingResults = []

        # list of ping result tuples from the database
        latestPingResults = self.databaseController.getLatestPingResults()

        # list of ping result tuples for successful pings that were previously failures
        failsuccessPingResults = []

        for hostname in hostnames:
            # If this is a new device then add it to the database
            if hostname not in self.devices:
                self.logger.debug("Found new device: " + hostname)
                device = Device(None, hostname, 1)
                newDevice = self.databaseController.addDevice(device)
                self.devices[hostname] = newDevice

            deviceId = self.devices[hostname].device_id

            pingResult = self.pingController.ping(hostname, self.pingCount, deviceId)
            if pingResult:
                successfulPingResults.append(pingResult)
            else:
                # New ping result with is_success=False
                pingResult = PingResult(device_id=deviceId, is_success=False)
                failedPingResults.append(pingResult)

            self.databaseController.addPingResult(pingResult)

        # Check if this device was previously down but is now up
        for pingResult in successfulPingResults:
            if pingResult.device_id in latestPingResults:
                prevIsSuccess = latestPingResults[pingResult.device_id].is_success
                datePinged = latestPingResults[pingResult.device_id].date_pinged
                now = datetime.datetime.now()
                minutesDownFor = math.ceil((now-datePinged).total_seconds()/60)
                if prevIsSuccess is not 1:
                    device = self.getDeviceById(pingResult.device_id)
                    self.logger.debug("Host '" + str(device.hostname) + "' was previously down for up to " + str(minutesDownFor) + " minutes but is now up.")
                    failsuccessPingResults.append(pingResult)

        # Check if this device was previously down and is still down
        # Copy failedPingResults into failedPingResultsCopy to avoid loop issues while deleting elements...
        # TODO: probably a better way of doing this
        failedPingResultsCopy = list(failedPingResults)
        for pingResult in failedPingResultsCopy:
            if pingResult.device_id in latestPingResults:
                prevIsSuccess = latestPingResults[pingResult.device_id].is_success
                if prevIsSuccess is not 1:
                    device = self.getDeviceById(pingResult.device_id)
                    self.logger.debug("Host '" + str(device.hostname) + "' was previously down and is still down.")
                    failedPingResults.remove(pingResult)

        # Send an email
        if failedPingResults or failsuccessPingResults:
            if not self.emailConf.enabled:
                self.logger.debug("Email is disabled. Skipping email...")
            else:
                self.logger.debug("Email is enabled: " + str(self.emailConf.enabled))
                subject = 'FoscamPing Email Controller'
                timeNow = time.strftime('%Y-%m-%d %H:%M:%S')
                text = ''

                for failedPingResult in failedPingResults:
                    device = self.getDeviceById(failedPingResult.device_id)
                    text += timeNow + ' - Failed to ping: ' + device.hostname + "\n"

                for pingResult in failsuccessPingResults:
                    device = self.getDeviceById(pingResult.device_id)
                    datePinged = latestPingResults[pingResult.device_id].date_pinged
                    now = datetime.datetime.now()
                    minutesDownFor = math.ceil((now-datePinged).total_seconds()/60)
                    text += timeNow + ' - Previously down for up to ' + str(minutesDownFor) + ' minutes but is now up: ' + device.hostname + "\n"

                self.emailController.sendEmail(self.emailConf.send_to, subject, text)

        return

    def getDeviceById(self, device_id):
        """
        Helper function to get a Device tuple by device_id
        :param device_id: device_id to search for.
        :return: Device tuple if found, else None.
        """
        for hostname in self.devices:
            # if device.device_id == device_id:
            if self.devices[hostname].device_id == device_id:
                return self.devices[hostname]
        self.logger.error("Device id not found" + str(device_id))
        return None
Beispiel #38
0
 def __init__(self, url):
     self.url = url
     self.dbAccess = DatabaseController()