def rename_colors(request, colorscheme_id): """ TODO: display the changed colors in a form, so that the user can choose witch colors should really be renamed. """ colorschmeme = ColorScheme.objects.get(id=colorscheme_id) colors = Color.objects.filter(colorscheme=colorschmeme) changed_colors = 0 existing_colors = [] for color in colors: hex_string = color.value old_color_name = color.name new_color_name = unique_color_name(existing_colors, hex_string) existing_colors.append(new_color_name) # needed to make names unique if new_color_name == old_color_name: # nothing to do continue color.name = new_color_name color.save() changed_colors += 1 messages.info(request, "%s colors exist. %s changed" % (len(existing_colors), changed_colors)) url = reverse("admin:pylucid_colorscheme_change", args=(colorscheme_id,)) return http.HttpResponseRedirect(url)
def rename_colors(request, colorscheme_id): """ TODO: display the changed colors in a form, so that the user can choose witch colors should really be renamed. """ colorschmeme = ColorScheme.objects.get(id=colorscheme_id) colors = Color.objects.filter(colorscheme=colorschmeme) changed_colors = 0 existing_colors = [] for color in colors: hex_string = color.value old_color_name = color.name new_color_name = unique_color_name(existing_colors, hex_string) existing_colors.append(new_color_name) # needed to make names unique if new_color_name == old_color_name: # nothing to do continue color.name = new_color_name color.save() changed_colors += 1 messages.info( request, "%s colors exist. %s changed" % (len(existing_colors), changed_colors)) url = reverse("admin:pylucid_colorscheme_change", args=(colorscheme_id, )) return http.HttpResponseRedirect(url)
def update_colorscheme(self): """ merge colors from headfiles with the colorscheme. """ if not self.render: # No CSS ColorScheme entries in the content -> do nothing return # Get all existing color values from content content, content_colors = unify_spelling(self.content) # Find the most appropriate entry that has the most match colors. best_score = None best_colorscheme = None tested_colorschemes = 0 for colorscheme in self.iter_colorschemes(): tested_colorschemes += 1 score = colorscheme.score_match(content_colors) if score > best_score: best_colorscheme = colorscheme best_score = score if best_colorscheme is None: failsafe_message( _('No existing colorscheme to merge colors found, ok. (tested %s colorschemes)') % tested_colorschemes ) best_colorscheme_dict = {} values2colors = {} colorschemes_data = {} else: failsafe_message( _('Merge colors with colorscheme "%(name)s" (score: %(score)s, tested %(count)s colorschemes)') % { "name": best_colorscheme.name, "score": best_score, "count": tested_colorschemes, } ) best_colorscheme_dict = best_colorscheme.get_color_dict() values2colors = dict([(v, k) for k, v in best_colorscheme_dict.iteritems()]) colorschemes_data = {best_colorscheme:best_colorscheme_dict} existing_color_names = set(best_colorscheme_dict.keys()) if settings.DEBUG: failsafe_message("Use existing colors: %r" % existing_color_names) # Check witch colors are not exist in best colorscheme, yet: best_colorscheme_values = best_colorscheme_dict.values() new_color_values = [] for color_value in content_colors: if color_value not in best_colorscheme_values: new_color_values.append(color_value) # Collect color information from all other colorschemes witch used this headfile: for colorscheme in self.iter_colorschemes(skip_colorschemes=colorschemes_data.keys()): color_dict = colorscheme.get_color_dict() colorschemes_data[colorscheme] = color_dict for color_name, color_value in color_dict.iteritems(): existing_color_names.add(color_name) if color_value not in values2colors: values2colors[color_value] = color_name # Create all new colors in any other colorscheme witch used this headfile: for new_color_value in new_color_values: if new_color_value in values2colors: # Use color name from a other colorscheme color_name = values2colors[new_color_value] else: # this color value doesn't exist in any colorscheme, give it a unique name color_name = unique_color_name(existing_color_names, new_color_value) values2colors[new_color_value] = color_name existing_color_names.add(color_name) # Replace colors in content and create not existing in every colorscheme update_info = {} for color_value, color_name in values2colors.iteritems(): # Replace colors with django template placeholders content = content.replace("#%s;" % color_value, "{{ %s }};" % color_name) # Create new colors for colorscheme in self.iter_colorschemes(): color_dict = colorschemes_data[colorscheme] if color_name in color_dict: # This color exist in this colorscheme continue color, created = Color.objects.get_or_create( colorscheme=colorscheme, name=color_name, defaults={"value": color_value} ) color.save() if created: if colorscheme not in update_info: update_info[colorscheme] = [] update_info[colorscheme].append(color) # Create page messages for colorscheme, created_colors in update_info.iteritems(): msg = _('Colors %(colors)s created in colorscheme "%(scheme)s"') % { "colors": ", ".join(['"%s:%s"' % (c.name, c.value) for c in created_colors]), "scheme": colorscheme.name, } failsafe_message(msg) self.content = content