def apply(self): try: if self.subject is None: self.delete_instance() return False except Bot.DoesNotExist: self.delete_instance() return False if self.action == 'category': from models import Category try: cat = Category.get(Category.id == self.value) self.subject.category = cat except Category.DoesNotExist: raise AttributeError("Category to change to does not exist.") elif self.action == 'name': self.subject.name = self.value elif self.action == 'username': self.subject.username = self.value elif self.action == 'description': self.subject.description = self.value elif self.action == 'extra': self.subject.extra = self.value elif self.action == 'country': if self._value == 'None' or self._value is None: self.subject.country = None else: from models import Country try: con = Country.get(id=self._value) self.subject.country = con except Country.DoesNotExist: raise AttributeError( "Country to change to does not exist.") elif self.action == 'inlinequeries': self.subject.inlinequeries = bool(self.value) elif self.action == 'official': self.subject.official = bool(self.value) elif self.action == 'offline': self.subject.offline = bool(self.value) elif self.action == 'spam': self.subject.spam = bool(self.value) elif self.action == 'add_keyword': kw_obj = Keyword(name=self.value, entity=self.subject) kw_obj.save() elif self.action == 'remove_keyword': try: kw_obj = Keyword.get(name=self.value, entity=self.subject) kw_obj.delete_instance() except Keyword.DoesNotExist: raise AttributeError( "Keyword to disable does not exist anymore.") self.subject.save() self.executed = True self.save() return True
def _md_plaintext(self): uname = util.escape_markdown(self.subject.username) value = self.value text = '' if self.action == 'category': from models import Category try: cat = Category.get(id=self.value) text += "move {} ➜ {}".format(uname, cat.name) except Category.DoesNotExist: raise AttributeError("Category to change to does not exist.") elif self.action == 'name': text += "set name {} ➜ {}".format(uname, str(value)) elif self.action == 'username': text += "set username {} ➜ {}".format(uname, str(value)) elif self.action == 'description': text += "change description text of {}".format(uname) elif self.action == 'extra': text += "change extra text {}".format(uname) elif self.action == 'country': text += "change country {} ➜ ".format(uname) if self._value == 'None' or self._value is None: text += "None" else: from models import Country try: con = Country.get(id=self._value) text += str(con) except Country.DoesNotExist: raise AttributeError( "Country to change to does not exist.") elif self.action == 'inlinequeries': text += "toggle inlinequeries {} ➜ {}".format(uname, str(value)) elif self.action == 'official': text += "toggle official {} ➜ {}".format(uname, str(value)) elif self.action == 'offline': text += "set {} {}".format('💤' if bool(value) else 'online', uname) elif self.action == 'spam': text += "mark {} as spammy".format(uname) elif self.action == 'add_keyword': text += "add keyword #{} to {}".format(str(value), uname) elif self.action == 'remove_keyword': text += "remove keyword #{} from {}".format(str(value), uname) return text
def value(self): # cast types from models import Category from models import Country if self._value == 'None': return None if self.action in self.BOOLEAN_ACTIONS: return bool(self._value) elif self.action == 'category': return Category.get(id=self._value) elif self.action == 'country': if self._value is None: return None return Country.get(id=self._value) else: return util.escape_markdown(str( self._value)) if self._value else None
def callback_router(bot, update, chat_data, user_data, job_queue): obj = json.loads(str(update.callback_query.data)) user = User.from_update(update) try: if "a" in obj: action = obj["a"] # BOTLISTCHAT if action == CallbackActions.DELETE_CONVERSATION: botlistchat.delete_conversation(bot, update, chat_data) # HELP elif action == CallbackActions.HELP: help.help(bot, update) elif action == CallbackActions.CONTRIBUTING: help.contributing(bot, update) elif action == CallbackActions.EXAMPLES: help.examples(bot, update) # BASIC QUERYING elif action == CallbackActions.SELECT_CATEGORY: select_category(bot, update, chat_data) elif action == CallbackActions.SELECT_BOT_FROM_CATEGORY: category = Category.get(id=obj["id"]) send_category(bot, update, chat_data, category) elif action == CallbackActions.SEND_BOT_DETAILS: item = Bot.get(id=obj["id"]) send_bot_details(bot, update, chat_data, item) # FAVORITES elif action == CallbackActions.TOGGLE_FAVORITES_LAYOUT: value = obj["v"] favorites.toggle_favorites_layout(bot, update, value) elif action == CallbackActions.ADD_FAVORITE: favorites.add_favorite_handler(bot, update) elif action == CallbackActions.REMOVE_FAVORITE_MENU: favorites.remove_favorite_menu(bot, update) elif action == CallbackActions.REMOVE_FAVORITE: to_remove = Favorite.get(id=obj["id"]) bot_details = to_remove.bot to_remove.delete_instance() if obj.get("details"): send_bot_details(bot, update, chat_data, bot_details) else: favorites.remove_favorite_menu(bot, update) elif action == CallbackActions.SEND_FAVORITES_LIST: favorites.send_favorites_list(bot, update) elif action == CallbackActions.ADD_ANYWAY: favorites.add_custom(bot, update, obj["u"]) elif action == CallbackActions.ADD_TO_FAVORITES: details = obj.get("details") discreet = obj.get("discreet", False) or details item = Bot.get(id=obj["id"]) favorites.add_favorite(bot, update, item, callback_alert=discreet) if details: send_bot_details(bot, update, chat_data, item) # ACCEPT/REJECT BOT SUBMISSIONS elif action == CallbackActions.APPROVE_REJECT_BOTS: custom_approve_list = [Bot.get(id=obj["id"])] admin.approve_bots(bot, update, override_list=custom_approve_list) elif action == CallbackActions.ACCEPT_BOT: to_accept = Bot.get(id=obj["id"]) admin.edit_bot_category(bot, update, to_accept, CallbackActions.BOT_ACCEPTED) # Run in x minutes, giving the moderator enough time to edit bot details job_queue.run_once( lambda b, job: botlistchat. notify_group_submission_accepted(b, job, to_accept), settings.BOT_ACCEPTED_IDLE_TIME * 60, ) elif action == CallbackActions.RECOMMEND_MODERATOR: bot_in_question = Bot.get(id=obj["id"]) admin.recommend_moderator(bot, update, bot_in_question, obj["page"]) elif action == CallbackActions.SELECT_MODERATOR: bot_in_question = Bot.get(id=obj["bot_id"]) moderator = User.get(id=obj["uid"]) admin.share_with_moderator(bot, update, bot_in_question, moderator) admin.approve_bots(bot, update, obj["page"]) elif action == CallbackActions.REJECT_BOT: to_reject = Bot.get(id=obj["id"]) notification = obj.get("ntfc", True) admin.reject_bot_submission( bot, update, None, to_reject, verbose=False, notify_submittant=notification, ) admin.approve_bots(bot, update, obj["page"]) elif action == CallbackActions.BOT_ACCEPTED: to_accept = Bot.get(id=obj["bid"]) category = Category.get(id=obj["cid"]) admin.accept_bot_submission(bot, update, to_accept, category) elif action == CallbackActions.COUNT_THANK_YOU: new_count = obj.get("count", 1) basic.count_thank_you(bot, update, new_count) # ADD BOT # elif action == CallbackActions.ADD_BOT_SELECT_CAT: # category = Category.get(id=obj['id']) # admin.add_bot(bot, update, chat_data, category) # EDIT BOT elif action == CallbackActions.EDIT_BOT: to_edit = Bot.get(id=obj["id"]) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_SELECT_CAT: to_edit = Bot.get(id=obj["id"]) admin.edit_bot_category(bot, update, to_edit) elif action == CallbackActions.EDIT_BOT_CAT_SELECTED: to_edit = Bot.get(id=obj["bid"]) cat = Category.get(id=obj["cid"]) botproperties.change_category(bot, update, to_edit, cat) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_COUNTRY: to_edit = Bot.get(id=obj["id"]) botproperties.set_country_menu(bot, update, to_edit) elif action == CallbackActions.SET_COUNTRY: to_edit = Bot.get(id=obj["bid"]) if obj["cid"] == "None": country = None else: country = Country.get(id=obj["cid"]) botproperties.set_country(bot, update, to_edit, country) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_DESCRIPTION: to_edit = Bot.get(id=obj["id"]) botproperties.set_text_property(bot, update, chat_data, "description", to_edit) elif action == CallbackActions.EDIT_BOT_EXTRA: to_edit = Bot.get(id=obj["id"]) # SAME IS DONE HERE, but manually botproperties.set_text_property(bot, update, chat_data, "extra", to_edit) elif action == CallbackActions.EDIT_BOT_NAME: to_edit = Bot.get(id=obj["id"]) botproperties.set_text_property(bot, update, chat_data, "name", to_edit) elif action == CallbackActions.EDIT_BOT_USERNAME: to_edit = Bot.get(id=obj["id"]) botproperties.set_text_property(bot, update, chat_data, "username", to_edit) # elif action == CallbackActions.EDIT_BOT_KEYWORDS: # to_edit = Bot.get(id=obj['id']) # botproperties.set_keywords_init(bot, update, chat_data, to_edit) elif action == CallbackActions.APPLY_ALL_CHANGES: to_edit = Bot.get(id=obj["id"]) admin.apply_all_changes(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_INLINEQUERIES: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "inlinequeries", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_OFFICIAL: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "official", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_OFFLINE: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "offline", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.EDIT_BOT_SPAM: to_edit = Bot.get(id=obj["id"]) value = bool(obj["value"]) botproperties.toggle_value(bot, update, "spam", to_edit, value) admin.edit_bot(bot, update, chat_data, to_edit) elif action == CallbackActions.CONFIRM_DELETE_BOT: to_delete = Bot.get(id=obj["id"]) botproperties.delete_bot_confirm(bot, update, to_delete) elif action == CallbackActions.DELETE_BOT: to_edit = Bot.get(id=obj["id"]) botproperties.delete_bot(bot, update, to_edit) # send_category(bot, update, chat_data, to_edit.category) elif action == CallbackActions.ACCEPT_SUGGESTION: suggestion = Suggestion.get(id=obj["id"]) components.botproperties.accept_suggestion( bot, update, suggestion) admin.approve_suggestions(bot, update, page=obj["page"]) elif action == CallbackActions.REJECT_SUGGESTION: suggestion = Suggestion.get(id=obj["id"]) suggestion.delete_instance() admin.approve_suggestions(bot, update, page=obj["page"]) elif action == CallbackActions.CHANGE_SUGGESTION: suggestion = Suggestion.get(id=obj["id"]) botproperties.change_suggestion(bot, update, suggestion, page_handover=obj["page"]) elif action == CallbackActions.SWITCH_SUGGESTIONS_PAGE: page = obj["page"] admin.approve_suggestions(bot, update, page) elif action == CallbackActions.SWITCH_APPROVALS_PAGE: admin.approve_bots(bot, update, page=obj["page"]) elif action == CallbackActions.SET_NOTIFICATIONS: set_notifications(bot, update, obj["value"]) elif action == CallbackActions.NEW_BOTS_SELECTED: show_new_bots(bot, update, chat_data, back_button=True) elif action == CallbackActions.ABORT_SETTING_KEYWORDS: to_edit = Bot.get(id=obj["id"]) admin.edit_bot(bot, update, chat_data, to_edit) # SENDING BOTLIST elif action == CallbackActions.SEND_BOTLIST: silent = obj.get("silent", False) re_send = obj.get("re", False) botlist.send_botlist(bot, update, resend=re_send, silent=silent) elif action == CallbackActions.RESEND_BOTLIST: botlist.send_botlist(bot, update, resend=True) # BROADCASTING elif action == "send_broadcast": broadcasts.send_broadcast(bot, update, user_data) elif action == "pin_message": broadcasts.pin_message(bot, update, obj["mid"]) elif action == "add_thank_you": basic.add_thank_you_button(bot, update, obj["cid"], obj["mid"]) # EXPLORING elif action == CallbackActions.EXPLORE_NEXT: explore.explore(bot, update, chat_data) except Exception as e: traceback.print_exc() # get the callback action in plaintext actions = dict(CallbackActions.__dict__) a = next(k for k, v in actions.items() if v == obj.get("a")) util.send_md_message( bot, settings.DEVELOPER_ID, "Exception in callback query for {}:\n{}\n\nWith CallbackAction {}\n\nWith data:\n{}" .format( user.markdown_short, util.escape_markdown(e), util.escape_markdown(a), util.escape_markdown(str(obj)), ), ) finally: bot.answerCallbackQuery(update.callback_query.id) return ConversationHandler.END
def translate_country_en(country_name, to): if to == "Ukrainian": country = Country.get(Country.English == country_name) return country.Ukrainian elif to == "Russian": country = Country.get(Country.English == country_name) return country.Russian elif to == "Belorussian": country = Country.get(Country.English == country_name) return country.Belorussian elif to == "English": country = Country.get(Country.English == country_name) return country.English elif to == "Spanish": country = Country.get(Country.English == country_name) return country.Spanish elif to == "German": country = Country.get(Country.English == country_name) return country.German elif to == "French": country = Country.get(Country.English == country_name) return country.French elif to == "Italian": country = Country.get(Country.English == country_name) return country.Italian elif to == "Japanese": country = Country.get(Country.English == country_name) return country.Japanese elif to == "Latvian": country = Country.get(Country.English == country_name) return country.Latvian elif to == "Czech": country = Country.get(Country.English == country_name) return country.Czech else: return False
def translate_country(country_name, froom): if country_name == "world": return country_name if froom == "Ukrainian": country_name = changei(country_name) country = Country.get(Country.Ukrainian == country_name) elif froom == "Russian": country = Country.get(Country.Russian == country_name) elif froom == "Belorussian": country = Country.get(Country.Belorussian == country_name) elif froom == "English": country = Country.get(Country.English == country_name) elif froom == "Spanish": country = Country.get(Country.Spanish == country_name) elif froom == "German": country = Country.get(Country.German == country_name) elif froom == "French": country = Country.get(Country.French == country_name) elif froom == "Italian": country = Country.get(Country.Italian == country_name) elif froom == "Japanese": country = Country.get(Country.Japanese == country_name) elif froom == "Latvian": country = Country.get(Country.Latvian == country_name) elif froom == "Czech": country = Country.get(Country.Czech == country_name) try: a = country.English return a except: return False
def payment_commercegate(request): integrator = Integrator.get('commerce_gate') redirect_url_failed = IntegratorSetting.get_var(integrator, 'redirect_url_failed') redirect_url_success = IntegratorSetting.get_var(integrator, 'redirect_url_success') endpoint_token = IntegratorSetting.get_var(integrator, 'endpoint_token') endpoint_cancel = IntegratorSetting.get_var(integrator, 'endpoint_cancel') website_id = IntegratorSetting.get_var(integrator, 'website_id') customer_id = IntegratorSetting.get_var(integrator, 'customer_id') password = IntegratorSetting.get_var(integrator, 'password') # Cargo el JSON try: data = json.loads(request.body) except Exception: message = 'error decoding json' body = { 'status': 'error', 'message': message } return HttpResponse(json.dumps(body), content_type='application/json', status=http_BAD_REQUEST) # Verifico las key mandatorias keys = [ 'user_id', 'email', 'recurrence' ] json_loader = __validate_json(data, keys) if json_loader['status'] == 'error': json_loader['message'] = 'check api mandatory parameters' return HttpResponse(json.dumps(json_loader), content_type='application/json', status=http_BAD_REQUEST) # Verifico si el usuario existe y sino lo creo try: user = User.objects.get(user_id=data['user_id']) user.email = data['email'] user.save() except ObjectDoesNotExist: user = User.create(data['user_id'], data['email'], integrator.country) # Si tiene algun UserPayment habilitado devuelvo un error if UserPayment.objects.filter(user=user, enabled=True).exists(): message = 'enabled user payment already exists' body = { 'status': 'error', 'message': message } return HttpResponse(json.dumps(body), content_type='application/json', status=http_BAD_REQUEST) package = Package.get(data['recurrence'], integrator) if package is None: message = 'package not found with that duration' body = { 'status': 'error', 'message': message } return HttpResponse(json.dumps(body), content_type='application/json', status=http_BAD_REQUEST) country = Country.get(integrator.country) up = UserPayment.create(user, package.duration, package.amount, country.currency) payment_id = 'PH_%s_%d' % (user.user_id, int(time())) ph = PaymentHistory.create(up, payment_id, integrator) params = { 'cid': customer_id, 'wid': website_id, 'packid': package.package_id, 'username': data['user_id'], 'email': data['email'] } url = '%s?%s' % (endpoint_token, urlencode(params)) try: resp, content = Http().request(url, 'POST') except Exception: message = 'communication error with commercegate' body = { 'status': 'error', 'message': message } return HttpResponse(json.dumps(body), content_type='application/json', status=http_BAD_REQUEST) iframe_params = { 'cid': customer_id, 'wid': website_id, 'token': content } if redirect_url_success: iframe_params['successUrl'] = redirect_url_success if redirect_url_failed: iframe_params['failedUrl'] = redirect_url_failed iframe_url = '%s?%s' % (endpoint, urlencode(iframe_params)) body = { 'status': 'success', 'value': { 'url': iframe_url } } return HttpResponse(json.dumps(body), content_type='application/json', status=http_POST_OK)