Saturday, December 29, 2018

android kotlin ArrayAdapter withen JSONObject itemdata

private inner class StableArrayAdapter(context: Context, viewResourceId: Int)
    : ArrayAdapter<JSONObject>(context, viewResourceId) {

    fun addItems(items: JSONArray) {
        for (i in 0 until items.length()) {
            val item = items.getJSONObject(i)
            this.add(item)
        }
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var retView: View? = null        if (convertView == null) {
            retView = LayoutInflater.from(getContext()).inflate(R.layout.main_row_item, parent, false)
        } else {
            retView = convertView
        }
        val item = getItem(position) as JSONObject
        retView!!.tag = item

        if (item.optBoolean("is_denied"))
            retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_odo_maroon)
        else {
            if (item.optBoolean("is_received"))
                retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_disable_blue)
            else {
                if (item.optBoolean("is_approved"))
                    retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_odo_orange)
                else {
                    if (item.optBoolean("is_sent"))
                        retView!!.icon_state.setBackgroundResource(R.drawable.bg_oval_active_blue)
                    else                        retView!!.icon_state.visibility = View.GONE                }
            }
        }

        val dateStr = item.getString("created_at")
        retView.tv_datetime.setText(AppHelper.formatJsonDate(dateStr))

        val toc = item.getJSONObject("toc")
        val tob = item.getJSONObject("tob")
        retView.tv_name.setText(toc.getString("name") + " / " + tob.getString("name"))
        retView.tv_descr.setText("№ " + item.getString("order_number") + " - " + item.getString("order_name"))

        retView.setOnClickListener { view ->            itemClick(view)
        }        return retView!!
    }

    fun itemClick(view: View) {
        val item = view.tag as JSONObject

        val args = Bundle()
        args.putString(ArgumentPassKeys.ARG_JSON_OBJECT, item.toString())

        val intent = Intent(_context, EventsActivity::class.java)
        intent.putExtras(args)
        _activity!!.startActivityForResult(intent, ActivityPassCodes.ITEM_EDIT)
    }
}

Tuesday, December 25, 2018

flask response svg or png qrcode generator using pyqrcode module

import pyqrcode, png
text = pyqrcode.create('')
text.svg('./app/static/uploads/uca-url.svg', scale=8)
text.png('./app/static/uploads/uca-url.png', scale=8)
# print(text.text())
# print(text.terminal(module_color='red', background='yellow'))
# print(url.terminal(quiet_zone=1))


@users_api.route('/')
def base_index():
import io
import pyqrcode, png
qr = pyqrcode.create('5035265994')
qr.png('test.png', scale=8)
image_binary = io.open("test.png", "rb")
print('***********type is ', type(image_binary))
response = make_response(image_binary.read())
response.headers.set('Content-Type', 'image/png')
# response.headers.set('Content-Disposition', 'attachment', filename='test.png')
return response
# return json_error(), 404

Saturday, December 22, 2018

python switcher case while else type() format (%s %03d {})

b = 'True'
print(type(b) == str)
print(type(b) == bool)

switcher = {
True: print('it is true'),
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday'
}
print(switcher.get(True, 'else'))

a = 3
while (a > 0):
print(a)
a -= 1
else:
print("Reached 0")

# Here age is a string object
age = "18"
print(age)
# Converting string to integer
int_age = int(age)
print(int_age)
age = False
int_age = int(age)
print(int_age)

print('%(language)s has %(number)05d quote types.' % {
"language": "Python",
"number": 2
})
print('form({})'.format(b))