Exemplo n.º 1
0
  def post(self):
    if not self._check_authentication():
      return

    params = self._get_parameters("slot", "date")
    if not params:
      return
    slot = int(params[0])
    date = make_date(params[1])
    
    name = self._get_name()

    # Find the room we're looking for.
    to_delete = Slot.query(ndb.AND(Slot.date == date, Slot.slot == slot)).get()
    if not to_delete:
      self._exit_handler("Slot not reserved.")
      return
    room = to_delete.room

    props = Slot.query(ndb.AND(Slot.date == date, Slot.owner == name,
        Slot.room == room))
    block_edges = self._find_block_edges(props)
    
    if to_delete.slot not in block_edges:
      self._exit_handler("Cannot delete slot in middle of block.")
      return
      
    to_delete.key.delete()
    logging.info("Deleted slot.")

    self._exit_handler()
    return
Exemplo n.º 2
0
    def unstretch_slot(self, slot_id):
        slot = self.get_one(slot_id, strict=True)
        if not slot.can_be_unstretched: raise CannotBeUnstretched()
        slot.duration -= 60

        new_slot = Slot(room=slot.room, status='empty', blocked=slot.blocked, duration=60)
        new_slot.begins = slot.begins + timedelta(minutes=slot.duration)

        db.session.add(new_slot)
        db.session.add(slot)
        db.session.commit()
        return slot
Exemplo n.º 3
0
def createSlot():
    slot_name = request.form['slot_name']
    slot_type = request.form['slot_type']
    slot_obj = Slot(name=slot_name, type_slot=slot_type)
    db.session.add(slot_obj)
    db.session.commit()

    return render_template("success.html", message="Added slot to DB")
Exemplo n.º 4
0
 def start_slot(self):
     if self.current_slot:
         self.stop_slot()
     project, task = self.current_project_task()
     self.current_slot = Slot(project, task)
     self.session.add(self.current_slot)
     self.session.commit()
     self.ui.stop_button.setEnabled(True)
     self.ui.start_button.setEnabled(False)
Exemplo n.º 5
0
def set_slot(num: int) -> Slot:
    """Set roulette Slot numbers and colors."""

    blacks: List[int] = [
        2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35
    ]
    color = 'RED'
    if num in blacks:
        color = 'BLACK'
    elif num == 0:
        color = 'WHITE'

    return Slot(number=num, color=color)
Exemplo n.º 6
0
  def get(self):
    # Get logged in user.
    user = users.get_current_user()
    
    # If the user is not logged in, ask them to.
    if not user:
      html = '<a href="%s">Sign In</a>' % users.create_login_url('/')
      # html = '<a href="%s">Sign In</a>' % users.create_login_url('/')
      self.response.write(html)
      return

    # As an example, create and save one slot that starts now.
    time = datetime.datetime.now()
    date = time.date()
    midnight = datetime.datetime.combine(date, datetime.time())
    seconds = (time - midnight).seconds
    slot = seconds / 60 / 30
    name = user.nickname().replace(".", " ").title()
    slot = Slot(owner = name, date = date, slot = slot)
    slot.put()
    
    # Load last 10 slots.
    slots = Slot.query().order(-Slot.slot).fetch(10)
    
    # Set values to pass to HTML template.
    # added 'days' & 'hours' values - Jun21
    # Pass username in the same format as it is stored in DB - Jun24
    template_values = {
      'user': user,
      'slots': slots,
      'days': range(0,7),
      'hours': range(0, 24),
      'username': name,
    }
    template = JINJA_ENVIRONMENT.get_template('index.html')
    self.response.write(template.render(template_values))
Exemplo n.º 7
0
  def get(self):
    if not self._check_authentication():
      return

    params = self._get_parameters("date", "room")
    if not params:
      return
    date = make_date(params[0])
    room = params[1]
   
    slots = Slot.query(ndb.AND(Slot.date == date, Slot.room == room))
   
    response = []
    for slot in slots:
      dateParam = convert_date_to_string(slot.date)
      data = {"date": dateParam, "slot": slot.slot, "owner": slot.owner}
      # data = {"slot": slot.slot, "owner": slot.owner}

      response.append(data)

    self.response.out.write(json.dumps(response))
Exemplo n.º 8
0
	def put(self, jwt):
		body = request.get_json()

		title = body['title']
		description = body['description']
		start_time = body['start_time']
		capacity = body['capacity']

		try:
			slot = Slot()

			slot.title = title
			slot.description = description
			slot.start_time = start_time
			slot.capacity = capacity

			db.session.add(slot)
			db.session.commit()
		except:
			db.session.rollback()
			abort(422, "unprocessable")

		return jsonify( { "success": True, "id": slot.id } )
