Example #1
0
    def test_ar_to_fa(self):
        self.assertEqual(digits.ar_to_fa("٠٩٨٧٦٥٤٣٢١"), "۰۹۸۷۶۵۴۳۲۱")
        self.assertEqual(digits.ar_to_fa(u"٠٩٨٧٦٥٤٣٢١"), u"۰۹۸۷۶۵۴۳۲۱")

        orig = "0987٦٥٤٣۲۱"
        converted = digits.en_to_fa(orig)
        converted = digits.ar_to_fa(converted)

        self.assertEqual(converted, "۰۹۸۷۶۵۴۳۲۱")
Example #2
0
def normalize(data):
    """this methode will check for arabic charecter and will convert them
    to persian and than change persian numerics to english one"""
    string = str(data)
    not_arabic = digits.ar_to_fa(string)
    res = digits.fa_to_en(not_arabic)
    return res
Example #3
0
def lead_add(request):
    if request.method == "POST" and request.user.is_authenticated \
            and request.user.is_staff:
        name_and_family = request.POST['name_and_family']
        gender = request.POST['gender']
        phone_fa = digits.ar_to_fa(request.POST['phone_number'])
        phone_en = digits.fa_to_en(phone_fa)
        register_status = request.POST['register_status']
        operator = request.user
        origin = Origin.objects.filter(id=int(request.POST['origin'])).first()
        #fields should be validate
        if len(name_and_family) > 2 and len(phone_en) > 5 and len(phone_en) < 16 and\
                phone_en.isdigit() and len(Lead.objects.filter(phone_number=phone_en)) == 0:
            lead = Lead(origin = origin, name_and_family = name_and_family.encode("utf-8"), gender = gender,\
                phone_number = phone_en, register_status = register_status,\
                led_time = datetime.now(),led_time_jalali = JalaliDateTime.now().strftime("%Y-%m-%d %H:%M:%S"),\
                led_time_jalali_str = JalaliDateTime.now().strftime("%c"))
            #if lead is registered save operator
            if register_status == "K":
                lead.registered_by = request.user
            else:
                lead.registered_by = None
            lead.save()
            lead.operator.add(operator)
            messages.success(request, "You'r new lead has been save")
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        #if same phone number added by 1 or more operators
        elif len(Lead.objects.filter(phone_number=phone_en)) == 1:
            lead = Lead.objects.filter(phone_number=phone_en).first()
            if request.user in lead.operator.all():
                messages.warning(request, "Phone number is repetitive")
                return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
            else:
                if lead.register_status != "K":
                    lead.operator.add(request.user)
                    lead.save()
                    messages.info(request, "lead is repetitive but you added to this lead operators")
                    messages.success(request, "You'r new lead has been save")
                    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
                else:
                    messages.info(request, "lead is repetitive and registered by another operator")
                    messages.warning(request, "You'r new lead did not changed!")
                    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        elif len(name_and_family) <= 3:
            messages.warning(request, "Check name and family fileld")
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        elif len(phone_en) <= 6 and len(phone_en) >= 16 and phone_en.isdigit() is False:
            messages.warning(request, "Phone number is in wrong format")
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        else:
            messages.warning(request, "something went wrong")
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    else:
        messages.error(request, "You'r not authorized")
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Example #4
0
def digit(values, arg):
    values = str(values)
    if arg == "" or arg == "en_to_fa":
        return digits.en_to_fa(values)
    elif arg == "ar_to_fa":
        return digits.ar_to_fa(values)
    elif arg == "fa_to_en":
        return digits.fa_to_en(values)
    elif arg == "fa_to_ar":
        return digits.fa_to_ar(values)
    else:
        return values
    def test_ar_to_fa(self):
        self.assertEqual(characters.ar_to_fa("السلام عليكم"), "السلام علیکم")
        self.assertEqual(characters.ar_to_fa("HI ي"), "HI ی")
        self.assertEqual(characters.ar_to_fa("دِ بِ زِ ذِ شِ سِ ى ي ك"),
                         "د ب ز ذ ش س ی ی ک")
        self.assertEqual(characters.ar_to_fa("دِ بِ زِ ذِ شِ سِ ى ي ك"),
                         "د ب ز ذ ش س ی ی ک")
        self.assertEqual(
            characters.ar_to_fa(
                "ظ ط ذ د ز ر و ، . ش س ي ب ل ا ت ن م ك ض ص ث ق ف غ ع ه خ ح ؟"),
            "ظ ط ذ د ز ر و ، . ش س ی ب ل ا ت ن م ک ض ص ث ق ف غ ع ه خ ح ؟",
        )

        with pytest.raises(TypeError):
            characters.ar_to_fa(12345)

        orig = "السلام عليكم ٠١٢٣٤٥٦٧٨٩"
        converted = characters.ar_to_fa(orig)
        converted = digits.ar_to_fa(converted)
        self.assertEqual(converted, "السلام علیکم ۰۱۲۳۴۵۶۷۸۹")
