Reading/Writing EDN/Map into a cookie - cookies

I'm writing a simple SPA game in ClojureScript and I want to save the game state, which is a map, to a cookie and read it back.
I'm successfully saving the cookie, though it turns my map to a string.
When I read it back, I get Error: No protocol method IAssociative.-assoc defined for type string:(then it outputs the map)
What's the simplest and best way to use maps with cookies?
I have tried https://github.com/reagent-project/reagent-utils and https://github.com/Quantisan/cljs-cookies, but I get similar issues.

I think you're looking for "cljs.core/read-string cljs.core/pr-str" functions. You need to serialize your Clojure objects to string and read it back to store it in a cookie. Functions above will be able to serialize all standard Clojure(Script) data structures for you. And then if you have something custom, this function can come to rescue: https://cljs.github.io/api/cljs.reader/register-tag-parserBANG.

Related

Why use pagination tokens?

I am implementing pagination on a webservice. My first thought was to use query params page and size, like Spring Data.
However, we are basing some of our design on the google webservice apis. I notice that they use pagination tokens, with each page result containing a nextPageToken. What are the advantages to using this approach? Changing data? What kind of info would be encoded in such a token?
When paginating with an offset, inserts and deletes into the data between your requests will cause rows to be skipped or included twice. If you are able to keep track in the token of what you returned previously (via a key or whatever), you can guarantee you don't return the same result twice, even when there are inserts/deletes between requests.
I'm a little uncertain of how you would encode a token, but for a single tables at least it seems that you could use the an encoded version of the primary key as a limit. "I just returned everything before key=200. Next time I'll only return things after 200." I guess this assumes a new item inserted between requests 1 and 2 will be given a key greater than existing keys.
https://use-the-index-luke.com/no-offset
One reason opaque strings are used for pagination tokens is so that you can change how pagination is implemented without breaking your clients. A query param like a page(I assume you mean page number) is transparent to your client and allows them to make assumptions about it.

Clojure/Ring how to get post parameters in submit order

I have a very complex form. I need to get the post parameters from that form in the order they have been submitted. The application is created in ring/compojure.
All the parameters that I can get from ring request are preprocessed (grouped, sorted..)
How do I get the raw parameter list (preferably parsed to key/value vector or some other list)?
Could you provide more information about your project. What HTTP server are you using (http-kit, clj-http, aleph), and what middleware do you have applied in your project?
All parameter based things aren't actually part of the ring spec, but are handled by middleware (see https://github.com/ring-clojure/ring/wiki/Parameters), so it greatly depends on what bits you're currently pulling in.
I'm not aware of any ring middleware that currently exist that do you want, they all seem to parse the parameter list and put it into a hashmap, and if multiple parameters exist with the same key name, they make the value in the hashmap a vector of the items.
That all being said, I have to ask. Why do you need them in a particular order?
Your best option is to create your own middleware. Use the wrap-param middleware as a guide. You just need to do your custom stuff at https://github.com/mmcgrana/ring/blob/master/ring-core/src/ring/middleware/params.clj#L29
That said, I also be wary of expecting the params in a particular order as it will make brittle the client-server communication.

Templavoila loose it's mapping after conversion of database to UTF-8

I'm using TemplaVoila 1.5.5 and TYPO3 4.5.5. I tried to switch a TYPO3 database from latin1_swedish_ci (ISO-8859-1) to utf8_general_ci. Therefore I wrote a PHP script which converted everything to binary and than everything to utf8_general_ci. Everything seemed to work except TemplaVoila (all other settings in Typo3 were already prepared for UTF-8 but not the database). I got the following message when opening the TYP3 page:
Couldn't find a Data Structure set for table/row "pages:x". Please
select a Data Structure and Template Object first.
If I looked in a template mapping I got the next message that there is no mapping available. In the table tx_templavoila_tmplobj in the column templatemapping the mapping is stored as BLOB. When converting to UTF-8 everything is gone. Because its binary I can't access it and convert it in an easy way.
How can I keep the mapping? I don't want to map everything new. What can I do?
Here there are two proposed solutions but I want to know if there are better ones. In the solution from Michael I also have to map everything again?
What is the fastet way to restore the mapping?
I can't say if you'll be able to recover the data now that it's been converted, but if you're willing to run your conversion script I have had some success with the following approach:
Unserialize the data in the templatemapping field of the tx_templavoila_tmplobj table.
Convert the unserialized data array to your target encoding (there is a helper method t3lib_cs::convArray which you might be able to use for this purpose).
Serialize the converted data and save it back to the templatemapping field.
Fastest way: just change the field manually back to mediumtext. All mappings should be fine again. I know, it's quick and dirty, but worked for me...