Exemplo n.º 9
0
  def post(self):
    if not self._check_authentication():
      return
    
    params = self._get_parameters("slot", "date", "room")
    if not params:
      return
    slot = int(params[0])
    date = make_date(params[1])
    room = params[2]

    # Make sure they're not booking too far in advance.
    advance = date - datetime.datetime.now().date()
    advance = advance.days
    if advance > self.advance_booking:
      self._exit_handler("User booked too far in advance.")
      return

    # Make sure we're not double-booking.
    if Slot.query(ndb.AND(Slot.date == date, Slot.room == room,
        Slot.slot == slot)).get():
      self._exit_handler("User double-booked slot %d." % (slot))
      return

    name = self._get_name()
       
    # Slots reserved by the same user must be at least 2 hours apart.
    empty = self.empty_time / self.slot_length
    slots = Slot.query(ndb.AND(Slot.date == date, Slot.owner == name,
        Slot.room == room))

    block_edges = self._find_block_edges(slots)

    # Make sure we're not booking too large a block.
    failed = False
    for i in range(0, len(block_edges)):
      edge = block_edges[i]
      if (edge - slot == 1 and i % 2 == 0):
        # Adding to beginning.
        if block_edges[i + 1] - slot >= self.max_slots:
          failed = True
      elif (slot - edge == 1 and i % 2 == 1):
        # Adding to end.
        if slot - block_edges[i - 1] >= self.max_slots:
          failed = True

    if failed:
      self._exit_handler("User cannot book this many consecutive slots.")
      return

    # Only worth doing this if there are other slots booked.
    if len(block_edges) != 0:
      # Find the edges that are closest to our slot and get rid of everything
      # else.
      block_edges.append(slot)
      block_edges.sort()
      position = block_edges.index(slot)
      if position == 0:
        # This is a special case.
        block_edges = [block_edges[1]]
      else:
        block_edges = block_edges[(position - 1):]
        block_edges = block_edges[:3]
        block_edges.remove(slot)
      
      for booked_slot in block_edges:
        logging.info("Booked slot: %d." % (booked_slot))
        if (abs(int(booked_slot) - slot) != 1 and \
            abs(int(booked_slot) - slot) <= empty):
          self._exit_handler("User did not leave enough space between blocks.")
          return

    slot = Slot(owner = name, slot = slot, date = date, room = room)
    slot.put()
    logging.info("Saved slot.")

    self._exit_handler()