Example #6
0
def handle_responses(update: Update, context: CallbackContext) -> None:
    message = update.message
    message_text = digits.ar_to_fa(digits.fa_to_en(message.text))
    user_data = context.user_data
    music_path = user_data['music_path']
    art_path = user_data['art_path']
    music_tags = user_data['tag_editor']
    current_tag = music_tags.get('current_tag')
    lang = user_data['language']

    logging.info(
        "%s:%s:%s",
        update.effective_user.id,
        update.effective_user.username,
        update.message.text
    )

    current_active_module = user_data['current_active_module']

    tag_editor_keyboard = generate_tag_editor_keyboard(lang)

    module_selector_keyboard = generate_module_selector_keyboard(lang)

    back_button_keyboard = generate_back_button_keyboard(lang)
    start_over_button_keyboard = generate_start_over_keyboard(lang)

    if current_active_module == 'tag_editor':
        if not current_tag:
            reply_message = translate_key_to(lp.ASK_WHICH_TAG, lang)
            message.reply_text(reply_message, reply_markup=tag_editor_keyboard)
        elif current_tag == 'album_art':
            reply_message = translate_key_to(lp.ASK_FOR_ALBUM_ART, lang)
            message.reply_text(reply_message, reply_markup=tag_editor_keyboard)
        else:
            save_text_into_tag(
                value=message_text,
                current_tag=current_tag,
                context=context,
                is_number=current_tag in ('year', 'disknumber', 'tracknumber')
            )
            reply_message = f"{translate_key_to(lp.DONE, lang)} " \
                            f"{translate_key_to(lp.CLICK_PREVIEW_MESSAGE, lang)} " \
                            f"{translate_key_to(lp.OR, lang).upper()}" \
                            f" {translate_key_to(lp.CLICK_DONE_MESSAGE, lang).lower()}"
            message.reply_text(reply_message, reply_markup=tag_editor_keyboard)
    elif current_active_module == 'music_cutter':
        try:
            beginning_sec, ending_sec = parse_cutting_range(message_text)
        except (ValueError, BaseException):
            reply_message = translate_key_to(lp.ERR_MALFORMED_RANGE, lang).format(
                translate_key_to(lp.MUSIC_CUTTER_HELP, lang),
            )
            message.reply_text(reply_message, reply_markup=back_button_keyboard)
            return
        music_path_cut = f"{music_path}_cut.mp3"
        music_duration = user_data['music_duration']

        if beginning_sec > music_duration or ending_sec > music_duration:
            reply_message = translate_key_to(lp.ERR_OUT_OF_RANGE, lang).format(
                convert_seconds_to_human_readable_form(music_duration))
            message.reply_text(reply_message)
            message.reply_text(
                translate_key_to(lp.MUSIC_CUTTER_HELP, lang),
                reply_markup=back_button_keyboard
            )
        elif beginning_sec >= ending_sec:
            reply_message = translate_key_to(lp.ERR_BEGINNING_POINT_IS_GREATER, lang)
            message.reply_text(reply_message)
            message.reply_text(
                translate_key_to(lp.MUSIC_CUTTER_HELP, lang),
                reply_markup=back_button_keyboard
            )
        else:
            diff_sec = ending_sec - beginning_sec

            os.system(
                f"ffmpeg -y -ss {beginning_sec} -t {diff_sec} -i {music_path} -acodec copy \
                {music_path_cut}"
            )

            try:
                save_tags_to_file(
                    file=music_path_cut,
                    tags=music_tags,
                    new_art_path=art_path if art_path else ''
                )
            except (OSError, BaseException):
                update.message.reply_text(translate_key_to(lp.ERR_ON_UPDATING_TAGS, lang))
                logger.error(
                    "Error on updating tags for file %s's file.",
                    music_path_cut,
                    exc_info=True
                )

            try:
                with open(music_path_cut, 'rb') as music_file:
                    # FIXME: After sending the file, the album art can't be read back
                    context.bot.send_audio(
                        audio=music_file,
                        chat_id=update.message.chat_id,
                        duration=diff_sec,
                        caption=f"*From*: {convert_seconds_to_human_readable_form(beginning_sec)}\n"
                                f"*To*: {convert_seconds_to_human_readable_form(ending_sec)}\n\n"
                                f"🆔 {BOT_USERNAME}",
                        reply_markup=start_over_button_keyboard,
                        reply_to_message_id=user_data['music_message_id']
                    )
            except (TelegramError, BaseException) as error:
                message.reply_text(
                    translate_key_to(lp.ERR_ON_UPLOADING, lang),
                    reply_markup=start_over_button_keyboard
                )
                logger.exception("Telegram error: %s", error)

            delete_file(music_path_cut)

            reset_user_data_context(context)
    else:
        if music_path:
            if user_data['current_active_module']:
                message.reply_text(
                    translate_key_to(lp.ASK_WHICH_MODULE, lang),
                    reply_markup=module_selector_keyboard
                )
        elif not music_path:
            message.reply_text(translate_key_to(lp.START_OVER_MESSAGE, lang))
        else:
            # Not implemented
            reply_message = translate_key_to(lp.ERR_NOT_IMPLEMENTED, lang)
            message.reply_text(reply_message)
