Example #1
0
 def add_subject(school_id, title, description, deadline):
     """add subject
     """
     subject = Subject(school_id=school_id,
                       title=title,
                       description=description,
                       create_time=getdate_now(),
                       deadline=deadline)
     subject.save()
Example #2
0
    def get_subject_filter(school_id, listtype):
        """get school's subject filter

        listtype:
        - 0 to show all
        - 1 to show on-going
        - 2 to show overdue
        """
        ret = {'school_id': school_id}
        if listtype == 0:
            pass
        elif listtype == 1:
            ret['deadline__gt'] = getdate_now()
        elif listtype == 2:
            ret['deadline__lte'] = getdate_now()
        else:
            return None
        return ret
Example #3
0
 def add_apply(user_id, school_id, message):
     """add school
     """
     apply = SchoolApply(user_id=user_id,
                         school_id=school_id,
                         message=message,
                         apply_time=getdate_now())
     apply.save()
     return True
Example #4
0
    def upload(prog_id):
        """when the prgram is uploaded
        """
        progs = Program.objects.filter(id=prog_id)
        if progs.exists():
            program = progs.last()
        else:
            return False

        program.status = 5
        program.upload_time = getdate_now()
        program.save()
        return True
Example #5
0
 def add_message(chat_id, user_id):
     """user send a message in a chat
     """
     chats = Chat.objects.filter(id=chat_id)
     if chats.exists():
         chat = chats.last()
         if user_id in [chat.user_1, chat.user_2]:
             chat.latest_time = getdate_now()
             chat.latest_sender = user_id
             chat.unread_count = chat.unread_count + 1
             chat.save()
             return True
     return False
Example #6
0
 def build_chat(user_1, user_2):
     """build chat between user_1 and user_2
     """
     chat_id = ChatHelper.get_chat(user_1, user_2)
     if chat_id is None:
         if user_1 > user_2:
             user_1, user_2 = user_2, user_1
         chat = Chat(user_1=user_1,
                     user_2=user_2,
                     latest_time=getdate_now())
         chat.save()
         chat_id = chat.id
     return chat_id
Example #7
0
 def disconnect(token, ip_address):
     """disconnect a session
     """
     if not isinstance(token, str) or not isinstance(ip_address, str):
         return False
     nowdate = getdate_now()
     sessions = Session.objects.filter(token=token,
                                       ip=ip_address,
                                       end_time__gt=nowdate)
     if sessions.exists():
         session = sessions.last()
         session.end_time = nowdate
         session.save()
     return True
Example #8
0
 def get_session_id(token, ip_address):
     """get session id by token and ip
     """
     if not isinstance(token, str) or not isinstance(ip_address, str):
         return None
     sessions = Session.objects.filter(token=token,
                                       ip=ip_address,
                                       end_time__gt=getdate_now())
     if sessions.exists():
         session = sessions.last()
         session.end_time = getdate_later()
         session.save()
         return session.id
     return None
Example #9
0
 def undo_message(user_id, message_id):
     """get messages between user_1 and user_2
     """
     qs = Message.objects.filter(id=message_id, valid=True)
     if not qs.exists():
         return False
     message = qs.last()
     if message.sender != user_id:
         return False
     if message.send_time + timezone.timedelta(seconds=5) < getdate_now():
         return False
     message.valid = False
     message.save()
     return True
Example #10
0
    def judge_program(prog_id, status, admin_id):
        """judge program
        """
        progs = Program.objects.filter(id=prog_id)
        if progs.exists():
            program = progs.last()
        else:
            return False

        program.judge = admin_id
        program.status = status
        program.judge_time = getdate_now()

        program.save()
        return True
Example #11
0
 def add_video(user_id, title, description, filename, filepath, school, category, video_size):
     """add video
     """
     video = Video(
         uploader=user_id,
         title=title,
         filename=filename,
         filepath=filepath,
         description=description,
         school=school,
         category=category,
         video_size=video_size,
         upload_time=getdate_now())
     video.save()
     return video.id
Example #12
0
    def add_session(ip_address, default_token=None):
        """add a session into database

        return token
        """
        if not isinstance(ip_address, str):
            return None
        token = default_token
        while token is None or SessionHelper.get_session_id(
                token, ip_address) is not None:
            token = randkey()
        Session(token=token,
                ip=ip_address,
                start_time=getdate_now(),
                end_time=getdate_later()).save()
        return token
Example #13
0
    def add_code(session_id, phone, default_code='GUXYNB'):
        """get the EntryLog by session_id.
        """
        if not VerifyHelper.del_codes(session_id, phone):
            return None

        if default_code is None:
            code = randkey(length=6)
        else:
            code = default_code
        VerifyCode(
            session_id=session_id,
            phone=phone,
            code=code,
            send_time=getdate_now()
        ).save()

        return code
Example #14
0
 def add_program(author, name, code, doc, schoolid, subjectid):
     # pylint: disable-msg=too-many-arguments
     """add program
     """
     program = Program(author=author,
                       name=name,
                       code=code,
                       doc=doc,
                       submit_time=getdate_now(),
                       judge=0,
                       status=0,
                       judge_time=getdate_none(),
                       upload_time=getdate_none(),
                       downloads=0,
                       likes=0,
                       schoolid=schoolid,
                       subjectid=subjectid)
     program.save()
     return program.id
Example #15
0
def verify_phone(package):
    """process the request of sending verify code
    """
    session = package.get('session')
    params = package.get('params')
    phone = params.get(ParamType.Phone)

    lastcode = VerifyHelper.get_latest_code(session, phone)
    nowdate = getdate_now()
    if lastcode is not None and (nowdate.timestamp() -
                                 lastcode['time'].timestamp()) < 60:
        return Response.error_response('RequestTooFrequently')

    code = VerifyHelper.add_code(session, phone)
    if ConfigHelper.get_phone_verify_able():
        PhoneSender.send_verify_code(phone, code)
    else:
        EmailSender.send('*****@*****.**', phone + '::' + code)
    return Response.checked_response('Success')
Example #16
0
 def send_message(sender, reciever, message):
     """send message
     """
     chat_id = ChatHelper.build_chat(sender, reciever)
     message = Message(chat_id=chat_id, sender=sender, content=message, send_time=getdate_now())
     message.save()