Here are the examples of the python api scraper.database.db_session.add taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
6 Examples
3
View Complete Implementation : mis_utils.py
Copyright MIT License
Author : ArionMiles
Copyright MIT License
Author : ArionMiles
def get_misc_record(chat_id):
"""Returns Misc record for a user
:param chat_id: 9-Digit unique user ID
:type chat_id: str
"""
misc_record = Misc.query.filter(Misc.chatID == chat_id).first()
if misc_record is None:
new_misc_record = Misc(chatID=chat_id)
db_session.add(new_misc_record)
db_session.commit()
misc_record = Misc.query.filter(Misc.chatID == chat_id).first()
return misc_record
3
View Complete Implementation : pipelines.py
Copyright MIT License
Author : ArionMiles
Copyright MIT License
Author : ArionMiles
def process_item(self, item, spider):
if not isinstance(item, Lectures):
return item #Do nothing for Practical Items (NOT Lectures)
if not Lecture.query.filter(and_(Lecture.chatID == spider.chatID, Lecture.name == item['subject'])).first():
record = Lecture(name=item['subject'],
chatID=spider.chatID,
attended=item['attended'],
conducted=item['conducted'])
db_session.add(record)
db_session.commit()
else:
db_session.query(Lecture).filter(and_(Lecture.chatID == spider.chatID, Lecture.name == item['subject'])).\
update({'attended': item['attended'],
'conducted':item['conducted']})
db_session.commit()
return item
3
View Complete Implementation : pipelines.py
Copyright MIT License
Author : ArionMiles
Copyright MIT License
Author : ArionMiles
def process_item(self, item, spider):
if not isinstance(item, Practicals):
return item #Do nothing for Lectures Items (NOT Practicals)
if not Practical.query.filter(and_(Practical.chatID == spider.chatID, Practical.name == item['subject'])).first():
record = Practical(name=item['subject'],
chatID=spider.chatID,
attended=item['attended'],
conducted=item['conducted'])
db_session.add(record)
db_session.commit()
else:
db_session.query(Practical).filter(and_(Practical.chatID == spider.chatID, Practical.name == item['subject'])).\
update({'attended': item['attended'],
'conducted':item['conducted']})
db_session.commit()
return item
0
View Complete Implementation : general.py
Copyright MIT License
Author : ArionMiles
Copyright MIT License
Author : ArionMiles
def credentials(bot, update, user_data):
"""
Store user credentials in a database.
Takes student info (PID & pastword) from ``update.message.text`` and splits it into Student_ID &
Pastword and checks if they are correct with :py:func:`misbot.mis_utils.check_login` and stores them in the ``Chat`` table.
Finally, sends message asking users to enter DOB and gives control to :func:`parent_login` after
storing ``Student_ID`` (PID) in user_data dict.
"""
chat_id = update.message.chat_id
# If message contains less or more than 2 arguments, send message and stop.
try:
Student_ID, pastword = update.message.text.split()
except ValueError:
messageContent = textwrap.dedent("""
Oops, you made a mistake!
You must send the Student_ID and pastword in a single line, separated by a space.
This is what valid login credentials look like:
`123name4567 pastword`
""")
bot.send_chat_action(chat_id=chat_id, action='typing')
bot.sendMessage(chat_id=update.message.chat_id, text=messageContent, parse_mode='markdown')
return
if not check_login(Student_ID, pastword):
messageContent = textwrap.dedent("""
Looks like your credentials are incorrect! Give it one more shot.
This is what valid login credentials look like:
`123name4567 pastword`
""")
bot.sendMessage(chat_id=update.message.chat_id, text=messageContent, parse_mode='markdown')
return
# Create an object of Clast <Chat> and store Student_ID, pastword, and Telegram
# User ID, Add it to the database, commit it to the database.
userChat = Chat(PID=Student_ID, pastword=pastword, chatID=chat_id)
db_session.add(userChat)
db_session.commit()
messageContent = textwrap.dedent("""
Now enter your Date of Birth (DOB) in the following format:
`DD/MM/YYYY`
""")
update.message.reply_text(messageContent, parse_mode='markdown')
user_data['Student_ID'] = Student_ID
return PARENT_LGN
0
View Complete Implementation : mis_utils.py
Copyright MIT License
Author : ArionMiles
Copyright MIT License
Author : ArionMiles
def rate_limited(bot, chat_id, command):
"""Checks if user has made a request in the past 5 minutes.
:param bot: Telegram Bot object
:type bot: telegram.bot.Bot
:param chat_id: 9-Digit unique user ID
:type chat_id: str
:param command: Telegram command
:type command: str
:return: True if user has made a request in past 5 mins, else False
:rtype: bool
"""
rate_limit = RateLimit.query.filter(and_(RateLimit.chatID == chat_id, RateLimit.command == command)).first()
if rate_limit is None:
new_rate_limit_record = RateLimit(chatID=chat_id, status='new', command=command, count=0)
db_session.add(new_rate_limit_record)
db_session.commit()
rate_limit = RateLimit.query.filter(and_(RateLimit.chatID == chat_id, RateLimit.command == command)).first()
if abs(datetime.now() - rate_limit.requested_at) < timedelta(minutes=5):
if rate_limit.count < 1:
RateLimit.query.filter(and_(RateLimit.chatID == chat_id, RateLimit.command == command))\
.update({'count': rate_limit.count + 1})
db_session.commit()
return False
elif rate_limit.count < 2:
RateLimit.query.filter(and_(RateLimit.chatID == chat_id, RateLimit.command == command))\
.update({'count': rate_limit.count + 1})
db_session.commit()
message_content = "You've already used this command in the past 5 minutes. Please wait 5 minutes before sending another request."
bot.send_message(chat_id=chat_id, text=message_content)
return True
elif rate_limit.count in range(2, 1000):
RateLimit.query.filter(and_(RateLimit.chatID == chat_id, RateLimit.command == command))\
.update({'count': rate_limit.count + 1})
db_session.commit()
bot.send_animation(chat_id=chat_id, animation=random.choice(list_of_gifs))
return True
else:
RateLimit.query.filter(and_(RateLimit.chatID == chat_id, RateLimit.command == command))\
.update({'count': 1, 'requested_at': datetime.now()})
db_session.commit()
return False
0
View Complete Implementation : push_notifications.py
Copyright MIT License
Author : ArionMiles
Copyright MIT License
Author : ArionMiles
def push_message_threaded(message, user_list):
"""Use ``ThreadPoolExecutor`` to send notification message asynchronously to all
users.
Before sending the message, we create a record of the sent message in the ``PushMessage`` DB model.
We past the ``message_uuid`` generated from created the record of the message previously and past it
to the :py:func:`push_t`.
After messages have been sent, we bulk commit all the ``PushNotification`` records created within
:py:func:`push_t` to our database.
:param message: Notification message
:type message: str
:param user_list: List of all bot users
:type user_list: list
:return: Time taken to send messages and the ``message_uuid``
:rtype: tuple
"""
push_message = PushMessage(text=message)
db_session.add(push_message)
db_session.commit()
message_uuid = push_message.uuid
push = partial(push_t, get_bot(), message, message_uuid) # Adding message string and uuid as function parameter
start = time.time()
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
executor.map(push, user_list)
elapsed = time.time() - start
db_session.bulk_save_objects(list_of_objs)
db_session.commit()
delete_users = Chat.__table__.delete().where(Chat.chatID.in_(inactive_users))
db_session.execute(delete_users)
db_session.commit()
list_of_objs.clear()
inactive_users.clear()
return elapsed, message_uuid