Example #7
0
    def test_long_ar_to_fa(self):
        orig = "السلام عليكم ٠١٢٣٤٥٦٧٨٩"
        converted = characters.ar_to_fa(orig)
        converted = digits.ar_to_fa(converted)

        self.assertEqual(converted, "السلام علیکم ۰۱۲۳۴۵۶۷۸۹")
Example #8
0
def lead_del_and_edit(request):
    if request.method == "POST" and request.user.is_authenticated \
            and request.user.is_staff:
        get_object_or_404(Lead, id=int(request.POST["id"]))
        lead = Lead.objects.filter(id=request.POST["id"]).first()
        if request.POST["submit"] == "delete" :
            if request.user.is_superuser:
                lead.delete()
                messages.success(request, "That lead is now gone!!!") 
                return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
            else:
                messages.warning(request, "You're not superuser") 
                return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        elif request.POST["submit"] == "edit":
            name_and_family = request.POST['name_and_family']
            gender = request.POST['gender']
            phone_fa = digits.ar_to_fa(request.POST['phone_number'])
            phone_en = digits.fa_to_en(phone_fa)
            register_status = request.POST['register_status']
            origin = Origin.objects.filter(id=int(request.POST['origin'])).first()
            lead = Lead.objects.filter(id=request.POST["id"]).first()
            #later we need for regester leads with 2 or more operators
            if name_and_family == lead.name_and_family and gender == lead.gender\
                and phone_en == lead.phone_number and register_status != lead.register_status\
                    and origin == lead.origin:
                status_changed = True
            else:
                status_changed = False
            #fields should be valid
            if len(name_and_family) > 2 and len(phone_en) > 5 and len(phone_en) < 16 and\
                    phone_en.isdigit():
                #edited phone should not be other lead phone number
                if (len(Lead.objects.filter(phone_number=phone_en)) == 1 \
                    and lead.id == Lead.objects.filter(phone_number=phone_en).first().id)\
                        or len(Lead.objects.filter(phone_number=phone_en)) == 0:
                    if request.user.is_superuser:
                        lead.orgin = origin
                        lead.name_and_family = name_and_family.encode("utf-8")
                        lead.gender = gender
                        lead.phone_number = phone_en
                        lead.register_status = register_status
                        #if lead is registered save operator
                        if register_status == "K":
                            lead.registered_by = request.user
                        else:
                            lead.registered_by = None
                        lead.save()
                        messages.success(request, "You'r lead has been changed")
                        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
                    #only single owner operator can change this lead or superuser
                    elif request.user in lead.operator.all() and len(lead.operator.all()) < 2:
                        lead.orgin = origin
                        lead.name_and_family = name_and_family.encode("utf-8")
                        lead.gender = gender
                        lead.phone_number = phone_en
                        lead.register_status = register_status
                        #if lead is registered save operator
                        if register_status == "K":
                            lead.registered_by = request.user
                        else:
                            lead.registered_by = None
                        lead.save()
                        messages.success(request, "You'r lead has been changed")
                        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
                    #want make operator register    
                    elif status_changed and request.user in lead.operator.all() and len(lead.operator.all()) > 1:
                        if (lead.register_status == "K" and lead.registered_by == request.user) or  lead.register_status != "K":
                            lead.register_status = register_status
                            #if lead is registered save operator
                            if register_status == "K":
                                lead.registered_by = request.user
                            else:
                                lead.registered_by = None
                            lead.save()
                            messages.success(request, "You'r lead has been registered")
                            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
                        else:
                            messages.warning(request, "You'r can not change lead status")
                            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
                    else:
                        messages.warning(request, "This is not you'r lead") 
                        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))                  
                else:
                    messages.warning(request, "Phone number is repititive")
                    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
            elif len(phone_en) <= 6 and len(phone_en) >= 16 and phone_en.isdigit() is False:
                messages.warning(request, "Phone number is in wrong format")
                return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
            elif len(name_and_family) <= 3:
                messages.warning(request, "Check name and family fileld")
                return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
            else:
                messages.warning(request, "something went wrong")
                return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
        else:
            messages.warning(request, "You'r request is not valid")
            return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    else:
        messages.error(request, "You'r not authorized") 
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))