Exemplo n.º 10
0
class QTracker(QtCore.QObject):
    db_path = os.path.expanduser('~/.config/qtracker/db.sqlite3')
    current_slot = False
    cp = None
    ct = None

    def __init__(self):
        QtCore.QObject.__init__(self)
        self.mw = QTrackerMW()
        self.ui = qtracker.Ui_QTracker()
        self.ui.setupUi(self.mw)

        self.pw = QtGui.QWidget()
        self.pui = project.Ui_widget()
        self.pui.setupUi(self.pw)

        self.tw = QtGui.QWidget()
        self.tui = task.Ui_widget()
        self.tui.setupUi(self.tw)
        #print "DB path => %s" % self.db_path
        self.engine = create_engine('sqlite:///%s' % self.db_path, echo=False)
        self._session = sessionmaker(bind = self.engine)
        self.meta = Base.metadata
        self.meta.create_all(self.engine)
        self.session = self._session()
        QtCore.QObject.connect(self.ui.addproject_button, QtCore.SIGNAL('clicked()'), self.pw.show)
        QtCore.QObject.connect(self.ui.addtask_button, QtCore.SIGNAL('clicked()'), self.tw.show)
        QtCore.QObject.connect(self.pui.button_box, QtCore.SIGNAL('accepted()'), self.add_project)
        QtCore.QObject.connect(self.tui.button_box, QtCore.SIGNAL('accepted()'), self.add_task)
        QtCore.QObject.connect(self.ui.project_combo, QtCore.SIGNAL('activated(int)'), self.choose_project)
        QtCore.QObject.connect(self.ui.start_button, QtCore.SIGNAL('clicked()'), self.start_slot)
        QtCore.QObject.connect(self.ui.stop_button, QtCore.SIGNAL('clicked()'), self.stop_slot)
        QtCore.QObject.connect(self.ui.rebuild, QtCore.SIGNAL('clicked()'), self.build)
        self.mw.close_signal.connect(self.stop_slot)
        self.build()
        self.build_tray_menu()
        self.choose_project(self.ui.project_combo.currentIndex())
        self.mw.show()

    def build_tray_menu(self):
        tr = QtGui.QIcon()
        tr.addPixmap(QtGui.QPixmap(":/icons/foto.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.tray = QtGui.QSystemTrayIcon(tr, self)
        self.tray.show()
        self.traymenu = QtGui.QMenu("QTracker Tray Menu")
        self.tm_switch = QtGui.QAction(self.mw)
        self.tm_switch.setText("&Switch to")
        self.traymenu.addAction(self.tm_switch)
        #self.about_widget.connect(self.tm_about, QtCore.SIGNAL('triggered()'), QtCore.SLOT('show()'))
        for i in self.projects:
            a = QtGui.QAction(self.mw)
            a.setText(i.name)
            self.traymenu.addAction(a)


        # self.tm_about = QAction(self.mw)
        # self.tm_about.setText(QApplication.translate("MainWindow", "&About", None, QApplication.UnicodeUTF8))
        # self.about_widget.connect(self.tm_about, QtCore.SIGNAL('triggered()'), QtCore.SLOT('show()'))

        # self.tm_close = QAction(self.mw)
        # self.tm_close.setText(QApplication.translate("MainWindow", "&Close", None, QApplication.UnicodeUTF8))
        # self.mw.connect(self.tm_close, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))

        # self.traymenu.addAction(self.tm_about)
        # self.traymenu.addAction(self.tm_close)
        QtCore.QObject.connect(self.tray, QtCore.SIGNAL('activated(QSystemTrayIcon::ActivationReason)'), self.show_main)
        self.tray.setContextMenu(self.traymenu)

    @QtCore.pyqtSlot(int)
    def show_main(self, value = -3):
        if value in [2, 3]:
            if self.mw.isVisible():
                self.mw.hide()
            else:
                self.mw.show()


    def current_project_task(self):
        _p = _t = None
        if self.ui.project_combo.currentIndex() >= 0:
            _p = self.projects[self.ui.project_combo.currentIndex()]
            if self.ui.task_combo.currentIndex() >= 0:
                _t = _p.tasks[self.ui.task_combo.currentIndex()]
        return _p, _t


    def start_slot(self):
        if self.current_slot:
            self.stop_slot()
        project, task = self.current_project_task()
        self.current_slot = Slot(project, task)
        self.session.add(self.current_slot)
        self.session.commit()
        self.ui.stop_button.setEnabled(True)
        self.ui.start_button.setEnabled(False)

    def stop_slot(self):
        if not self.current_slot:
            return
        self.current_slot.stop()
        time_for_slot = self.current_slot.end - self.current_slot.start
        p = self.current_slot.project
        p.time_spent += time_for_slot.seconds
        t = self.current_slot.task
        if t:
            t.time_spent += time_for_slot.seconds
            self.session.add(t)
        self.session.add(p)
        self.session.add(self.current_slot)
        self.session.commit()
        self.ui.stop_button.setEnabled(False)
        self.ui.start_button.setEnabled(True)
        self.current_slot = False
        #self.build()


    def build(self, project = None, task = None):
        _p = _t = None
        if project:
            _p = project
        else:
            if self.ui.project_combo.currentIndex() >= 0:
                _p = self.projects[self.ui.project_combo.currentIndex()]
        if task:
            _t = task
        # else:
        #     if self.ui.task_combo.currentIndex() >= 0:
        #         _t = _p.tasks[self.ui.task_combo.currentIndex()]

        print "P T", _p, _t
        self.tui.project_combo.clear()
        self.ui.project_combo.clear()
        self.ui.time_report.clear()
        self.projects = self.session.query(Project).order_by('name').all()
        for p in self.projects:
            print "Tasks for %s =>" % p, p.tasks
            self.tui.project_combo.addItem(p.name)
            self.ui.project_combo.addItem(p.name)
            parent = QtGui.QTreeWidgetItem(self.ui.time_report, [p.name, htime(p.time_spent), str(len(p.slots))])
            parent.setExpanded(True)
            for t in p.tasks:
                parent.addChild(QtGui.QTreeWidgetItem([t.name, htime(t.time_spent), str(len(t.slots))]))
        if _p:
            self.choose_project(self.projects.index(_p))
            if _t:
                self.ui.task_combo.setCurrentIndex(_p.tasks.index(_t))
        

    def add_project(self):
        name = str(self.pui.newproject_line.text())
        print "Devo creare il progetto =>", name
        exists = self.session.query(Project).filter_by(name = name).all()
        if exists:
            self.pui.error.setText("Project %s already exixts!" % name)
            return
        self.pui.error.setText("")
        p = Project(name)
        self.session.add(p)
        self.session.commit()
        print p.id
        self.pw.hide()
        self.pui.newproject_line.setText('')
        self.build(project)

    def add_task(self):
        name = str(self.tui.newtask_line.text())
        p = self.tui.project_combo.currentIndex()
        project = self.projects[p]
        print "Adding task %s for project %s" % (name, p)
        exists = self.session.query(Task).filter_by(name = name, project = project).all()
        if exists:
            self.tui.error.setText("Task %s for project %s already exists!" % (name, project.name))
            return

        self.tui.error.setText("")
        t = Task(name, project)
        self.session.add(t)
        self.session.commit()
        self.tw.hide()
        self.tui.newtask_line.setText('')
        self.build(project, t)

    def choose_project(self, index, task = None):
        if index < 0:
            self.ui.task_combo.clear()
            return
        self.tui.project_combo.setCurrentIndex(index)
        self.ui.task_combo.clear()
        for t in self.projects[index].tasks:
            self.ui.task_combo.addItem(t.name)
        if task:
            self.ui.task_combo.setIndex(task)