def create_archive(username, category_id, tracker_id, user_id): """ Archive summary of results in the database and store a zip archive in the data directory. """ redis = worker.get_redis() worker.start_job() db_session = worker.get_session() found_count = 0 not_found_count = 0 error_count = 0 results = (db_session.query(Result).options(subqueryload( Result.image_file)).filter(Result.tracker_id == tracker_id).all()) site_count = len(results) # Generate zip file filename = re.sub('[\W_]+', '', username) # Strip non-alphanumeric char zip_file_id = create_zip(filename, results, user_id) for result in results: if result.status == 'e': error_count += 1 elif result.status == 'f': found_count += 1 elif result.status == 'n': not_found_count += 1 archive = Archive(tracker_id=tracker_id, username=username, category_id=category_id, site_count=site_count, found_count=found_count, not_found_count=not_found_count, error_count=error_count, zip_file_id=zip_file_id, user_id=user_id) # Write to db db_session.add(archive) db_session.commit() # Publish message = { 'id': archive.id, 'name': archive.username, 'status': 'created', 'archive': archive.as_dict(), } redis.publish('archive', json.dumps(message)) worker.finish_job()
def apply_action(mails): # we are going to do actions according to this list # actions = ["Mark as read", "mark as unread", "Archive message", "Add label"] print("Enter your action: ") for i, value in enumerate(actions): print("For " + value.upper() + " Enter " + str(i)) action_choice = int(raw_input("Enter your choice number: ")) if action_choice == 0: # mark as read for x in mails: label_objects = session.query(Label).filter_by(mail_id=x.mail_id) for label in label_objects: if label.mail_label == "UNREAD": session.delete(label) session.commit() elif action_choice == 1: # mark as unread for x in mails: label_objects = session.query(Label).filter_by(mail_id=x.mail_id) applied = False for label in label_objects: if label.mail_label == "UNREAD": applied = True if not applied: # if contrast label is not available, let's add this to file new_label_object = Label(mail_label="UNREAD", mail_id=x.mail_id) session.add(new_label_object) session.commit() elif action_choice == 2: # archieved message for x in mails: newArcheiveObj = Archive(mail_id=x.mail_id, mail_time=x.mail_time, mail_from=x.mail_from, mail_to=x.mail_to, subject=x.subject, text_of_body=x.text_of_body) session.add(newArcheiveObj) session.commit() # let's delete from mail file session.delete(x) session.commit() elif action_choice == 3: # add label label = raw_input("Enter your label name: ") for x in mails: newObj = Label(mail_label=label, mail_id=x.mail_id) session.add(newObj) session.commit() print("Congratulation, It has been applied. Aregorn")