How to use Openlayer refresh strategy with django-olwidget?

I would like to have "realtime" like map.
My main question is:
How to use django-olwidget with openlayers OpenLayers.Strategy.Refresh?
Do I need to start back "from scratch" to use manually openlayers?
With django-olwidget, the data is on the web page so the args which define data-source, protocol.
My "second" question is about which format should I choose...
geoJSON? kml? other?
Can those formats contain openlayers point specific "style" specifications like:
{'graphic_name': 'square', 'point_radius': 10, 'fill_color': "#ABBAAB', 'stroke_color':'#BAABBA'}.
I already overriden the default map template olwidget/multi_layer_map.html to access my map object in JS. I think it should be rather simple to apply a js function on each data layers before passing it to the map.
Thanx in advance.
PS: I'm french speaker.
PS2: I asked this question as a feature request on github: https://github.com/yourcelf/olwidget/issues/89
If you're going to use regularly-refreshing data (without refreshing the page) and serialization formats like geoJSON and KML, django-olwidget won't help you very much out of the box. You might find it easier just to use OpenLayers from scratch.
But if you really wanted to use django-olwidget, here's what I would do:
Subclass olwidget.InfoLayer to create a new vector layer type that uses a network-native format like geoJSON or KML to acquire its data.
Add a corresponding python subclass to be able to use it with Django forms or whatever the use case is. You'll probably need to specify things like the URL from which the map will poll its data.
This is a lot of work beyond writing for OpenLayers directly. The advantages would be that you would get easy Django form integration with the same map.
As to which serialization format to use: I'm partial to JSON flavors over XML flavors such as KML, but it really doesn't matter much -- Django and OpenLayers both speak both fluently.
About the styling,you should take a look at the StyleMap[1] where you can set style properties according to attributes.
For the main question, I’m sorry I don’t know django-olwidget…
1 - http://openlayers.org/dev/examples/stylemap.html

Pitfalls of generating JSON in Django templates

I've found myself unsatisfied with Django's ability to render JSON data. If I use built in serializes then database foreign key relationships are not included in the data (only the keys). Also, it seems to be impossible to include custom data in the json feed that isn't part of the model being serialized.
As a test I implemented a template that rendered some JSON for the resultset of a particular model. I was able to include/exclude whatever parts of the model I wanted and was able to include custom data as well.
The test seemed to work well and wasn't slower than the recommended serialization methods.
Are there any pitfalls to this using this method of serialization?
While it's hard to say definitively whether this method has any pitfalls, it's the method we use in production as you control everything that is serialized, even if the underlying model is changed. We've been running a high traffic application in for almost two years using this method.
Hope this helps.
One problem might be escaping metacharacters like ". Django's template system automatically escapes dangerous characters, but it's set up to do that for HTML. You should look up exactly what the template escaping does, and compare that to what's dangerous in JSON. Otherwise, you could cause XSS problems.
You could think about constructing a data structure of dicts and lists, and then running a JSON serializer on that, rather than directly on your database model.
I don't understand why you see the choice as being either 'use Django serializers' or 'write JSON in templates'. The middle way, which to my mind is much more robust and fits your use case well, is to build up your data as Python lists/dictionaries and then simply use simplejson.dumps() to convert it to a JSON string.
We use this method to get custom JSON format consumed by datatables.net
It was the easiest method we find to accomplish this task and it looks very fine with no problems so far.
You can find details here: http://datatables.net/development/server-side/django
So far, generating JSON from templates, we've run into the need to escape newlines. Looking at doing simplejson.dumps() next.